0

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

2
  • 1
    Python natively supports JSON. Have you tried json module? Commented Aug 10, 2019 at 7:38
  • What do you mean by "had no luck"? Can you show us the code that didn't work? Commented Aug 10, 2019 at 7:42

2 Answers 2

2

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"])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! My problem was that I did not pay attention to the syntax difference for jsonpath
0

Use the json module

import json

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.