0

I'm trying to parse a JSON file with multiple objects by one of the values in the object, however I am not sure if this is possible with my method.

JSON

[{"Temp":512,"Name":"sdfd3","SearchTags":["North"]},
[{"Temp":45,"Name":"dfs5","SearchTags":["South"]},
[{"Temp":251,"Name":"sfsd6","SearchTags":["North"]},

Python

myObj = response.content;

x = json.loads(myObj)


for item in x:
    if myObj(Name) == "dfs5":
        print(Temp, SearchTags)

I am new to JSON and Python but cannot seem to find any guidance on searching where a JSON file has multiple lines.

Any help is greatly appreciated.

1
  • The given JSON file is invalid even considering multiple JSON objects. Square brackets not closed. Commented Apr 22, 2020 at 17:06

1 Answer 1

1

First of all your JSON object is not correct. Still, I have tried to recreate your issue.

myObj = [
    {"Temp":"512","Name":"sdfd3","SearchTags":["North"]},
    {"Temp":45,"Name":"dfs5","SearchTags":["South"]},
    {"Temp":251,"Name":"sfsd6","SearchTags":["North"]}
]

for item in myObj:
    if item.get("Name") == "dfs5":
        print(item["Temp"], item["SearchTags"])
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, will give this a test. I have copied the example from a really large JSON file and just checked and I have made a typo when copying, the JSON file is formatted like you have put.
Thanks, I seem to be getting the error below when using the code above. if item.get("Name") == "dfs5": AttributeError: 'int' object has no attribute 'get'
Well, I can't recreate your issue so can't say much. Now it seems that you have redefined the item variable to an int.

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.