# Converting a JSON String to Object

**URL:** https://discourse.nodered.org/t/converting-a-json-string-to-object/74438
**Category:** General
**Tags:** mqtt
**Created:** [30 January 2023 17:39 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438 "2023-01-30T17:39:05Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![daolio](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@daolio](https://discourse.nodered.org/u/daolio)
#### Post date: [30 January 2023 17:39 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438/1 "2023-01-30T17:39:05Z")

</div>

So i have a Formated JSON String which is published by a PLC

{"data":{"Temperatura":27,"Umidade":78}}

And i need to convert this string into a Java Script object,

I've tried the JSON parser node as in the image bellow:

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

But then i got the error:

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

It may be a simple promblem but i'm really strugling with this.

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [30 January 2023 17:46 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438/2 "2023-01-30T17:46:50Z")

</div>

Welcome to the forum.

Try setting the MQTT node to return a parsed JSON object and remove the JSON node.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [30 January 2023 18:10 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438/3 "2023-01-30T18:10:23Z")

</div>

Strange string length is 41, but error is at position 45. That suggest to me you may have some junk/zero length chars at end of string.  
Can you show us a copy value from debug, and maybe a screenshot.

How is the string created and sent to the broker?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [30 January 2023 18:50 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438/4 "2023-01-30T18:50:47Z")

</div>

> [@E1cid](#):
>
> Strange string length is 41, but error is at position 45.

That is the length of the error string.  
@daolio add a debug node to the mqtt node and show us that.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [30 January 2023 18:54 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438/5 "2023-01-30T18:54:07Z")

</div>

Doh!  
[edit] Still the string length is only 40 in the supplied example.

---

<div class="post-metadata">

### Author: ![daolio](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@daolio](https://discourse.nodered.org/u/daolio)
#### Post date: [30 January 2023 19:42 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438/6 "2023-01-30T19:42:37Z")

</div>

This is the image from the debug node getting the MQTT Data:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/280c3bb5f033498927546067c67d60c636c3e234.png)

And this is the string beeing published by my PLC:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/59e0f560ab42c3bd61ddf0aed49da03d2933fb86.png)

---

<div class="post-metadata">

### Author: ![daolio](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@daolio](https://discourse.nodered.org/u/daolio)
#### Post date: [30 January 2023 19:44 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438/7 "2023-01-30T19:44:08Z")

</div>

I've tried already but it returns me this error:

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

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [30 January 2023 19:53 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438/8 "2023-01-30T19:53:49Z")

</div>

Again data string is 81 but i only count 45.  
Can you copy value using debug copy value icon/button (appears when you hover mouse over property name). Post here using \</\> button.

Something is being added can you expand the orange boxes in image 2.

---

<div class="post-metadata">

### Author: ![daolio](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@daolio](https://discourse.nodered.org/u/daolio)
#### Post date: [30 January 2023 19:57 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438/9 "2023-01-30T19:57:37Z")

</div>

Value : {"data":{"Temperatura":22964,"Umidade":23476}}

And the value on the orange boxes are the same that you see in this table:

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

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [30 January 2023 20:19 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438/10 "2023-01-30T20:19:33Z")

</div>

You did not use the \</\> button when posting as i asked.

Have you tried trimming the string?  
e.g.

```auto
[{"id":"d556411065ad6217","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"Temperatura\":22964,\"Umidade\":23476}}","payloadType":"str","x":150,"y":3980,"wires":[["c6b3048da8f259f9","37ac30dcdea7deb2"]]},{"id":"c6b3048da8f259f9","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 226","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":3820,"wires":[]},{"id":"37ac30dcdea7deb2","type":"function","z":"da8a6ef0b3c9a5c8","name":"function 20","func":"msg.payload = msg.payload.trim();\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":3980,"wires":[["390eff7019b8b578"]]},{"id":"390eff7019b8b578","type":"json","z":"da8a6ef0b3c9a5c8","name":"","property":"payload","action":"","pretty":false,"x":530,"y":3980,"wires":[["c6b3048da8f259f9"]]}]

```

If that works then you definitely have junk chars at end.

---

<div class="post-metadata">

### Author: ![daolio](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@daolio](https://discourse.nodered.org/u/daolio)
#### Post date: [30 January 2023 20:23 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438/11 "2023-01-30T20:23:24Z")

</div>

I got it!

As some of you said the lenght of my string was incorrect, the reason is that i was using the wrong function on my PLC go get the size of the string.

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

Now that i have the right function, it returns me the size of 45 bytes, wich is correct cause my string has 45 character. Now if i use the JSON parser it creates a JSON Object from my string:

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

---

<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: [13 February 2023 20:23 UTC](https://discourse.nodered.org/t/converting-a-json-string-to-object/74438/12 "2023-02-13T20:23:30Z")

</div>

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