0

I want to get the data from a json. I have the idea of a loop to access all levels. I have only been able to pull data from a single block.

print(output['body']['data'][0]['list'][0]['outUcastPkts'])

How do I get the other data?

import json,urllib.request
data = urllib.request.urlopen("http://172.0.0.0/statistic").read()
output = json.loads(data)
for elt in output['body']['data']:
 print(output['body']['data'][0]['inUcastPktsAll'])
 for elt in output['list']:
 print(output['body']['data'][0]['list'][0]['outUcastPkts'])
{
 "body": {
 "data": [
 { 
 "inUcastPktsAll": 3100617019,
 "inMcastPktsAll": 7567,
 "inBcastPktsAll": 8872,
 "outPktsAll": 8585575441,
 "outUcastPktsAll": 8220240108,
 "outMcastPktsAll": 286184143,
 "outBcastPktsAll": 79151190,
 "list": [
 {
 "outUcastPkts": 117427359,
 "outMcastPkts": 1990586,
 "outBcastPkts": 246120
 },
 {
 "outUcastPkts": 0,
 "outMcastPkts": 0,
 "outBcastPkts": 0
 }
 ]
 },
 {
 "inUcastPktsAll": 8269483865,
 "inMcastPktsAll": 2405765,
 "inBcastPktsAll": 124466,
 "outPktsAll": 3101194852,
 "outUcastPktsAll": 3101012296,
 "outMcastPktsAll": 173409,
 "outBcastPktsAll": 9147,
 "list": [
 {
 "outUcastPkts": 3101012296,
 "outMcastPkts": 90488,
 "outBcastPkts": 9147
 },
 {
 "outUcastPkts": 0,
 "outMcastPkts": 0,
 "outBcastPkts": 0
 }
 ]
 }
 ],
 "msgs": [ "successful" ]
 },
 "header": {
 "opCode": "1",
 "token": "",
 "state": "",
 "version": 1
 }
}
asked Mar 5, 2021 at 7:31
1
  • 1
    What do you mean by other data? Commented Mar 5, 2021 at 7:40

2 Answers 2

1
output = json.loads(data) #Type of output is a dictionary.
 #Try to use ".get()" method.
print(output.get('body')) #Get values of key 'body'
print(output.get('body').get('data')) #Get a list of key 'data' 

If a key doesn't exist, the '.get()' method will return None. https://docs.python.org/3/library/stdtypes.html#dict.get

answered Mar 5, 2021 at 7:53
Sign up to request clarification or add additional context in comments.

Comments

0

In python you can easily iterate over the objects of a list like so:

>>> l = [1, 2, 3, 7]
>>> for elem in l:
... print(elem)
...
1
2
3
7

This works regarding what can of object do you have in the list (integers, tuples, dictionaries). Having that in mind, your solution was not far off, you only to do the following changes:

for entry in output['body']['data']:
 print(entry['inUcastPktsAll'])
 for list_element in entry['list']:
 print(list_element['outUcastPkts'])

This will give you the following for the json object you have provided:

3100617019
117427359
0
8269483865
3101012296
0
Dharman
34k27 gold badges105 silver badges156 bronze badges
answered Mar 5, 2021 at 8:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.