0

I want to read a json format file in python like the following code:

import json 
with open('/Users/mm/Downloads/Sybil_0709/traceJSON-9-7-A0-25202-7.json') as f:
    data = json.load(f)

and the format of data in json file is like the following:

{"type":2,"rcvTime":25202.0,"pos":[265.1170585817953,50.63668707563142,0.0],"pos_noise":[3.0443504254537427,3.012953826828906,0.0],"spd":[-0.07537135458144502,0.7293700027197375,0.0],"spd_noise":[0.0,0.0,0.0],"acl":[-0.22882024035485216,2.21455321228441,0.0],"acl_noise":[0.0009127765282318503,0.0009127765282318503,0.0],"hed":[-0.10279023863383258,0.994703054605544,0.0],"hed_noise":[7.140621228823163,6.5884995959359229,0.0]}
{"type":2,"rcvTime":25203.0,"pos":[265.1699249881993,52.59385797481798,0.0],"pos_noise":[3.0462168835460057,3.1256992472669999,0.0],"spd":[-0.29879155673167498,2.8914114620366195,0.0],"spd_noise":[-0.00006879720336298315,0.0006657518188789772,0.0],"acl":[-0.1860122509250316,1.800253207324483,0.0],"acl_noise":[0.00006879720336298315,0.0006657518188789772,0.0],"hed":[-0.09352386990051694,0.9956170377001546,0.0],"hed_noise":[7.301952656805316,5.307503095056338,0.0]}

however I just get the following error:

JSONDecodeError                           Traceback (most recent call last)
/var/folders/ss/_3stplfj063cqkd70rr3rsdh0000gn/T/ipykernel_59160/1300083755.py in <module>
      1 import json
      2 with open('/Users/mm/Downloads/Sybil_0709/traceJSON-9-7-A0-25202-7.json') as f:
----> 3     data = json.load(f)

~/opt/anaconda3/lib/python3.9/json/__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    291     kwarg; otherwise ``JSONDecoder`` is used.
    292     """
--> 293     return loads(fp.read(),
    294         cls=cls, object_hook=object_hook,
    295         parse_float=parse_float, parse_int=parse_int,

~/opt/anaconda3/lib/python3.9/json/__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    344             parse_int is None and parse_float is None and
    345             parse_constant is None and object_pairs_hook is None and not kw):
--> 346         return _default_decoder.decode(s)
    347     if cls is None:
    348         cls = JSONDecoder

~/opt/anaconda3/lib/python3.9/json/decoder.py in decode(self, s, _w)
    338         end = _w(s, end).end()
    339         if end != len(s):
--> 340             raise JSONDecodeError("Extra data", s, end)
    341         return obj
    342 

JSONDecodeError: Extra data: line 2 column 1 (char 428)
MinaMRM
  • 343
  • 4
  • 16
  • 2
    The file is not valid JSON syntax. It's a concatenation of JSON objects. Would need to add '[' to the begininng and ']' to make it a list of objects with a comma after all but the last one. You can read each line as a String then parse the JSON for that line. – CodeMonkey Apr 12 '22 at 16:29
  • Duplicate of https://stackoverflow.com/questions/12451431/loading-and-parsing-a-json-file-with-multiple-json-objects ? – CristiFati Apr 12 '22 at 16:37

1 Answers1

0

The data file itself is not valid JSON syntax as the JSON exceptions are showing but each line in the file is a JSON object so you can read the file one line at a time and parse each line as a JSON object from a string.

Try something like this:

import json

with open('traceJSON-9-7-A0-25202-7.json') as f:
    for s in f:
        data = json.loads(s)
        print(data.keys())
        # add your logic here

Output:

dict_keys(['type', 'rcvTime', 'pos', 'pos_noise', 'spd', 'spd_noise', 'acl', 'acl_noise', 'hed', 'hed_noise'])
dict_keys(['type', 'rcvTime', 'pos', 'pos_noise', 'spd', 'spd_noise', 'acl', 'acl_noise', 'hed', 'hed_noise'])
CodeMonkey
  • 22,825
  • 4
  • 35
  • 75