Reputation: 21
I'm using the following code to read data with JSON from a device in my network.
#!/usr/bin/env python
import json
import requests
r = requests.get('http://xxx.xxx.xxx.xxx/link/to/json', auth=('user', 'password'))
print(r.json())
This send back to me the following JSON string which is printed in the terminal.
{'Header': {'Device': '80', 'Timestamp': 1501165924, 'Version': 1}, 'Status code': 0, 'Data': {'Outputs': [{'AD': 'A', 'Value': {'Unit': '0', 'State': 1, 'Value': 30}, 'Number': 1}, {'AD': 'A', 'Value': {'Unit': '0', 'State': 0, 'Value': 0}, 'Number': 2}, {'AD': 'D', 'Value': {'Unit': '0', 'Value': 0}, 'Number': 3}, {'AD': 'D', 'Value': {'Unit': '0', 'Value': 0}, 'Number': 4}, {'AD': 'D', 'Value': {'Unit': '0', 'Value': 0}, 'Number': 5}, {'AD': 'A', 'Value': {'Unit': '0', 'State': 0, 'Value': 0}, 'Number': 6}, {'AD': 'A', 'Value': {'Unit': '0', 'State': 0, 'Value': 0}, 'Number': 7}, {'AD': 'D', 'Value': {'Unit': '0', 'Value': 0}, 'Number': 8}, {'AD': 'D', 'Value': {'Unit': '0', 'Value': 0}, 'Number': 9}, {'AD': 'D', 'Value': {'Unit': '0', 'Value': 0}, 'Number': 10}, {'AD': 'D', 'Value': {'Unit': '0', 'Value': 0}, 'Number': 11}, {'AD': 'D', 'Value': {'Unit': '0', 'Value': 1}, 'Number': 12}, {'AD': 'D', 'Value': {'Unit': '0', 'Value': 0}, 'Number': 13}], 'Inputs': [{'AD': 'A', 'Value': {'Unit': '1', 'Value': 23.1}, 'Number': 1}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 23.0}, 'Number': 2}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 22.5}, 'Number': 3}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 23.1}, 'Number': 4}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 78.3}, 'Number': 5}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 50.8}, 'Number': 6}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 53.1}, 'Number': 7}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 43.2}, 'Number': 8}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 21.8}, 'Number': 9}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 53.2}, 'Number': 10}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 1049.6}, 'Number': 11}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 28.5}, 'Number': 12}, {'AD': 'D', 'Value': {'Unit': '43', 'Value': 0}, 'Number': 13}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 52.9}, 'Number': 14}, {'AD': 'A', 'Value': {'Unit': '1', 'Value': 47.8}, 'Number': 15}, {'AD': 'A', 'Value': {'Unit': '3', 'Value': 350}, 'Number': 16}]}, 'Status': 'OK'}
Unfortunately I don't know how to read the desired data from this string. Doing it manually using find operations on the string is way to complicated. I think there's a way to access the values. What do I have to to do access the value of "Number 3" in the Inputs section?
BTW: I recognized that requests from Python puts the 'Number', 'AD' and 'Value' sections in a different order every time it reads the JSON data from the device. Is that normal or could that lead to problems? In the Browser the same JSON data shows up like this:
{ "Header":{ "Version":1, "Device":"80", "Timestamp":1501166962 }, "Data":{ "Inputs":[ { "Number":1, "AD":"A", "Value":{ "Value":23.0, "Unit":"1" } } , { "Number":2, "AD":"A", "Value":{ "Value":23.0, "Unit":"1" } } , { "Number":3, "AD":"A", "Value":{ "Value":22.4, "Unit":"1" } } , { "Number":4, "AD":"A", "Value":{ "Value":23.0, "Unit":"1" } } , { "Number":5, "AD":"A", "Value":{ "Value":78.3, "Unit":"1" } } , { "Number":6, "AD":"A", "Value":{ "Value":50.7, "Unit":"1" } } , { "Number":7, "AD":"A", "Value":{ "Value":53.1, "Unit":"1" } } , { "Number":8, "AD":"A", "Value":{ "Value":45.6, "Unit":"1" } } , { "Number":9, "AD":"A", "Value":{ "Value":21.8, "Unit":"1" } } , { "Number":10, "AD":"A", "Value":{ "Value":58.9, "Unit":"1" } } , { "Number":11, "AD":"A", "Value":{ "Value":1049.6, "Unit":"1" } } , { "Number":12, "AD":"A", "Value":{ "Value":28.6, "Unit":"1" } } , { "Number":13, "AD":"D", "Value":{ "Value":0, "Unit":"43" } } , { "Number":14, "AD":"A", "Value":{ "Value":59.3, "Unit":"1" } } , { "Number":15, "AD":"A", "Value":{ "Value":51.5, "Unit":"1" } } , { "Number":16, "AD":"A", "Value":{ "Value":351, "Unit":"3" } } ], "Outputs":[ { "Number":1, "AD":"A", "Value":{ "State":1,"Value":30, "Unit":"0" } } , { "Number":2, "AD":"A", "Value":{ "State":0,"Value":0, "Unit":"0" } } , { "Number":3, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":4, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":5, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":6, "AD":"A", "Value":{ "State":0,"Value":0, "Unit":"0" } } , { "Number":7, "AD":"A", "Value":{ "State":0,"Value":0, "Unit":"0" } } , { "Number":8, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":9, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":10, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":11, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } , { "Number":12, "AD":"D", "Value":{ "Value":1, "Unit":"0" } } , { "Number":13, "AD":"D", "Value":{ "Value":0, "Unit":"0" } } ]}, "Status":"OK", "Status code":0 }
As you can see there in the browser there are used double quotes instead of apostrophes.
br, Simon
Upvotes: 1
Views: 104
Reputation: 1161
r.json()
gives you a Python dictionary, so you can do something like
data = r.json()
print data["Status code"]
which would give you the status code from your JSON.
So, if you wanted to access the number
element of the third value in outputs
, you'd do this:
print data["Outputs"][2]["number"]
P.S: Do note that single-quotes and double-quotes are interchangeable in Python.
Upvotes: 4