Template API returning invalid JSON response (all quotes are single quotes)

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.

I’ve also found instances of “None” without being put in quotes in the output, so we have to do more, we have to use the above tr to convert single quotes into double quotes, and then also a way to replace None with “None” (this is a nontrivial replacement as you wouldn’t want to interfere with it if the substring None shows up in a string somewhere). So far those are the only things I’ve needed to clear up before I could use jq to parse the output from a template api call.