Best way(s) to render json, for Experience view

For starters,
Appreciate the handy toolbox item - payload tester in debug panel:

So wondering how it’s best to process / render data in regards to json;
taking an example workflow here,

I gather:
*MQTT node puts data in a string
*json decode node separates that into objects
*Endpoint reply returns that back, curiously is re-encoding as json (string?) required?

With that, as you see in the toolbox above, doesn’t like to parse with template helpers. Noting replyData is an array of objects, like [0: {“Name”: “Smith”, “Date”:“5/17/23”}]
(But there are no commas separating any items in the debug panel, even beyond the replyData)

And to be sure for 3rd step above, removing jsonEncode doesn’t work as the debug panel shows ‘not able to render json’ (workflow times out) -
image

One thing is, you can’t have a space between delimiter and helper (ex, “{{” and “#each”), but that’s not the issue above…

Side note, I’m getting it to work ‘cleanly’ in Experience view (albeit some hard-coding), but it won’t work in payload template tester, funny enough.

Hi @emcdee!

First, do you have an actual device that is reporting data to the broker? If so, do you mind sharing an example of how the initial payload is formatted? The JSON template in your screenshot isn’t formatted properly (see the error in the Output box), so if that’s the payload you’re passing through, that explains the error in the debug log.

Note that Virtual Button Triggers are very helpful for this phase of testing and troubleshooting - it allows you to simulate a custom payload. I recommend using one of these with a payload matching the format in which you expect MQTT messages to be received. Along with the Debug Node, this should help you get to the bottom of your issue. Let us know how it goes!

Sure, I generally use Virtual buttons, actually prior payload seemed fine but somehow I found a way to ‘break’ that too when adding a button for demo here. And forgive as I’m not seeing what’s not adding up -

So the pieces here:
*Virtual button w/ demo json payload
*And original demo workflow

Both example and real payload appear to have working json = good, but erroring out.
And going back to the first question, when/where do you need json decode vs encode?
(So decode puts say an MQTT string onto payload as objects; and encode packages objects into json string for say http; else dunno why template engine and debug panel throwing errors).

Yes, you’re correct - the JSON: Decode Node takes a JSON string and saves it as an object on the payload. The JSON: Encode Node does the inverse - it takes an object and saves it as a JSON string on the payload.

Based on your screenshots, it looks like your Virtual Button is passing an object to a JSON: Decode Node, rather than a JSON string - hence the error message. If you passed that same payload to a JSON: Encode Node, it would convert it to a JSON string.

For example, given the following payload:

{
  "orderNumber": 1,
  "userName": "Thomas Edison",
  "userEmail": "thomas.edison@illumination.xyz",
  "contents": [
    {
      "productID": 42,
      "productName": "Lightbulb",
      "quantity": 1
    },
    {
      "productID": 96,
      "productName": "Dimmer Switch",
      "quantity": 3
    }
  ],
  "orderCompleted": true
}

The JSON: Encode Node would convert that to a JSON string:

"{\"orderNumber\":1,\"userName\":\"Thomas Edison\",\"userEmail\":\"thomas.edison@illumination.xyz\",\"contents\":[{\"productID\":42,\"productName\":\"Lightbulb\",\"quantity\":1},{\"productID\":96,\"productName\":\"Dimmer Switch\",\"quantity\":3}],\"orderCompleted\":true}"

If the above JSON string was set as the Source String Path in a JSON: Encode Node, it would be converted back to an object.

I’ve attached a simple workflow using these examples:
json-encodedecode-develop.flow (4.3 KB)

Hopefully this helps. Let us know if you have any other questions!

1 Like

Ok thanks for the thoughtful response, yes for this I should have I put the virtual button one rung down on ‘Endpoint reply’

Now going back to the original post, are you saying this applies there too?

Yes, the error from your original post was presumably due to the Source String Path in the JSON: Decode Node being an object (or an invalid JSON string), rather than a properly formatted JSON string.

The error message also indicates that you had the error handling for the JSON: Decode Node set to “Error the Workflow”, which stops the workflow from progressing any further if an error is encountered. Therefore, the Endpoint: Reply Node in your workflow wasn’t being executed.