A few times over the past week I’ve helped some users in the Discord channel for Home Assistant with some jinja magic using structured data. Jinja itself can parse structured data using dot notation or you can use the bracket notation if you’re using variables.

In this example we’ll use the RESTful sensor as an example, but you can use this approach inside of sripts/macros/automations as well.

sensor:
  - platform: rest
    resource: https://launchlibrary.net/1.2.2/launch/next/10
    name: launch window
    # if the current timestamp is in the launch window
    # this sensor will return the UTC timestamp of the launch
    value_template: >-
      {%- for launch in value_json.launches %}
      {% endfor %}

In this example you’ll see the RESTful sensor reach out and grab some structured data from the launch library API. In the value template you can see an example of looping through an array returned on the response. The JSON response is stored in jinja scope as value_json.

{
         "id":1227,
         "name":"Antares 230 | Cygnus CRS OA-8 (S.S. Gene Cernan)",
         "windowstart":"November 11, 2017 12:37:00 UTC",
         "windowend":"November 11, 2017 12:37:00 UTC",
         "net":"November 11, 2017 12:37:00 UTC",
         "wsstamp":1510403820,
         "westamp":1510403820,
         "netstamp":1510403820,
         "isostart":"20171111T123700Z",
         "isoend":"20171111T123700Z",
         "isonet":"20171111T123700Z",
         "status":1,
         "inhold":0,
         "tbdtime":0,
         "vidURLs":[
            "https:\/\/www.youtube.com\/watch?v=wwMDvPCGeE0"
         ],
         "vidURL":null,
         "infoURLs":[
 
         ],
         "infoURL":null,
         "holdreason":null,
         "failreason":null,
         "tbddate":0,
         "probability":-1,
         "hashtag":null,
         "location":{
            "pads":[
               {
                  "id":109,
                  "name":"Launch Area 0 A, Wallops Island, Virginia",
                  "infoURL":"",
                  "wikiURL":"",
                  "mapURL":"http:\/\/maps.google.com\/maps?q=37.8337+N,+75.4881+W",
                  "latitude":37.8337,
                  "longitude":-75.4881,
                  "agencies":[
                     {
                        "id":44,
                        "name":"National Aeronautics and Space Administration",
                        "abbrev":"NASA",
                        "countryCode":"USA",
                        "type":1,
                        "infoURL":"http:\/\/www.nasa.gov",
                        "wikiURL":"http:\/\/en.wikipedia.org\/wiki\/National_Aeronautics_and_Space_Administration",
                        "infoURLs":[
                           "http:\/\/www.nasa.gov"
                        ]
                     }
                  ]
               }
            ],
            "id":19,
            "name":"Wallops Island, Virginia, USA",
            "infoURL":"",
            "wikiURL":"",
            "countryCode":"USA"
         },
         "rocket":{
            "id":117,
            "name":"Antares 230",
            "configuration":"230",
            "familyname":"Antares",
            "agencies":[
               {
                  "id":179,
                  "name":"Orbital ATK",
                  "abbrev":"OA",
                  "countryCode":"USA",
                  "type":3,
                  "infoURL":"https:\/\/www.orbitalatk.com\/",
                  "wikiURL":"https:\/\/en.wikipedia.org\/wiki\/Orbital_ATK",
                  "infoURLs":[
                     "https:\/\/www.orbitalatk.com\/"
                  ]
               }
            ],
            "wikiURL":"https:\/\/en.wikipedia.org\/wiki\/Antares_(rocket)",
            "infoURLs":[
 
            ],
            "imageURL":"https:\/\/s3.amazonaws.com\/launchlibrary\/RocketImages\/placeholder_1920.png",
            "imageSizes":[
               320,
               480,
               640,
               720,
               768,
               800,
               960,
               1024,
               1080,
               1280,
               1440,
               1920
            ]
         },
         "missions":[
            {
               "id":549,
               "name":"Cygnus CRS OA-8 (S.S. Gene Cernan)",
               "description":"This is the ninth planned flight of the Orbital ATK's unmanned resupply spacecraft Cygnus and its eighth flight to the International Space Station under the Commercial Resupply Services contract with NASA.",
               "type":11,
               "typeName":"Resupply"
            }
         ]
      }

In this individual launch you can access any of the direct properties like name simply by using launch.name If you want to get at a property of a child object you can continue using dot notation to get at buried data; rocket agency name as an example would be launch.rocket.agencies[0].name. Since agencies is an array you have to tell it which index to use.

How else could you use this? Well lets say you had a script that needed to use a specific value based on a specific input. Assuming you had data passed into the service call like

data:
  mode: 'COOL'
  fan: 'AUTO'

you could use the following inside of your script

{% set mode_info = {
	"COOL": {
		"FAN": {
			"AUTO": 1,
			"LOW": 2,
			"HIGH": 3
		}
	},
	"HEAT": {
		"FAN": {
			"AUTO": 1,
			"LOW": 2,
			"HIGH": 3
		}
	}
}
%}
{{mode_info[data.mode].FAN.[data.fan]}}

Using structured data can remove a lot of repetitive code and make maintenance and modifications less of a burden. Give it a shot!