How to extract from nested JSON?

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