Integration mustache parse error

electron
Tags: #<Tag:0x00007fe21ee96e40>

#1

I’m trying to send a set of error codes to an integration endpoint. In the interest of being conservative about data volume at scale I was attempting to only send error codes which exist rather than a binary state for every error code. I have tried the following format in a number of shapes, but I consistently get this parse error. Any ideas what I’m overlooking?

{
    "e": "{{{PARTICLE_EVENT_NAME}}}",
    "d": {
        "{{#b}}b:{{{b}}},{{/b}}"    #<<Parse error line 6: Expected ':' instead of '"'
        "{{#c}}c:{{{c}}},{{/c}}"
        "{{#d}}d:{{{d}}},{{/d}}"
        "{{#f}}f:{{{f}}},{{/f}}"
        "{{#i}}i:{{{i}}},{{/i}}"
        "{{#p}}p:{{{p}}},{{/p}}"
        "0":"{{{0}}}"
    },
    "_ts": "{{{PARTICLE_PUBLISHED_AT}}}"
}

#2

You are missing the inner double quotes.

 "{{#b}}b:{{{b}}},{{/b}}" 

should probably rather look like

 {{#b}}"b":"{{{b}}}",{{/b}} 

#3

That’s how I did it on my first attempt. Just tried it again and I’m getting the same error.


#4

Try this

{
    "e": "{{{e}}}",
    "d": {
     {{#if b}}"b":"{{{b}}}",{{/if}}
     {{#if c}}"c":"{{{c}}}",{{/if}}
     {{#if d}}"d":"{{{d}}}",{{/if}}
     {{#if f}}"f":"{{{f}}}",{{/if}}
     {{#if i}}"i":"{{{i}}}",{{/if}}
     {{#if p}}"p":"{{{p}}}",{{/if}}
     "0":"{{{0}}}"
    }
    "_ts": "{{{PARTICLE_PUBLISHED_AT}}}"
}

#5

Error now says ‘Bad String’: ‘parse error on line 3’

I copy and pasted exactly from your code block.


#6

What are you using to validate?
I have used this online validator


#7

Particle integrations on the Console. Trying to save this as a custom template. That text box is throwing the errors.


#8

I see.
Particle Integrations obviously does not use the latest mustache parsing nor does it support handlebars conditions :wink:

According to the online validator above Mustache v1.2.0 would accept this as valid template

 {
    "e": "{{{e}}}",
    "d": {
     {{#b}}"b":"{{{b}}}",{{/b}}
     {{#c}}"c":"{{{c}}}",{{/c}}
     {{#d}}"d":"{{{d}}}",{{/d}}
     {{#f}}"f":"{{{f}}}",{{/f}}
     {{#i}}"i":"{{{i}}}",{{/i}}
     {{#p}}"p":"{{{p}}}",{{/p}}
    }
    "_ts": "{{{PARTICLE_PUBLISHED_AT}}}"
}

@rickkas7 may know how to achieve what you’re after.
Would be good to know which version of handlebars/mustache are supported.


#9

You latest version is really close, but I think you still need the dummy item at the end of the “d” object. Otherwise, if there is a p element, there will be a trailing comma, which will cause an error.


#10

Should look like this (at least this is what I was testing.) Trailing comma shouldn’t be possible.

{
    "e": "{{{e}}}",
    "d": {
     {{#if b}}"b":"{{{b}}}",{{/if}}
     {{#if c}}"c":"{{{c}}}",{{/if}}
     {{#if d}}"d":"{{{d}}}",{{/if}}
     {{#if f}}"f":"{{{f}}}",{{/if}}
     {{#if i}}"i":"{{{i}}}",{{/if}}
     {{#if p}}"p":"{{{p}}}",{{/if}}
     "0":"{{{0}}}"
    }
    "_ts": "{{{PARTICLE_PUBLISHED_AT}}}"
}

This has the “Bad string” error.


#11

Yup, my bad - I did have it in previous versions (as taken from the original).
But even this doesn’t get through (although it should IMO)


vs.

The {{#if x}} syntax seems to be only valid for handlebars not mustache.


#12

ScruffR is correct. The {{#if x}} syntax is not supported but you can make your case work using {{#x}} which is how I do conditionals in the Google maps device locator.


#13

@rickkas7, but why does console integrations complain as shown in my screenshot above at line 4 (and when removing the condition from that line the red X jumps to the next line)?

Even if the string is acceptable for the backend, the editor should not flag that.
Also the editor is rather annoying with the misalignment between editing position and the carret.


#14

It’s because when you use the Custom Template in the integration, it’s setting the json key, not the body key, and it must be valid JSON without mustache parsing, which line 4 is not.

When you use the CLI to set the body key, mustache parsing is applied. Interestingly, if you then edit it with the GUI you get a different presentation that does not parse it as JSON and it’s just one text block with no syntax checking.


#15

So it may be advisable to first check the validity with a third party tool and then copy/paste the valid handlebars expression into the body element like here

Hmm, I do sense some room for improvement in the UX :upside_down_face:


#16

@rickkas7

This is a valid workaround for simple webhooks. The Azure integration only allows JSON and will not allow me to save with the syntax it finds to be invalid. Any options there?