1

I am trying to parse an array of JSON and return them.

I saw that I could use something like this outside of an HttpTrigger function in Python

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

I would like to do this in my HTTP trigger function

I imagined something like this:

with open('json_file.json','rb') as file:
               jsonStr = file.read()
               
return func.HttpResponse(json.loads(jsonStr)['Body']['bytes'] for jsonStr item in func.HttpResponse, status_code=200)

Where json_file.json is a file with the following content.

json_file.json

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

I know the return will not work with the "func.HttpResponse", but I can't figure out how to loop through all the JSON objects in my file using an HTTP trigger function in Python.

Kind Regards

0

1 Answer 1

1
import json
with open('json_file.json','rb') as file:
               test = file.readlines()
num_lines = sum(1 for line in open('json_file.json'))
jsonarr = []
print(num_lines)
for x in range(1,num_lines-1):
    if x!=3:
        #print(test[x].decode('utf8')[:-3])
        data = json.loads(test[x].decode('utf8')[:-3])
        jsonarr.append(data)
    else:
        #print(test[x].decode('utf8'))
        data = json.loads(test[x].decode('utf8'))
        jsonarr.append(data)

for json in jsonarr:
    item = json["id"]
    print(item)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.