Json String to object help please

Hi all, I've got a camera that sends this out on MQTT ,

{ “type”:“MOTION_A”,
“trigger”:“ON”, “memo”:“car:80%”, “json”:"[{“api”:“objects”,“found”:{“success”:true,“predictions”:[{“confidence”:0.79541016,“label”:“car”,“y_min”:629,“x_min”:1323,“y_max”:774,“x_max”:1575}],“duration”:0}}]", “path”:"@4226890"}

I'm struggling to convert it into an object so I can use the info for automations.

ive tried to convert to json but it fails, I've tried a split(",") but I either loose info or I just get errors. trigger, memo, and path are just single entry's but the json section increased with the number of items it finds.

really appreciate some help on this. Thanks

There is a setting in the mqtt out node to output a parse object. check the dropdown in the output section.

Doesn't look like a valid JSON string!

hi E1cid ,
thank you I didn't know that was there. it works for the majory of the code except when I add the Json part.

it fail after that.Failed to parse JSON string


You need to escape all the quotes inside the outer two quotes

“json”:"[{\“api\”:\“objects\”,\“found\”: ....etc...0}}]"

or remove the outer two


I can add variables to the MQTT from the camera but I can't edit the variables data directly. ive renames it to data from json to be a bit more clear. this is how it looks in MQTT explorer. ive taken the other parts out to make it easier to see.

{“data": [{"api":"objects","found":{"success":true,"predictions":[{"confidence":0.67333984,"label":"person","y_min":1494,"x_min":1696,"y_max":1761,"x_max":1994}],"duration":0}},{"api":"faces","found":{"success":true,"predictions":,"duration":0}}]}

this still says there is a fault with it.

Can you post the output of the MQTT node connected to a debug node please.

{“data": [{"api":"objects","found":{"success":true,"predictions":[{"confidence":0.42358398,"label":"dining table","y_min":440,"x_min":1290,"y_max":700,"x_max":1772},{"confidence":0.86376953,"label":"person","y_min":1329,"x_min":1034,"y_max":1770,"x_max":1284}],"duration":0}},{"api":"faces","found":{"success":true,"predictions":[{"confidence":0,"userid":"unknown","y_min":1356,"x_min":1055,"y_max":1423,"x_max":1108}],"duration":0}}]}

that's the one part that it fails on. ive removed the other data lines from the camera for clarity

The first quote in “data" is prettified, not the same as the second quote.

1 Like

Yep, well spotted. If you change it to a normal quote the string validates!

thank you very much it works. I've put the whole string together and get all the information. Really appreciate you help thank you .

1 Like

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