How to extract from nested JSON?

I’m certain I tried that and failed! Must’ve had a typo somewhere…

Fair point but, “don’t follow me I’m lost” and “@petro is always right” are pretty good maxims :stuck_out_tongue_winking_eye:

Not at all. It is partly a learning experience but I also thought I could use it to get the address of a device tracker without needing to use any of the custom components out there.

I stopped using google a few days ago as Life360 seems to be more than good enough on its own but Life360 doesn’t in my experience populate the address field very often (ever?) and the Places custom component is potentially brilliant but I have had too many issues with it to persevere (and I think the developer of it has stopped working on it).

I am of course now expecting you to tell me of a much simpler way I could do this that I haven’t even thought of :wink:

Lol I wish I was always right!

I thought I understood @petro outline and made various trials on the starting example with success. However, when I tried to apply it to my json file mentioned below, I got stuck again with no glue what I’m doing wrong, e.g. value_template: “{{ value_json.retrieve_reply.seqnr }}” or value_template: “{{ value_json[‘retrieve_reply’][‘control’][‘ch_status’] }}” gives an Unknown result. Any help would be appreciated.

{
“retrieve_reply”: {
“seqnr”: 0,
“status”: {
“device_id”: “6808-1401-3107_15-07-002-091”,
“date_time”: 638614357
},
“report”: {
“report_time”: 638614357,
“burning_hours”: 9445.14,
“details”: {
“boiler_temp”: 22.0,
“boiler_return_temp”: 22.0
}
},
“control”: {
“ch_status”: 161,
“ch_control_mode”: 0,
“ch_mode”: 2
},
“schedules”: {
“ch_schedule”: {
“base_temp”: 19.0,
“entries”: [
[]
]
},
“dhw_schedule”: {
“base_temp”: 60.0,
“entries”: [
[]
]
}
},
“configuration”: {
“temp_unit”: 0,
“time_format”: 1,
“time_zone”: 0
},
“acc_status”: 2
}
}

Your attempts are correct. So your source data must be setup wrong or something else is wrong. Show your whole config for what you are doing.

Thanks for yr quick turnaround which is appreciated and at least with one encouraging note. Before I avail further info I would appreciate your advise where I could locate the commands for the forum postings to ensure the yaml code is formatted properly and how to insert the lengthy json file with scroll bars.

For the location of the source.json file and sensor file, the configuration.yaml file contains:

homeassistant:
  whitelist_external_dirs:
    - /config

sensor: !include_dir_merge_list sensors

In the sensor sub-directory I have the json.yaml file containing:

- platform: file
  name: Test-JSON variable
  file_path: source.json
  value_template: "{{ value_json.retrieve_reply.seqnr }}"

The above setup works perfectly well using the jason file contained in the first post of this topic. Hence, for me there are only two issues remaining which could attribute to my failure to extract data:

  1. the used value_template is wrongly formatted which you have already responded to is not the case;
  2. the source.json file is corrupt, which according to online json validation does not appear to be the case.

your path seems wrong. I’ve never used the file platform to get something in the root config directory so I can’t be certain. But that’s where I’d start looking.

h’mm, surprising since I use exactly the same setup for the file quoted at the beginning of this topic and that seems to be working fine. It’s all in the same directory. Only the json source file changed. Is there a possibility you check my jason file your end with the used value templates? If so, pls. let me know how I can insert a file with copy clipboard capability.

Hi Petro, I sussed it out and it’s all working fine now. It appeared that my json file shows up with two blank lines appended at the end which screwed it up. All data I can get across based on your Feb 19 dictionary and list guidelines. Thanks for standing by and availing yourselves to sort this out.

1 Like

Hi, I would like to jump into this topic :slight_smile:
I have following mqtt output from distant raspberry monitor ISP-RPi-mqtt-daemon:

{
  "info": {
    "timestamp": "2021-01-28T18:21:44+00:00",
    "rpi_model": "RPi 3 Model B r1.2",
    "ifaces": "e,w,b",
    "host_name": "raspberrypi",
    "fqdn": "raspberrypi.home",
    "ux_release": "buster",
    "ux_version": "5.4.79-v7+",
    "up_time": " 9:53",
    "last_update": "2021-01-28T07:09:19+00:00",
    "fs_total_gb": 16,
    "fs_free_prcnt": 18,
    "networking": {
      "eth0": {
        "IP": "192.168.1.156",
        "mac": "b8:27:eb:88:xa:84"
      },
      "wlan0": {
        "mac": "b8:27:eb:dd:f9:yz"
      }
    },
    "drives": {
      "root": {
        "size_gb": 16,
        "used_prcnt": 18,
        "device": "/dev/root",
        "mount_pt": "/"
      },
      "mnt-nfs_shares": {
        "size_gb": 2048,
        "used_prcnt": 38,
        "device-nfs": {
          "ip": "192.168.1.99",
          "dvc": "/volume1/Shinobi"
        },
        "mount_pt": "/mnt/nfs_shares"
      }
    },
    "memory": {
      "size_mb": "924.844",
      "free_mb": "618.168"
    },
    "cpu": {
      "hardware": "BCM2835",
      "model": "ARMv7 Processor rev 4 (v7l)",
      "number_cores": 4,
      "bogo_mips": "307.20",
      "serial": "00000000f688ac84"
    },
    "throttle": [
      "bad response [VCHI initialization failed] from vcgencmd"
    ],
    "temperature_c": 46.2,
    "temp_gpu_c": -1,
    "temp_cpu_c": 46.2,
    "reporter": "ISP-RPi-mqtt-daemon v1.5.4",
    "report_interval": 5
  },
  "friendly_name": "NVR Shinobi"
}

I need to extract IP, mnt-nfs_shares (size_gb, used_prcnt) and temperature_c

Could anybody advice pls? @petro I guess :slight_smile:

Go to https://jsonpathfinder.com

  1. Paste your JSON string into the left hand pane.
  2. It will appear as a JSON object in the right hand pane.
  3. In the right hand pane, click on the key you want (for example, IP).
  4. The correct JSON path will appear at the top of the right hand pane.

In the following screenshot, the JSON path is x.info.networking.eth0.IP

To use it in Home Assistant, replace x with value_json to produce:

value_json.info.networking.eth0.IP
2 Likes

@123 Thanks a lot. :+1: I missed such a tool somehow. It saves lot of time.

Hi all,
I’m quite new to Home-Assistant and the last two days I’m trying to figure out how to extract some data from a nested JSON file which somehow is not working for me.
The given answers helped me already a lot to get an idea how it could be done, but I think I still miss something as I can’t make it run.

The data of the JSON file is data about petrol stations and fuel prices. I would like to extract the following info:

  • Name (of the petrol station)
  • Label (diesel/gasoline)
  • Price (amount)
    This would be the formatted JSON output:
[
  {
    "id": 1354695,
    "name": "avanti - Rum Dörfer Straße 26b",
    "location": {
      "address": "Doerfer Strasse 26b",
      "postalCode": "6064",
      "city": "Rum",
      "latitude": 47.2873,
      "longitude": 11.46305
    },
    "contact": {
      "telephone": "800202055",
      "fax": "800",
      "website": "www.omv.com"
    },
    "openingHours": [
      {
        "day": "MO",
        "label": "Montag",
        "order": 1,
        "from": "07:00",
        "to": "20:00"
      },
      {
        "day": "DI",
        "label": "Dienstag",
        "order": 2,
        "from": "07:00",
        "to": "20:00"
      },
      {
        "day": "MI",
        "label": "Mittwoch",
        "order": 3,
        "from": "07:00",
        "to": "20:00"
      },
      {
        "day": "DO",
        "label": "Donnerstag",
        "order": 4,
        "from": "07:00",
        "to": "20:00"
      },
      {
        "day": "FR",
        "label": "Freitag",
        "order": 5,
        "from": "07:00",
        "to": "20:00"
      },
      {
        "day": "SA",
        "label": "Samstag",
        "order": 6,
        "from": "07:00",
        "to": "20:00"
      },
      {
        "day": "SO",
        "label": "Sonntag",
        "order": 7,
        "from": "07:00",
        "to": "20:00"
      },
      {
        "day": "FE",
        "label": "Feiertag",
        "order": 8,
        "from": "07:00",
        "to": "20:00"
      }
    ],
    "offerInformation": {
      "service": false,
      "selfService": true,
      "unattended": true
    },
    "paymentMethods": {
      "cash": false,
      "debitCard": true,
      "creditCard": true,
      "others": "OMV STATIONSKARTE, DKV, MASTERCARD, MAESTRO, ROUTEX, DINERS, VISA"
    },
    "paymentArrangements": {
      "cooperative": false,
      "clubCard": false
    },
    "position": 1,
    "open": true,
    "distance": 3.3402903474358094,
    "prices": [
      {
        "fuelType": "DIE",
        "amount": 1.113,
        "label": "Diesel"
      }
    ]
  },
  {
    "id": 665616,
    "name": "avanti - Rum Siemensstraße 1",
    "location": {
      "address": "Siemensstrasse 1",
      "postalCode": "6063",
      "city": "Rum",
      "latitude": 47.27753,
      "longitude": 11.45756
    },
    "contact": {
      "telephone": "800202055",
      "fax": "800",
      "website": "www.omv.com"
    },
    "openingHours": [
      {
        "day": "MO",
        "label": "Montag",
        "order": 1,
        "from": "00:00",
        "to": "24:00"
      },
      {
        "day": "DI",
        "label": "Dienstag",
        "order": 2,
        "from": "00:00",
        "to": "24:00"
      },
      {
        "day": "MI",
        "label": "Mittwoch",
        "order": 3,
        "from": "00:00",
        "to": "24:00"
      },
      {
        "day": "DO",
        "label": "Donnerstag",
        "order": 4,
        "from": "00:00",
        "to": "24:00"
      },
      {
        "day": "FR",
        "label": "Freitag",
        "order": 5,
        "from": "00:00",
        "to": "24:00"
      },
      {
        "day": "SA",
        "label": "Samstag",
        "order": 6,
        "from": "00:00",
        "to": "24:00"
      },
      {
        "day": "SO",
        "label": "Sonntag",
        "order": 7,
        "from": "00:00",
        "to": "24:00"
      },
      {
        "day": "FE",
        "label": "Feiertag",
        "order": 8,
        "from": "00:00",
        "to": "24:00"
      }
    ],
    "offerInformation": {
      "service": false,
      "selfService": true,
      "unattended": true
    },
    "paymentMethods": {
      "cash": false,
      "debitCard": true,
      "creditCard": true,
      "others": "OMV STATIONSKARTE, DKV, MASTERCARD, MAESTRO, ROUTEX, DINERS, VISA"
    },
    "paymentArrangements": {
      "cooperative": false,
      "clubCard": false
    },
    "position": 2,
    "open": true,
    "distance": 3.7335185653760985,
    "prices": [
      {
        "fuelType": "DIE",
        "amount": 1.113,
        "label": "Diesel"
      }
    ]
  },
  {
    "id": 36007,
    "name": "Disk",
    "location": {
      "address": "Innsbrucker Straße 28",
      "postalCode": "6060",
      "city": "Hall",
      "latitude": 47.2783007,
      "longitude": 11.4950006
    },
    "contact": {
      "telephone": "43522357242"
    },
    "openingHours": [
      {
        "day": "MO",
        "label": "Montag",
        "order": 1,
        "from": "06:00",
        "to": "20:00"
      },
      {
        "day": "DI",
        "label": "Dienstag",
        "order": 2,
        "from": "06:00",
        "to": "20:00"
      },
      {
        "day": "MI",
        "label": "Mittwoch",
        "order": 3,
        "from": "06:00",
        "to": "20:00"
      },
      {
        "day": "DO",
        "label": "Donnerstag",
        "order": 4,
        "from": "06:00",
        "to": "20:00"
      },
      {
        "day": "FR",
        "label": "Freitag",
        "order": 5,
        "from": "06:00",
        "to": "20:00"
      },
      {
        "day": "SA",
        "label": "Samstag",
        "order": 6,
        "from": "06:00",
        "to": "20:00"
      },
      {
        "day": "SO",
        "label": "Sonntag",
        "order": 7,
        "from": "08:00",
        "to": "20:00"
      },
      {
        "day": "FE",
        "label": "Feiertag",
        "order": 8,
        "from": "08:00",
        "to": "20:00"
      }
    ],
    "offerInformation": {
      "service": false,
      "selfService": true,
      "unattended": false
    },
    "paymentMethods": {
      "cash": true,
      "debitCard": true,
      "creditCard": true,
      "others": "MOL Card, DKV, UTA"
    },
    "paymentArrangements": {
      "cooperative": false,
      "clubCard": false
    },
    "position": 3,
    "open": true,
    "distance": 0.9625345521235231,
    "prices": [
      {
        "fuelType": "DIE",
        "amount": 1.119,
        "label": "Diesel"
      }
    
    ]
  }
]

To extract the data of my interest would be:
Name of the petrol station:

value_template: '{{ value_jason.[0].name }}'

Label:

value_template: '{{ value_jason.[0].prices[0].label}}'

Price:

value_template: '{{ value_jason.[0].prices[0].amount}}'

In my configuration.yaml I added the following:

sensor:
  - platform: rest
    resource: https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=47.281929&longitude=11.506570&fuelType=DIE&includeClosed=false
    scan_interval: 15
    name: Name
    value_template: '{{ value_jason.[0].name }}'

If I restart HomeAssistant, nothing shows up in the “Developer-Tools”.

Would be highly appreciated if someone could point me in the direction what I’m missing or miss-configured.

Thank you all in advance!

value_json

Hi petro,
thank you for your fast reply. I can’t believe that I have overseen this typo :thinking:
I corrected the typo now to “value_json”, validated the configuration and restarted the Home-Assistant service.
Still I can’t find any sensor for the name. I really don’t know what I’m missing out, as the template from the documentation which shows my IP address works without any problem.

Change to:

value_template: '{{ value_json[0].name }}'
value_template: '{{ value_json[0].name }}'

For future reference, refer to my previous post showing how to use JSONpathfinder to determine the correct path to a desired value.

It clearly shows the use of value_json as opposed to value_jason.

Hi All,

I’m always struggling when I want use JSON in sensors.
What I want is create a sensor with state: result.name
But only that name that == 1inch for example
And some attributes:

buy
sell
last
change24h
{
	"apistatus": 1,
	"error": false,
	"msg": "API online",
	"all": {
		"volume24h": "14357818.00"
	},
	"result": [{
		"name": "0x",
		"ticker": "ZRX",
		"buy": "1.31943",
		"sell": "1.26262",
		"last": "1.35109",
		"change24h": "5.73",
		"change72h": "-6.10",
		"change168h": "2.61",
		"volume24h": "7209.4528",
		"logo": "https:\/\/www.bitladon.com\/img\/currency\/ZRX.png",
		"eurovolume24h": "9512.37",
		"graph": "https:\/\/www.bitladon.com\/img\/graphs\/ZRXblue.png?v=1613640360"
	}, {
		"name": "1inch",
		"ticker": "1INCH",
		"buy": "4.25513",
		"sell": "4.06775",
		"last": "4.25400",
		"change24h": "6.45",
		"change72h": "0.61",
		"change168h": "-2.38",
		"volume24h": "9989.5216",
		"logo": "https:\/\/www.bitladon.com\/img\/currency\/1INCH.png",
		"eurovolume24h": "42506.71",
		"graph": "https:\/\/www.bitladon.com\/img\/graphs\/1INCHblue.png?v=1613640360"
	}, {
		"name": "Aave",
		"ticker": "AAVE",
		"buy": "393.863",
		"sell": "376.716",
		"last": "395.050",
		"change24h": "7.52",
		"change72h": "2.51",
		"change168h": "-9.81",
		"volume24h": "163.4485",
		"logo": "https:\/\/www.bitladon.com\/img\/currency\/AAVE.png",
		"eurovolume24h": "64376.32",
		"graph": "https:\/\/www.bitladon.com\/img\/graphs\/AAVEblue.png?v=1613640360"
	}, {
		"name": "AdEx",
		"ticker": "ADX",
		"buy": "0.59580",
		"sell": "0.57015",
		"last": "0.64296",
		"change24h": "5.88",
		"change72h": "13.71",
		"change168h": "10.62",
		"volume24h": "1069.3654",
		"logo": "https:\/\/www.bitladon.com\/img\/currency\/ADX.png",
		"eurovolume24h": "637.13",
		"graph": "https:\/\/www.bitladon.com\/img\/graphs\/ADXblue.png?v=1613640360"
	}]
}

Can someone assist me with this?

value_template: ‘{{ value_json.result[0].name }}’ would get “0x”

‘[1].name’ would get “1inch”, ‘[2].name’ would get “Aave”, etc.

Edit: For the attributes you’d have to use json_attributes, but I have similar looking JSON to yours (I’d be able to set “result” and everything within it as an attribute, but not individual items within “result”) and was unable to get attributes to work on mine, even with json_path; someone else will have to help you with that part.

When i try this in developer tool to get value from :slight_smile:

{{ value_json[0].result[1].buy }}

output is 0.59580

Thats correct.
Now to see how to add this line as json_attribute

maybe something like this:

  json_attributes:
    - {{ value_json[0].result[1].buy }}
    - {{ value_json[0].result[1].sell }}
    - {{ value_json[0].result[1].change24h }}
{% set value_json = 

[{
	"apistatus": 1,
	"error": false,
	"msg": "API online",
	"all": {
		"volume24h": "14357818.00"
	},
	"result": [{
		"name": "0x",
		"ticker": "ZRX",
		"buy": "1.31943",
		"sell": "1.26262",
		"last": "1.35109",
		"change24h": "5.73",
		"change72h": "-6.10",
		"change168h": "2.61",
		"volume24h": "7209.4528",
		"logo": "https:\/\/www.bitladon.com\/img\/currency\/ZRX.png",
		"eurovolume24h": "9512.37",
		"graph": "https:\/\/www.bitladon.com\/img\/graphs\/ZRXblue.png?v=1613640360"
	}, {
		"name": "AdEx",
		"ticker": "ADX",
		"buy": "0.59580",
		"sell": "0.57015",
		"last": "0.64296",
		"change24h": "5.88",
		"change72h": "13.71",
		"change168h": "10.62",
		"volume24h": "1069.3654",
		"logo": "https:\/\/www.bitladon.com\/img\/currency\/ADX.png",
		"eurovolume24h": "637.13",
		"graph": "https:\/\/www.bitladon.com\/img\/graphs\/ADXblue.png?v=1613640360"
	}]
}
]
%}