-2

I have a json file called test.json

[
    {
        "uuid": "4c85cb89-88bb-4019-81ee-1215363c9e9a",
        "ulid": "01FX3473AG0WJHEQ29A62HGQA9"
    },
    {
        "uuid": "1d5af2a4-9ed4-4f96-996c-6d1580a691b4",
        "ulid": "01FX34BBRC5P3C1ECMDG9Q5VJQ"
    }
]

How do I parse this json file ?

One object at a time.

So the first json object is with uuid that starts with 4c85 and the second one is with 1d5

My expected output is from the json array

I need to get individual objects and then do something with the keys of the object.

So far my code is not working because

with open("test.json", encoding="utf-8") as f:    
    for line in f.readlines():                    
        print(line)    

Its getting the whole array as a line.

0

1 Answer 1

1

You can read the file to a list of dictionaries:

import json
my_list = json.load(open("test.json"))

>>> my_list
[{'uuid': '4c85cb89-88bb-4019-81ee-1215363c9e9a',
  'ulid': '01FX3473AG0WJHEQ29A62HGQA9'},
 {'uuid': '1d5af2a4-9ed4-4f96-996c-6d1580a691b4',
  'ulid': '01FX34BBRC5P3C1ECMDG9Q5VJQ'}]
Sign up to request clarification or add additional context in comments.

2 Comments

Please mark questions like this as duplicates, no need for another answer.
Thanks not_speshal Indeed I missed to call the open method in the json.load

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.