0

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.

[
{"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"}
]

Edit: I'm getting the following error when I try to use it with an array: The file I am trying to parse is as follows: ( Result: Failure Exception: TypeError: list indices must be integers or slices, not str )

PrayingMantis
  • 150
  • 2
  • 13

2 Answers2

1

response is a list so you were indexing a list with a string.

response['Body'] = [json.loads(item['Body']['bytes']) for item in response] should work.

Anson Miu
  • 1,171
  • 7
  • 6
  • Out of curiosity, is it the same case if I return a func.HttpResponse ? ``` return func.HttpResponse(json.loads(jsonStr['Body']['bytes']) for jsonStr in func.HttpResponse,status_code=200) ``` Where jsonStr is a document filled with an array of JSON objects. Should I do something like that? Or am I doing it all wrong? – PrayingMantis Apr 09 '21 at 07:57
  • Assuming you're using Azure Functions here - the body of the HttpResponse should be a JSON string, so something like `func.HttpResponse(json.dumps([ json.loads(item['Body']['bytes']) for item in response ]), mimetype="application/json", status_code=200)` should do it. You will need to define the MIME type of the response to be JSON. See related: https://stackoverflow.com/a/59719773/8704792 – Anson Miu Apr 09 '21 at 08:40
  • Oh alright, thanks for letting me know about the "MIME type", Should I replace "response" with "jsonStr" which is the file that contains the JSON array? I defined it by doing: with open('json_file.json','rb') as file: jsonStr = file.read() – PrayingMantis Apr 11 '21 at 11:37
1

I think you're looking for this?


for dict1 in x:
   print(dict1["Body"]["bytes"])

Your error was, you weren't iterating through the dicts in the list.

Buddy Bob
  • 5,829
  • 1
  • 13
  • 44