0

I am writing a python program that reads, parses, and hopefully filters out results in a JSON file coming from a URL. I have searched and watched videos for methods that could filter out specific results from a JSON file. However, this JSON file seems a little bit complicated.

 {
  "collection": {
    "version": "1.0",
    "href": "http://images-api.nasa.gov/search?q=galaxy&page=1",
    "items": [
      {
        "href": "https://images-assets.nasa.gov/image/PIA04921/collection.json",
        "data": [
          {
            "center": "JPL",
            "title": "Andromeda Galaxy",
            "nasa_id": "PIA04921",
            "media_type": "image",
            "keywords": [
              "Galaxy Evolution Explorer GALEX"
            ],
            "date_created": "2003-12-10T22:41:32Z",
            "description_508": "This image is from NASA Galaxy Evolution Explorer is an observation of the large galaxy in Andromeda, Messier 31. The Andromeda galaxy is the most massive in the local group of galaxies that includes our Milky Way.",
            "secondary_creator": "NASA/JPL/California Institute of Technology",
            "description": "This image is from NASA Galaxy Evolution Explorer is an observation of the large galaxy in Andromeda, Messier 31. The Andromeda galaxy is the most massive in the local group of galaxies that includes our Milky Way."
          }
        ],
        "links": [
          {
            "href": "https://images-assets.nasa.gov/image/PIA04921/PIA04921~thumb.jpg",
            "rel": "preview",
            "render": "image"
          }
        ]
      },
      {
        "href": "https://images-assets.nasa.gov/image/PIA04634/collection.json",
        "data": [ (different galaxies with their data etc.) 

The JSON file's content is too long, and I am trying to filter out the galaxies with a specific title such as Sombrero. Since each father array has so many children, how would I implement it? I have tried doing the following:

from urllib.request import urlopen
import json

url = "https://images-api.nasa.gov/search?q=galaxy&page=1"

response = urlopen(url)
data_json = json.loads(response.read())

list(list(filter(lambda x:x["title"]=="Sombrero", data_json)))
4
  • I have also tried using the lambda method but failed to accomplish my task. Commented Feb 6, 2022 at 7:11
  • in the given json, what do you want returned? there's no "sombrero" here Commented Feb 6, 2022 at 7:12
  • It is a very long JSON file, and the way how each planet's info is formatted is the same. The file does contain several data about different planets. Thus, I want to go through all the JSON file and search for Sombrero, and have its info printed out. I have seen some questions here and tried following the guidelines. Commented Feb 6, 2022 at 7:16
  • Each planet has a "data" block with the "title" string. Commented Feb 6, 2022 at 7:19

1 Answer 1

1

You can use list comprehension:

planets = [i for i in data_json['collection']['items'] if i['data'][0]['title'] == 'Andromeda Galaxy']

To filter for partial string matches:

planets = [i for i in data_json['collection']['items'] if 'Andromeda' in i['data'][0]['title']]

To filter for partial string matches regardless capitalization:

planets = [i for i in data_json['collection']['items'] if lower('Andromeda') in lower(i['data'][0]['title'])]
Sign up to request clarification or add additional context in comments.

6 Comments

#When I did this planets = [i for i in data_json['collection']['items'] if i['data'][0]['title'] == 'Sombrero'] print(planets) #This was the output ----> []
That is because there is no Sombrero galaxy in the data as @enke commented
I just tried to use "Andromeda" instead of "Sombrero" and got the same output
That is because you're now looking for a partial string match, see updated answer for a solution
When I filtered out "andromeda" by using ctrl + f in images-api.nasa.gov/search?q=galaxy&page=1 , I got 11, whereas in my PyCharm ----> []
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.