GET request: how to read variables with periods in the name?


#1

I’m performing a GET request to get data from the new Google Nest APIs. I’m successfully getting back my data, but I can’t figure out how to extract the data I need to read.

The API returns a JSON blob that looks like this (parts removed for brevity):

{
  "name": "removed for privacy",
  "type": "sdm.devices.types.THERMOSTAT",
  ...
  "traits": {
    "sdm.devices.traits.Humidity": {
      "ambientHumidityPercent": 56
    },
    ...
    "sdm.devices.traits.ThermostatMode": {
      "mode": "COOL",
      "availableModes": [
        "HEAT",
        "COOL",
        "HEATCOOL",
        "OFF"
      ]
    },
}

I’m trying to read the thermostat mode, which is located at response['traits']['sdm.devices.traits.ThermostatMode']['mode'].

The dots in the one variable name are causing me problems.

I can read things like name with the expression $response.name.

To drill into the parameter I need, I tried $response.traits.sdm.devices.traits.ThermostatMode.mode which did not return anything. I also could not get $response.traits.sdm.devices.traits.ThermostatMode.

I can read $response.traits, which gives me this:

[sdm.devices.traits.Info:[customName:], sdm.devices.traits.Humidity:[ambientHumidityPercent:65], sdm.devices.traits.Connectivity:[status:ONLINE], sdm.devices.traits.Fan:[timerMode:OFF], sdm.devices.traits.ThermostatMode:[mode:COOL, availableModes:[HEAT, COOL, HEATCOOL, OFF]], sdm.devices.traits.ThermostatEco:[availableModes:[OFF, MANUAL_ECO], mode:OFF, heatCelsius:18.92499, coolCelsius:24.59267], sdm.devices.traits.ThermostatHvac:[status:OFF], sdm.devices.traits.Settings:[temperatureScale:FAHRENHEIT], sdm.devices.traits.ThermostatTemperatureSetpoint:[coolCelsius:23.66678], sdm.devices.traits.Temperature:[ambientTemperatureCelsius:23.65999]]

I suppose I could get to the value I need with string parsing, but that feels fragile.

Any suggestions?


Save JSON data with hyphen
Accessing a HTTP response JSON path with a period in the key
#2

I solved this by cheating. I created my own API in a Google Cloud Function that wraps the Nest API and exposes a more minimal version. It solved my problem because I used simple JSON responses and it also made the piston a little simpler because I could do some of the work in my Google Cloud Function.

Would still like to figure out if I could have done this purely in the piston.


#3

Here’s another way of doing it. If the key with the period doesn’t change, you can hardcode the key into a variable.

10/25/2020, 8:04:42 PM +736ms
+18ms	║Full resp: [name:removed for privacy, type:sdm.devices.types.THERMOSTAT, traits:[sdm.devices.traits.Humidity:[ambientHumidityPercent:56], sdm.devices.traits.ThermostatMode:[mode:COOL, availableModes:[HEAT, COOL, HEATCOOL, OFF]]]]
+27ms	║Response: COOL

Similar thread here…