0

I have a JSON file with values in [] brackets as shown. I am trying to create a [key:value] dictionary that shows the [id_value : text_value.]

{"id":6127465, "users":{"name":[{"dr_info":[28,37],"text":"trees"}],"favorites":[]}}
{"id":9285628, "users":{"name":[{"dr_info":[16,24],"text":"grass"}, {"id_info":[30,34],"text":"trees"}],"favorites":[]}}
{"id":7625927, "users":{"name":[{"dr_info":[18,23],"text":"grass"}],"favorites":[], "type" : "photo"}}
{"id":8725946, "users":{"name":[{"dr_info":[23,33],"text":"grass"}, {"id_info":[37,41],"text":"trees"}],"favorites":[]}}

Taking as an example the first two JSON lines above. The output for the dictionary would be :


[6127465 : 'trees']

[9285628 : 'grass' , 'trees'] and so on.


Here is what I have coded so far but I can't get the values very well.

dict={}
with open(fileName, 'r') as file_to_read:
    for line in file_to_read:
        data = json.loads(line)
        json_tree = objectpath.Tree(data)
        json.dumps(data, ensure_ascii=True)
        dict[json_tree.execute('$.id')] = json_tree.execute('$.users.name.text')
return dict

New edit. (Answer)

dict={}
    with open(fileName, 'r') as file_to_read:
        for line in file_to_read:
            data = json.loads(line)
            json_tree = objectpath.Tree(data)
            json.dumps(data)
            dict[json_tree.execute('$.id')] = list(json_tree.execute('$.users.name.text'))
    return dict
Biyinzika
  • 75
  • 9
  • why are you using `objectpath`? – n8sty Sep 27 '18 at 03:44
  • I use it to traverse the values. I can then output the values as a list / tuple. The original python json library did not manage to traverse the values as well as object path. – Biyinzika Sep 27 '18 at 05:25
  • The line `json.dumps(data)` is useless. – stovfl Sep 27 '18 at 08:13
  • I tried this with your example data; after `data = json.loads(line)` I can successfully get the ID at `data["id"]` and the name info in the list at `data["users"]["name"]`. There's no need for `objectpath` here. – kungphu Sep 27 '18 at 08:17
  • Thanks for that. My output for lists of the JSON arrays kept on having a 'u' before them. I read somewhere that json.dumps with its default values for ASCII helps eliminate those. – Biyinzika Sep 27 '18 at 10:48

1 Answers1

0

New edit : Answer

dict={}
    with open(fileName, 'r') as file_to_read:
        for line in file_to_read:
            data = json.loads(line)
            json_tree = objectpath.Tree(data)
            json.dumps(data)
            dict[json_tree.execute('$.id')] = list(json_tree.execute('$.users.name.text'))
    return dict
Biyinzika
  • 75
  • 9