Converting a JSON String to Object

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:

But then i got the error:

image

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

Welcome to the forum.

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

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?

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

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

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

image

And this is the string beeing published by my PLC:

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

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.

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

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

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

Have you tried trimming the string?
e.g.

[{"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.

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.

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:

1 Like

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