Mustache template for JSON keys containing spaces and .'s

Hi Everyone–

I am trying to get stock info with a webhook. My query returns the following JSON:

{
  "Global Quote": {
    "01. symbol": "^DJI",
    "02. open": "25261.4700",
    "03. high": "25441.4300",
    "04. low": "25261.4700",
    "05. price": "25429.6100",
    "06. volume": "141020593",
    "07. latest trading day": "2018-11-05",
    "08. previous close": "25270.8300",
    "09. change": "158.7793",
    "10. change percent": "0.6283%"
  }
}

I cannot for the life of me figure out how to write the response template so I get only one item. What’s screwing me up is the key format. If I remove the .'s from the keys, {{#Global Quote}}{{01 symbol}}{{/Global Quote}} will get the ^DJI value. If I keep the .'s, no dice. I’ve tried formatting like {{['01. symbol']}} and pretty much every variation on that theme. Does anyone have any suggestions? Thank you!

I'm not sure whether this was addressed alreaedy, but I remember several discussions about dots breaking webhook JSON validation
https://community.particle.io/search?q=json%20dot

This may be worth following up with @Dave as he saw it as a bug on Particle's end back in this post

I’m almost certain this is impossible. Mustache template selectors don’t support escaping, and spaces seem to cause problems, and dot is completely not going to work because it’s the path separator for nested objects.

Assuming this is the result from the webhook you probably can pass it all the way back to the device as a string and do a brute-force parsing on the device.

(The other issue, where you can’t send up a key with a . in it is true because the request data is stored in a Javascript object, which can’t have a key with a . in it. Actually, Javascript object keys can only be a Unicode letter character, _ or $. But that shouldn’t matter for a response as long as you send the whole response back as a string.)

@rickkas7, this is what I thought, but after feeding the above JSON into multiple online validators, they all came back with verdict VALID

https://jsonlint.com/
https://jsonformatter.curiousconcept.com
https://codebeautify.org/jsonvalidator
https://www.jsonschemavalidator.net/

It’s valid JSON to have almost any character in the key, but it’s not valid Javascript.

There is a valid argument that the data should be able to be all valid JSON, not all valid Javascript objects, but that’s not the way it’s implemented for request data, because it’s stored and manipulated as Javascript objects.

2 Likes