Drakorex
Drakorex

Reputation: 41

How to use python parse json with nested children

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.

Upvotes: 1

Views: 1351

Answers (1)

Joe Iddon
Joe Iddon

Reputation: 20414

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"
  }
]

Upvotes: 2

Related Questions