What is the correct way to parse a json array with the use of json.loads?

  arrays, json, parsing, python

I’m trying to figure out how I can parse a complete array from a JSON file. I managed to do it with a single object but I have no idea why it doesn’t work for a complete array.

The file I am trying to parse is as follows:

[
{"SequenceNumber": 20, "Offset": "4294967296", "EnqueuedTimeUtc": "4/8/2021 7:22:56 AM", "SystemProperties": {"x-opt-enqueued-time": {"long": 1617866576085}}, "Properties": {"Postman-Token": {"string": "21be7531-8f6b-422c-b676-#####"}}, "Body": {"bytes": "{"id":1,"receiver":"77777777","message":{"test":" test signal","PrsId":3,"DriverId":2,"GUID":"1s3q1d-s546dq1-8e22e","LineId":2,"SvcId":2,"Lat":-456.546547,"Lon":-68.546547,"TimeStamp":"2021-03-18T08:29:36.758Z","Recorder":"dq65ds4qdezzer","Env":"PRD"},"operator":20404,"sender":"MSISDN","binary":1,"sent":"2021-03-29T08:29:36.758Z"}"}},
{"SequenceNumber": 20, "Offset": "4294967296", "EnqueuedTimeUtc": "4/8/2021 7:22:56 AM", "SystemProperties": {"x-opt-enqueued-time": {"long": 1617866576085}}, "Properties": {"Postman-Token": {"string": "21be7531-8f6b-422c-b676-#####"}}, "Body": {"bytes": "{"id":1,"receiver":"77777777","message":{"test":" test signal","PrsId":3,"DriverId":2,"GUID":"1s3q1d-s546dq1-8e22e","LineId":2,"SvcId":2,"Lat":-65634.546547,"Lon":-68.546547,"TimeStamp":"2021-03-18T08:29:36.758Z","Recorder":"dq65ds4qdezzer","Env":"PRD"},"operator":20404,"sender":"MSISDN","binary":1,"sent":"2021-03-29T08:29:36.758Z"}"}},
{"SequenceNumber": 20, "Offset": "4294967296", "EnqueuedTimeUtc": "4/8/2021 7:22:56 AM", "SystemProperties": {"x-opt-enqueued-time": {"long": 1617866576085}}, "Properties": {"Postman-Token": {"string": "21be7531-8f6b-422c-b676-#####"}}, "Body": {"bytes": "{"id":1,"receiver":"77777777","message":{"test":" test signal","PrsId":3,"DriverId":2,"GUID":"1s3q1d-s546dq1-8e22e","LineId":2,"SvcId":2,"Lat":-78946.546547,"Lon":-68.546547,"TimeStamp":"2021-03-18T08:29:36.758Z","Recorder":"dq65ds4qdezzer","Env":"PRD"},"operator":20404,"sender":"MSISDN","binary":1,"sent":"2021-03-29T08:29:36.758Z"}"}}
]

I figured out how to do this with a single object by doing the following in Python:

response['Body'] = json.loads(response['Body']['bytes'])

What am I doing wrong? Shouldn’t it be the same even for an array?

The wished outcome would be like this

[
  {"id":1,"receiver":"77777777","message":{"test":" test signal","VehId":3,"DriverId":2,"GUID":"1s3q1d-s546dq1-8e22e","LineId":2,"SvcId":2,"Lat":-64.546547,"Lon":-68.546547,"TimeStamp":"2021-03-18T08:29:36.758Z","Recorder":"dq65ds4qdezzer","Env":"PRD"},"operator":20404,"sender":"MSISDN","binary":1,"sent":"2021-03-29T08:29:36.758Z"},
  {"id":1,"receiver":"77777777","message":{"test":" test signal","VehId":3,"DriverId":2,"GUID":"1s3q1d-s546dq1-8e22e","LineId":2,"SvcId":2,"Lat":-64.546547,"Lon":-68.546547,"TimeStamp":"2021-03-18T08:29:36.758Z","Recorder":"dq65ds4qdezzer","Env":"PRD"},"operator":20404,"sender":"MSISDN","binary":1,"sent":"2021-03-29T08:29:36.758Z"},
  {"id":1,"receiver":"77777777","message":{"test":" test signal","VehId":3,"DriverId":2,"GUID":"1s3q1d-s546dq1-8e22e","LineId":2,"SvcId":2,"Lat":-64.546547,"Lon":-68.546547,"TimeStamp":"2021-03-18T08:29:36.758Z","Recorder":"dq65ds4qdezzer","Env":"PRD"},"operator":20404,"sender":"MSISDN","binary":1,"sent":"2021-03-29T08:29:36.758Z"}
]

Source: Python Questions

LEAVE A COMMENT