Assemble a JSON packet


#1

In my application, my server with the customer facing application calls into a webhook on Losant and expects back a JSON packet with the conditioned data collected from the device.

Currently I have two JSON encode nodes in serial and then a Function node to assemble the two JSON encodings into into one which is then delivered to the Webhook Reply. Is there a better way to do this then the JS in the Function node assembling the string? It works, just want to know if I’m missing something.


#2

You probably can do what you want without involving any function nodes, depending on what exactly you are building up as your object. If you have two objects on the payload that you want to place on a single final json object, you can do something like the following as a string template combined with the use of the jsonEncode helper for the webhook reply node:

{
  "prop1": {{ jsonEncode path.to.first.obj }},
  "prop2": {{ jsonEncode path.to.second.obj }}
}

and make sure to set the content type header of the reply to application/json.