JSON string to object

I am trying to manage some data received from Sigfox backend.
The body format I am using on the backend is like this:

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

as specified by them here. Did try to change it but then it gets refused.

Using the above body format, I receive an object that looks like a Json string

{"{ "device" : "2058EE", "time" : "1654757096", "data" : "5af3107a1d47", "seqNumber" : "134" }":""}

I need help to unpack such output and make a proper object. I did try using a Json node but so far no luck. Any suggestion on how to obtain a proper formatted object is welcomed.
Thanks

Add a change node before your json node to remove the guff
image
image

If the string is valid JSON, you can simply use the JSON node. However, what you've shared is pushing JSON to an unhealthy limit. I think you've done something to the incoming data and managed to wrap it in another object with the value confused with the property name.

So I would work backwards and find out what is doing that wrap. Get rid of it and you will be able to use the JSON node.

I did try with the change node. I am still unable to make an object.
This is what I am getting out of the change node (if I copy the data):

{"{ "device" : "2058EE", "time" : "1654774964", "data" : "5af3107a1d47", "seqNumber" : "155" }":""}

This is what is showing on the debug window.
Schermata 2022-06-09 alle 13.48.15

@TotallyInformation Actually I am not doing anything to the incoming data. I just pass them to the change node. As I said in my previous post, I did try to pass the data directly to the Json node but still no luck.

This is my flow:

 [
    {
        "id": "6e84afa4c1db253e",
        "type": "http in",
        "z": "33240871a29e1c47",
        "name": "Sigfox Receiver",
        "url": "/webhook_test_zXaeGZybPYu6f61Z",
        "method": "post",
        "upload": false,
        "swaggerDoc": "",
        "x": 240,
        "y": 220,
        "wires": [
            [
                "9813a5dbef26495b",
                "886facf86abfa2e0",
                "7a7dc16b3481b7dc"
            ]
        ]
    },
    {
        "id": "9813a5dbef26495b",
        "type": "http response",
        "z": "33240871a29e1c47",
        "name": "Response",
        "statusCode": "201",
        "headers": {},
        "x": 460,
        "y": 160,
        "wires": []
    },
    {
        "id": "d6ab6688f8e9603a",
        "type": "debug",
        "z": "33240871a29e1c47",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 220,
        "wires": []
    },
    {
        "id": "7a7dc16b3481b7dc",
        "type": "change",
        "z": "33240871a29e1c47",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "{\"{",
                "fromt": "str",
                "to": "{",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "\": \"\" }",
                "fromt": "str",
                "to": "}",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 500,
        "y": 220,
        "wires": [
            [
                "d6ab6688f8e9603a"
            ]
        ]
    }
]

I think you must have removed your json node

In that case, the output from your IoT device is wrong. It would be best to fix that if you can.

The problem is this:

{
    "{ "device" : "2058EE", "time" : "1654774964", "data" : "5af3107a1d47", "seqNumber" : "155" }": ""
}

{ "device" : "2058EE", "time" : "1654774964", "data" : "5af3107a1d47", "seqNumber" : "155" } has been used as an object property name - it is actually fairly amazing that anything could process it since it is an invalid property name:

> node
Welcome to Node.js v16.14.0.
Type ".help" for more information.
> const x = {"{ "device" : "2058EE", "time" : "1654774964", "data" : "5af3107a1d47", "seqNumber" : "155" }":""}
const x = {"{ "device" : "2058EE", "time" : "1654774964", "data" : "5af3107a1d47", "seqNumber" : "155" }":""}
               ^^^^^^

Uncaught SyntaxError: Unexpected identifier
>

> const y = `{"{ "device" : "2058EE", "time" : "1654774964", "data" : "5af3107a1d47", "seqNumber" : "155" }":""}`
undefined
> JSON.parse(y)
Uncaught SyntaxError: Unexpected token d in JSON at position 5
>

In a function node, you can use a simple chained set of replaces to make the initial string so that it can be a valid object:

> const y = `{"{ "device" : "2058EE", "time" : "1654774964", "data" : "5af3107a1d47", "seqNumber" : "155" }":""}`
undefined
> JSON.parse(y)
Uncaught SyntaxError: Unexpected token d in JSON at position 5
> const z = y.replace(`{"`, '').replace(`":""}`, '')
undefined
> JSON.parse(z)
{
  device: '2058EE',
  time: '1654774964',
  data: '5af3107a1d47',
  seqNumber: '155'
}
>

I did try that:
..to Java script object
..to Json string
..to Java script object


Note however that this assumes that the original input IS actually a string. If by some very bizarre turn of events it actually is an object on input then you need:

> const w = {}
undefined
> w['{ "device" : "2058EE", "time" : "1654774964", "data" : "5af3107a1d47", "seqNumber" : "155" }'] = ''
''
> w
{
  '{ "device" : "2058EE", "time" : "1654774964", "data" : "5af3107a1d47", "seqNumber" : "155" }': ''
}
> Object.keys(w)[0]
'{ "device" : "2058EE", "time" : "1654774964", "data" : "5af3107a1d47", "seqNumber" : "155" }'
> JSON.parse(Object.keys(w)[0])
{
  device: '2058EE',
  time: '1654774964',
  data: '5af3107a1d47',
  seqNumber: '155'
}
>

Or in your case, maybe something like JSON.parse(Object.keys(msg.payload)[0])

Can you put a debug node after Sigfox receiver and show us what is coming out of it

Also put one after the change node so we can see what it's doing as well

Here it is... driving me mad...

OK
1st off change name on the 3 debug nodes so we know which one is which :slight_smile:
2nd
Put a json node set to do always convert to json string after sigfox receiver and lets see happens then

Here it is.
I don't wont to abuse you but if you like I can direct the Sigfox message directly to you. Eventually PM me.

No need. Use my last post as the basis. The input After Sigfox Receiver is the crazy object.

So you need a change node rather than the json node.

Ok - put the original json node back in
Change change node to this

try again :slight_smile:

Bingo!

Schermata 2022-06-09 alle 14.43.52

Thanks a million to all of you for the help.
Really appreciated.

Or you can add a function node containing:

msg.payload = JSON.parse(Object.keys(msg.payload)[0])

return msg;

:smile_cat:

Thats exactly what I did.
Thanks again and also thanks to cymplecy for guiding

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.