(solved) Extract a nested array value in JSON

Hi - hoping someone can help stop me pulling my hair out…

I have a REST sensor that returns the following JSON:

{ 
  "@context" : "http://environment.data.gov.uk/flood-monitoring/meta/context.jsonld" ,
  "meta" : { 
    "publisher" : "Environment Agency" ,
    "licence" : "http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/" ,
    "documentation" : "http://environment.data.gov.uk/flood-monitoring/doc/reference" ,
    "version" : "0.9" ,
    "comment" : "Status: Beta service" ,
    "hasFormat" : [ "http://environment.data.gov.uk/flood-monitoring/id/stations/2077/readings.csv?latest", "http://environment.data.gov.uk/flood-monitoring/id/stations/2077/readings.rdf?latest", "http://environment.data.gov.uk/flood-monitoring/id/stations/2077/readings.ttl?latest", "http://environment.data.gov.uk/flood-monitoring/id/stations/2077/readings.html?latest" ]
  },
  "items" : [ { 
    "@id" : "http://environment.data.gov.uk/flood-monitoring/data/readings/2077-level-stage-i-15_min-mASD/2020-02-23T11-00-00Z" ,
    "dateTime" : "2020-02-23T11:00:00Z" ,
    "measure" : "http://environment.data.gov.uk/flood-monitoring/id/measures/2077-level-stage-i-15_min-mASD" ,
    "value" : 3.518
  }
   ]
}

I’m trying to extract the value from within the nested items array.
I’ve tried various templates such as:

{{value_json.items.value}}
{{value_json.[items].value}}
{{value_json.items[0].value}}
{{value_json.items.0.value}}

all unsuccessfully. Does anyone have any ideas?

I believe the word items is a reserved word and so its usage becomes ambiguous when you use dot-notation.

Change it to bracket-notation (which clarifies the meaning of items) and it should work:

{{ value_json["items"][0].value }}

alternately, this (completely in bracket-notation):

{{ value_json["items"][0]["value"] }}

For more information, see this post:


Screenshot from Template Editor confirming it works:

1 Like

Perfect! That works exactly as expected. Thanks

1 Like

Glad to hear it!

For the benefit of other users, please mark my post with the Solution tag. Only you, the author of this topic, can do that.

It will automatically place a checkmark next to the topic’s title, thereby indicating to other users that this topic has an accepted Solution. It also places a link under your first post that leads to the Solution. All of this helps other people who may have the same issue and are looking for answers.

Out of interest is this to monitor river levels in the UK? Can you share your sensor etc? It would be good to monitor the river level in York where I live.

Hi all - you might be interested in this

Especially @andye2801 - i’m monitoring the same river level!

Screenshot 2020-02-26 at 08.37.48

This adds a config flow so you can just “Add Integration”, choose “UK Environment Agency - Flood Gauges”, then choose a monitoring station from a dropdown. Then all the gauges for that station appear in your instance.

I’ve added a template binary sensor as well because my road floods at 4.2m, and have notifications set to go off as it approaches this. Bit overkill :stuck_out_tongue:

The HA reviewers are really busy, but i’ve got a bunch of other PR’s in this week so i’m hopefully it will go in too. Hoping it’s not too niche!

1 Like

Wow, nice work @Jc2k. That’s precisely the type of integration I was looking at developing.

Great work, do we know how this integration is going for being implemented?

It took quite a while to get a review, and in that time a bunch of new functionality was added to HA which I have now been asked to implement/use in order to pass review. So now i need to semi-rewrite it. And i’ve not got much free time to do that. It’s still on my list though.

Ah fair enough! I have been playing around with HA a bit more recently so may have a go at manually extracting the river level value. I have made a very simple card for the bin collections where I live by extracting the values from a JSON response so the river level should be similar I guess.
image

Is there a best practice to get multiple values from the same API? I know what I have done is not efficient and can’t find the best way to do it but for the bin collection I have 3 times the following to grab the values required. Should I be requesting the JSON response and reference that in my sensors?

- platform: rest
    resource: https://doitonline.york.gov.uk/BinsApi/EXOR/getWasteCollectionDatabyUprn?uprn=xxxxxxxxxxx
    name: Grey Bin
    value_template: '{{value_json.0.NextCollection | regex_findall_index("\d\d\d\d\d\d\d\d\d\d") | int | timestamp_custom("%d.%m.%Y") }}'

Depending on the data’s structure, you can use json_attributes to store multiple values as attributes of the sensor.

Alternatively, you can store the received value in the sensor’s state and then use multiple Template Sensors to extract and display each desired parameter. However, this technique has a limitation; an entity’s state cannot hold more than 255 characters.

FWIW the river level stuff was merged today so should be in… 0.115.0 i think.