Rest Sensor : Dealing with json response that is a list of dictionaries (rather than a dictionary)

Hi, I’m trying to capture data from a Restful API which has the format of

[
  { "key1": "value1", "key2": "value2" },
  { "key1": "value3", "key2": "value4" },
  ...
]

What I would ideally like is to capture the data into the attributes of a sensor, such that the attributes come out like this:

Data:
  - key1: value1
    key2: value2
  - key1: value3
    key2: value4
...

Unfortunately the Restful sensor just doesn’t seem to be set up to deal with a list, rather than a dictionary. I can use json_attributes to extract out the first entry of key1, key2 but it doesn’t progress through the json data and extract them all, nor put them into the desired structure.

I’ve tried to accomplish this via a python script, but can’t find how to make a restful api call from within the script (the environment doesn’t support import of requests, nor is it allowed to call_service). I can’t capture the full json object from the restful sensor - and then pass this into the python script to manipulate because the value_template of the sensor is limited to 255 characters.

In searching I’ve not found a simple answer, there appears to be ways to do this via ‘appdaemon’ and I know that various integrations and HACS custom_components accomplish this via more extensive python implementations (so there is definitely a way). However, it feels like there should be something pretty simple to do this - as if the raw json data was ever so slightly different to be a dictionary rather than a raw list it would be easy.

Any help or advice is much appreciated.

Thanks

I suggest removing the blueprints topic on this so it shows up in the configuration area. This is not a blueprints problem, it’s a template problem. One of the template guru’s should be able to guide you in the right direction.

Done - thanks.

In the end I split my rest api response into 4 separate sensors, see config snippet of the value_template for the two associated with ‘key1’ below. The response is saved as a text string in the state of the sensor in the shape of a list. I brought all four of these sensors into a python script, and manipulated the data into the format I wanted for the ultimate sensor attributes.

value_template: "{{ ( value_json | selectattr('identifier', 'eq', 'selected') | map(attribute='key1') | list )[46:70] }}"

value_template: "{{ ( value_json | selectattr('identifier', 'eq', 'selected') | map(attribute='key1') | list )[70:94] }}"