1

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

2
  • No problem in the order of the elements of the dict. Commented Jul 27, 2017 at 13:18
  • Thanks for your answer! Commented Jul 27, 2017 at 13:40

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

would be nice explaining that printing a dictionary gives a string, hence the confusion.
You might also want to show how to access data in a nested dictionary, e.g. data["Header"]["Version"]. Also, comment on the order of the data in a dictionary.
OK, that seems to work. Thanks for that! But when I try to read Number it says "TypeError: 'int' object is not subscriptable" and when trying AD I get the message "TypeError: string indices must be integers". What's the problem?
I found my mistake. I have to read one value only and go trough step by step. Then it's not problem. When I try to read a string and an int value at the same time these errors seem to appear. Thanks for your help!
Hi @SimonD., if my answer solved your problem, you can accept it by clicking the check mark on the right margin. Thank you!

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.