简体   繁体   中英

How to extract value from complicated JSON with python

Cant figure out how to extract values from JSON. I've tryed jsonpath-rw but with no luck.

I've tryed jsonpath-rw but with no luck.

This is part of JSON file I need to parse

{
  "id": 0,
  "Text": "Sensor",
  "Children": [
    {
      "id": 1,
      "Text": "USER-PC",
      "Children": [
        {
          "id": 2,
          "Text": "Intel Celeron E3300",
          "Children": [
            {
              "id": 3,
              "Text": "Clocks",
              "Children": [
                {
                  "id": 4,
                  "Text": "Bus Speed",
                  "Children": [],
                  "Min": "200 MHz",
                  "Value": "200 MHz",
                  "Max": "200 MHz",
                  "ImageURL": "images/transparent.png"
                },
                {
                  "id": 5,
                  "Text": "CPU Core #1",
                  "Children": [],
                  "Min": "1200 MHz",
                  "Value": "1200 MHz",
                  "Max": "2500 MHz",
                  "ImageURL": "images/transparent.png"
                },
                {
                  "id": 6,
                  "Text": "CPU Core #2",
                  "Children": [],
                  "Min": "1200 MHz",
                  "Value": "1200 MHz",
                  "Max": "2500 MHz",
                  "ImageURL": "images/transparent.png"
                }
              ],
              "Min": "",
              "Value": "",
              "Max": "",
              "ImageURL": "images_icon/clock.png"
            },
            {
              "id": 7,
              "Text": "Temperatures",
              "Children": [
                {
                  "id": 8,
                  "Text": "CPU Core #1",
                  "Children": [],
                  "Min": "39,0 °C",
                  "Value": "39,0 °C",
                  "Max": "46,0 °C",
                  "ImageURL": "images/transparent.png"
                },
                {
                  "id": 9,
                  "Text": "CPU Core #2",
                  "Children": [],
                  "Min": "31,0 °C",
                  "Value": "31,0 °C",
                  "Max": "45,0 °C",
                  "ImageURL": "images/transparent.png"
                }
              ],
              "Min": "",
              "Value": "",
              "Max": "",
              "ImageURL": "images_icon/temperature.png"
            },
            {
              "id": 10,
              "Text": "Load",
              "Children": [
                {
                  "id": 11,
                  "Text": "CPU Total",
                  "Children": [],
                  "Min": "0,0 %",
                  "Value": "0,8 %",
                  "Max": "100,0 %",
                  "ImageURL": "images/transparent.png"
                },
                {
                  "id": 12,
                  "Text": "CPU Core #1",
                  "Children": [],
                  "Min": "0,0 %",
                  "Value": "1,6 %",
                  "Max": "100,0 %",
                  "ImageURL": "images/transparent.png"
                },
                {
                  "id": 13,
                  "Text": "CPU Core #2",
                  "Children": [],
                  "Min": "0,0 %",
                  "Value": "0,0 %",
                  "Max": "100,0 %",
                  "ImageURL": "images/transparent.png"
                }
              ],
              "Min": "",
              "Value": "",
              "Max": "",
              "ImageURL": "images_icon/load.png"
            }
          ],

For example, I need the value of "Value", under "id": 8

Here is JSONPATH for it: $.Children[0].Children[0].Children[1].Children[0].Value

If your data is in the string json_data , just load the json and access the data as any other dictionary or list:

import json
data = json.loads(json_data)
print(data["Children"][0]["Children"][0]["Children"][1]["Children"][0]["Value"])

使用json模块

import json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM