Select single value from payload

I am not a coder but I challenge myself. I am attempting to select only a specific value from a payload.
this is my payload:

{
  "applicationId": "61ac43d686ea5501fd13a215",
  "applicationName": "Sigfox callback",
  "data": {
    "body": {
      "{   \"device\" : \"2058EE\",   \"time\" : \"1638684238\",   \"data\" : \"5af3107a1d47\",   \"seqNumber\" : \"48\" }": ""
    },
    "headers": {
      "accept-charset": "UTF-8;q=0.9,*;q=0.7",
      "accept-encoding": "gzip,deflate",
      "accept-language": "fr",
      "content-length": "150",
      "content-type": "application/x-www-form-urlencoded",
      "user-agent": "SIGFOX",
      "x-forwarded-for": "185.110.98.5",
      "x-forwarded-proto": "https",
      "x-real-ip": "185.110.98.5"
    },
    "method": "post",
    "path": "/",
    "query": {
    },
    "replyId": "61ac441b09119443483690b9.ncLGvXeJe5koM4xW7skVg.uacZ7klp0W6vpw_EOygbgW"
  },
  "flowId": "61ac47e18c09256f92188669",
  "flowName": "Sigfox1",
  "flowVersion": "develop",
  "globals": {
  },
  "relayId": "000000000000000000000000",
  "relayType": "public",
  "time": "2021-12-05T06:05:10.491Z",
  "triggerId": "61ac441b09119443483690b9",
  "triggerType": "webhook"
}

I am trying to select and output only the value "data"in the body.
I have tried with a function on this way:

payload = payload.data.body.data

but I am not coming right. I can output the entire string “body”: {
“{ “device” : “2058EE”, “time” : “1638762361”, “data” : “5af3107a1d47”, “seqNumber” : “61” }”: “”
}

payload = payload.data.body
return payload;

But I am battling to get only the string data in body.
How can I extract only the value in data?
Thanks for helping

Hey @Felice_Massaro,

Welcome to the Losant forums, we’re happy to have you!

Your payload path looks correct, but the issue is that your body value in your payload is not JSON, it is just a string that looks like JSON. You should include a JSON: Decode Node before the Function Node to turn that string into a JSON Object.

Also, a small note, it looks like data.body may not be valid JSON with the format you’ve sent. Are you able to change the incoming data?

Thank you,
Heath

@Heath Thank you for your warm welcome.

The body of the callback I have in the Sigfox backend was autogenerated when I defined the keys in the device:

{
  "device" : "{device}",
  "time" : "{time}",
  "data" : "{data}",
  "seqNumber" : "{seqNumber}"
}

About the decode node. I see that a path to the payload and to the destination load is needed. Where do I find those paths?

Yes I can change the body of the callback but I am not sure if it will comply with Sigfox. The above one is working.
Thank you for helping

@Felice_Massaro

I see that a path to the payload and to the destination load is needed. Where do I find those paths?

The destination path is anywhere on the payload where you want the output to be placed. So, for example, if you wanted the decoded JSON value to be placed back at the place where it came in, your node configuration would look like this:

image

But, our best practice is to put new/mutated values in a new path, like working. That would look like this:

image

When placing an output of a node at a new payload path that doesn’t exist yet, Losant automatically makes that new path for you.

Let me know if this works for you, or if you need any more information on anything!

Thank you,
Heath

I did try but no luck! I am not sure about what I am doing wrong. I did use the path as per your second example data.body >>> working .body
But I am now getting " InvalidJsonError Source did not contain valid JSON: data.body"

CORRECTION
The error message from the decode node is: Source did not contain valid JSON: data.body

@Felice_Massaro,

As I mentioned in my first reply, what you’re sending along via SigFox is not valid JSON. What’s happening is that you are receiving a JSON String as an Object Key where the value is an empty string ("").

If you are unable to edit the incoming body of the request, you will need to use an Object Node with the Keys operation. Then you can use the JSON: Decode node to decode that JSON String that is created.

I’ve attached a sample workflow that shows this process using the exact body of your payload in the Virtual Button. How to import a workflow.

decode-develop.flow (2.2 KB)

Let me know if this works for you!

Heath

1 Like

Sorry to abuse you. How would you change the incoming body? I am willing to try on that side.
As I mentioned in my first post, I am not a coder so during the learning process … I need some help.
Thanks

@Felice_Massaro,

From the looks of it, I assumed you were using a SigFox device and are able to choose what SigFox sends when it invokes the Webhook trigger as this blog post (which is a bit old) suggests.

Is this the correct assumption? How is the webhook trigger being triggered?

Heath

Bingo!. It now works perfectly.
I have used your flow replacing the button with my webhook and added a function after the Json decode node as following:

payload = payload.working.decoded.data;

finally I get the value I was looking for.
Thanks a million for your help and extra help.

1 Like