Lukas Petrikas
Lukas Petrikas

Reputation: 85

Not able to parse simple json object

In my python program, I am using requests library to make post request to the server. It is then returning me information that I convert to the json format as following:

    response = requests.post(URL_get_box_info, data = jsonData, headers=headers)
    data = response.json()

The json formatted response looks like:

{'SearchCompletedActions': [
{
'Id': '123', 
'ConnectDateTime': '2021-05-06T15:24:24.2966667Z',
'WorkerId': '123',
'WorkerName': 'aaa',  
'BatchNumber': '155', 
'Documentation': '222',
'OperationalNumber': '234',
'Imei': '123',
'SerialNumber': '123', 
'Completed': True, 
'Pass': True
}
,
{
'Id': '144', 
'ConnectDateTime': '2021-05-06T15:24:24.2966667Z',
'WorkerId': '123',
'WorkerName': '111', 
'BatchNumber': '123', 
'Documentation': '555',
'OperationalNumber': '123',
'Imei': '555',
'SerialNumber': '1233', 
'Completed': True, 
'Pass': True
}]
}

All I want to do now is to display this data in a nice format in terminal. I have downloaded jq module but I am not able to parse the json for some reason. I have also tried: http://jsonviewer.stack.hu/

Attempting to use jq to display json in terminal returns an error:

pi@raspberrypi:~/Desktop/programming/json_data $ jq . response2.json
parse error: Invalid numeric literal at line 1, column 26

I am not able to understand what is the issue here. The json looks alright to me. I appreciate any help

Upvotes: 1

Views: 577

Answers (1)

Brian Destura
Brian Destura

Reputation: 12078

The output you have from response.json() is already JSON decoded, so its actually represented as a dict in python. If you want the valid json format you can do this:

import json
json.dumps(data)

which will return a string that is in a valid json format or use

response.text

Upvotes: 1

Related Questions