Home > Software design >  Search for many things in a json file
Search for many things in a json file

Time:07-29

I am having issue creating a dictionary list by searching for many strings. From a json document I would like to extract the type of cat and snail and export these findings into a separate file. I am able to do it for one feature, that being cat. However I am having issues with doing it with more than 1 feature. Or even look to do it in an efficient way.

Sample Data

"type": "blah",
"animals": [
    {
        "type": "dog1",
        "name": "oscar",
        }
    },
    {
        "type": "dog2",
        "name": "John",
        }
    },
    {
        "type": "snail1",
        "name": "Fred",
        "Colors": [
            "Red"
        ],
        "Contact_info": [
            {
                "Owner": "Jill",
                "Owner_number": "123"
            }
        ],
    
    },
    {
        "type": "cat3",
        "name": "Freddy",
        "Colors": [
            "Blue"
        ],
        "Contact_info": [
            {
                "Owner": "Ann",
                "Owner_number": "1323"
            }
        ],

Idea Result

{
    "type": "snail1",
    "name": "Fred",
    "Colors": [
        "Red"
    ],
    "Contact_info": [
        {
            "Owner": "Jill",
            "Owner_number": "123"
        }
    ],

},
{
    "type": "cat3",
    "name": "Freddy",
    "Colors": [
        "Blue"
    ],
    "Contact_info": [
        {
            "Owner": "Ann",
            "Owner_number": "1323"
        }
    ],

Code so far:

with open("data.json", "rb") as f:
    # Load obj list
    data = json.load(f)["animals"]

# Create a list of dictionaries if obj type contains "snail" or cat
animal= [a for a in data if "snail" in a.get("type")]
animal = [a for a in data if "cat" in a.get("type")]

CodePudding user response:

Your code written is partially correct, however, the error comes up where you have similar variables in the last 2 lines. In the snail part, you are creating a variable and then in the cat one you are again redefining that variable (this totally erases the value of the animal variable defined above). For example

var = 'Hi'
var = 'Bye'
print(var) #Output: Bye

Hence, you should either use different variables or totally use another method

animals_required = ["cat", "snail"]
animal_list = [i for i in data["animals"] for animal in animals_required if animal in i["type"]]
#List Comprehension

CodePudding user response:

This is how you do it:

# Only Cats:
cats = [animal for animal in data.get("animals") if "cat" in animal.get("type")]

# Only Snails:
snails = [animal for animal in data.get("animals") if "snail" in animal.get("type")]

# Both Cats and Snails
cats_n_snails = [
    animal
    for animal in data.get("animals")
    if "cat" in animal.get("type") or "snail" in animal.get("type")
]

Provided that this is the data:

data = {
   "type":"blah",
   "animals":[
      {
         "type":"dog1",
         "name":"oscar"
      },
      {
         "type":"dog2",
         "name":"John"
      },
      {
         "type":"snail1",
         "name":"Fred",
         "Colors":[
            "Red"
         ],
         "Contact_info":[
            {
               "Owner":"Jill",
               "Owner_number":"123"
            }
         ]
      },
      {
         "type":"cat3",
         "name":"Freddy",
         "Colors":[
            "Blue"
         ],
         "Contact_info":[
            {
               "Owner":"Ann",
               "Owner_number":"1323"
            }
         ]
      }
   ]
}
  • Related