# JSON string to object

**URL:** <https://discourse.nodered.org/t/json-string-to-object/63659>\
**Category:** General\
**Created:** [9 June 2022 06:52 UTC](https://discourse.nodered.org/t/json-string-to-object/63659 "2022-06-09T06:52:03Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [9 June 2022 06:52 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/1 "2022-06-09T06:52:03Z")

</div>

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](https://support.sigfox.com/docs/data-advanced). 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

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [9 June 2022 08:36 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/2 "2022-06-09T08:36:28Z")

</div>

Add a change node before your json node to remove the guff  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6d90b3c038f1171ef0226395a9554f0a85305cce.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c137c8c7746a61cd623987b224cd9a1f61126ef6.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 June 2022 11:21 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/3 "2022-06-09T11:21:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [9 June 2022 11:53 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/4 "2022-06-09T11:53:07Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/b/fbd172907e221a4e966d43f8cd56894b47c14bce.png)

@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:

```auto
 [
    {
        "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"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [9 June 2022 12:05 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/5 "2022-06-09T12:05:41Z")

</div>

> [@cymplecy](#):
>
> Add a change node before your json node

I think you must have removed your json node

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/a/0aa1fb8ef041db519b0531a92e2538e14de85010.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 June 2022 12:10 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/6 "2022-06-09T12:10:08Z")

</div>

> [@FeliceM](#):
>
> @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.

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:

```auto
{
    "{ "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:

```auto
> 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:

```auto
> 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'
}
>

```

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [9 June 2022 12:11 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/7 "2022-06-09T12:11:13Z")

</div>

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

 ![Schermata 2022-06-09 alle 14.08.24](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/61a8bdf9c34e4c94203cbbb5b3bee7f098309761.png)  
 ![Schermata 2022-06-09 alle 14.08.19](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/54e85ddabc767208c7faf12137cc40827ee3779a.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 June 2022 12:15 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/8 "2022-06-09T12:15:08Z")

</div>

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:

```auto
> 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])`

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [9 June 2022 12:16 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/9 "2022-06-09T12:16:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [9 June 2022 12:23 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/10 "2022-06-09T12:23:30Z")

</div>

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

 ![Schermata 2022-06-09 alle 14.19.36](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/471f495f0aae8b8947a3ab9b339dc6f7b16b5dd8.jpeg)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [9 June 2022 12:29 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/11 "2022-06-09T12:29:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [9 June 2022 12:37 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/12 "2022-06-09T12:37:21Z")

</div>

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.

 ![Schermata 2022-06-09 alle 14.35.41](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/4/a4bd367bcc1811840e1e74086437bdee4e429a9d.jpeg)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 June 2022 12:40 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/13 "2022-06-09T12:40:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [9 June 2022 12:41 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/14 "2022-06-09T12:41:48Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3dc595b82d6b560387b21aab7db9e402a88a1ee3.png)

try again 🙂

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [9 June 2022 12:44 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/15 "2022-06-09T12:44:39Z")

</div>

Bingo!

![Schermata 2022-06-09 alle 14.43.52](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/1838fb52b0577088b99ebc56af572b4b7505063d.png)

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 June 2022 12:46 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/16 "2022-06-09T12:46:09Z")

</div>

Or you can add a function node containing:

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

return msg;

```

😸

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [9 June 2022 12:48 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/17 "2022-06-09T12:48:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 June 2022 12:48 UTC](https://discourse.nodered.org/t/json-string-to-object/63659/18 "2022-06-23T12:48:46Z")

</div>

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