-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.

jhon.smith
  • 1,963
  • 6
  • 30
  • 56

1 Answers1

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'}]
not_speshal
  • 22,093
  • 2
  • 15
  • 30