1

I am trying to collect specific data from this json using python and I can not figure out how to navigate the structure.

I have tried looking at similar questions on and cant seem to figure it out.

This is the python code that I have

import json
import requests
response = requests.get("example.com/data.json")
data = json.loads(response.text)
#print (json.dumps(data, indent=4))
with open("data_file.json", "w") as write_file:
    json.dump(data, write_file)

for stuff in data['Children']:
    print(stuff['id'])

Here is part of the json I am trying to read

{
    "Min": "Min", 
    "Text": "Sensor", 
    "ImageURL": "", 
    "Value": "Value", 
    "Children": [
        {
            "Min": "", 
            "Text": "PC", 
            "ImageURL": "images_icon/computer.png", 
            "Value": "", 
            "Children": [
                {
                    "Min": "", 
                    "Text": "MSI Z170A GAMING M7 (MS-7976)", 
                    "ImageURL": "images_icon/mainboard.png", 
                    "Value": "", 
                    "Children": [], 
                    "Max": "", 
                    "id": 2
                }, 
                {
                    "Min": "", 
                    "Text": "Intel Core i7-6700K", 
                    "ImageURL": "images_icon/cpu.png", 
                    "Value": "", 
                    "Children": [
                        {
                            "Min": "", 
                            "Text": "Clocks", 
                            "ImageURL": "images_icon/clock.png", 
                            "Value": "", 
                            "Children": [
                                {
                                    "Min": "100 MHz", 
                                    "Text": "Bus Speed", 
                                    "ImageURL": "images/transparent.png", 
                                    "Value": "100 MHz", 
                                    "Children": [], 
                                    "Max": "100 MHz", 
                                    "id": 5
                                }, 
                                {
                                    "Min": "4408 MHz", 
                                    "Text": "CPU Core #1", 
                                    "ImageURL": "images/transparent.png", 
                                    "Value": "4409 MHz", 
                                    "Children": [], 
                                    "Max": "4409 MHz", 
                                    "id": 6
                                }, 
                                {
                                    "Min": "4408 MHz", 
                                    "Text": "CPU Core #2", 
                                    "ImageURL": "images/transparent.png", 
                                    "Value": "4409 MHz", 
                                    "Children": [], 
                                    "Max": "4409 MHz", 
                                    "id": 7
                                },
                            ], 
                            "Max": "", 
                            "id": 4
                        }, 
                        {
                            "Min": "", 
                            "Text": "Temperatures", 
                            "ImageURL": "images_icon/temperature.png", 
                            "Value": "", 
                            "Children": [
                                {
                                    "Min": "24.0 \u00b0C", 
                                    "Text": "CPU Core #1", 
                                    "ImageURL": "images/transparent.png", 
                                    "Value": "32.0 \u00b0C", 
                                    "Children": [], 
                                    "Max": "58.0 \u00b0C", 
                                    "id": 11
                                }, 
                                {
                                    "Min": "30.0 \u00b0C", 
                                    "Text": "CPU Package", 
                                    "ImageURL": "images/transparent.png", 
                                    "Value": "36.0 \u00b0C", 
                                    "Children": [], 
                                    "Max": "62.0 \u00b0C", 
                                    "id": 15
                                }
                            ], 
                            "Max": "", 
                            "id": 10
                        }, 

                    ], 
                    "Max": "", 
                    "id": 3
                }, 
            ], 
            "Max": "", 
            "id": 1
        }
    ], 
    "Max": "Max", 
    "id": 0
}

I am getting back only "1" returned. I need to get the Min, Max, Value from each entry but id was the only thing that I have been able to get so far.

2
  • 2
    You need to use recursion, you can try googling something like "python traverse nested structure recursively" to get some ideas. Commented Apr 14, 2019 at 19:40
  • @AlexHall Is my answer what you had in mind? Commented Apr 14, 2019 at 19:56

1 Answer 1

2

Recursion is pretty neat here... if the Python tricks need explaining, please ask.

def get_stuff(data_dict):
    #gets: min,max,value from input and returns in a list alongside children's
    # create new object of the relevant data fields
    my_data = {k:data_dict[k] for k in ['Min', 'Max', 'Value']}
    # recursively get each child's data and add that to a new list
    children_data = [d for child in data_dict['Children'] for d in get_stuff(child)]
    # add our data to the start of the children's data
    return [my_data] + children_data

Which, when run on the data you posted in the question, gives:

[
  {
    "Min": "Min",
    "Max": "Max",
    "Value": "Value"
  },
  {
    "Min": "",
    "Max": "",
    "Value": ""
  },
  {
    "Min": "",
    "Max": "",
    "Value": ""
  },
  {
    "Min": "",
    "Max": "",
    "Value": ""
  },
  {
    "Min": "",
    "Max": "",
    "Value": ""
  },
  {
    "Min": "100 MHz",
    "Max": "100 MHz",
    "Value": "100 MHz"
  },
  {
    "Min": "4408 MHz",
    "Max": "4409 MHz",
    "Value": "4409 MHz"
  },
  {
    "Min": "4408 MHz",
    "Max": "4409 MHz",
    "Value": "4409 MHz"
  },
  {
    "Min": "",
    "Max": "",
    "Value": ""
  },
  {
    "Min": "24.0 \u00b0C",
    "Max": "58.0 \u00b0C",
    "Value": "32.0 \u00b0C"
  },
  {
    "Min": "30.0 \u00b0C",
    "Max": "62.0 \u00b0C",
    "Value": "36.0 \u00b0C"
  }
]
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, I am very new to python, could you explain how I would run this on the data?
@Drakorex Put this function at the bottom of your code, then below it, call it on your data variable and store the output in a variable. That is to say: output = get_stuff(data). You can then print(output), or better, what I did to get this neat formatting was like print(json.dumps(output,indent=2)).
@Joe_Iddon Awesome, what if I wanted only the integer "100" instead of "Max": "100 MHz"?
Then just replace the my_data line with: my_data = [data_dict[k] for k in ['Min', 'Max', 'Value']] which will mean each element in the output list will be a sub-list with just the values from what was previously a dictionary.

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.