As we all know, JSON must use double quotes for strings. I have found that the response from the template API is JSON as usual but always uses single quotes, and cannot parse via jq
, whereas the JSON response from other non-template endpoints use double quotes.
I am using the docker image ghcr.io/home-assistant/home-assistant stable 7cf0fd8cf42d
on a Raspberry Pi 4 model B. I have done minimal configuration, having completed onboarding and established a long term auth token.
Here is my minimal reproduction:
❯ cat home_assistant_raspi_auth.header
Authorization: Bearer <TOKEN REDACTED>
Content-Type: application/json
❯ which hacurl_data
hacurl_data () {
curl -s -H @home_assistant_raspi_auth.header raspberrypi.local:8123/api/$1 -d "$2"
}
❯ hacurl_data template '{"template": "{{ areas() }}"}'
['living_room', 'kitchen', 'bedroom']
I have in some light testing using a less trivial jinja template confirmed that usually piping through tr "'" '"'
to convert all single quotes into double quotes before jq
works, however, this is bound to cause problems once strings containing single quotes and double quotes appear inside strings in the responses, because the ways to escape these characters differ between single quoted strings and double quoted strings.