Problem with a json string inside a json

Hello,

I have writting a small flow to play Plex playlist on my TV with HA but I am unable to send this json object:

{
"entity_id": "media_player.plex_plex_for_lg_lg_oled65c9pla",
"media_content_type": "PLAYLIST",
"media_content_id": '{ "playlist_name": "Dance", "shuffle": "1" }'
}

The problem is with the last line. Single quotes does not work (unexpected " error) and I have tried to use scape char \ before the double quotation but, then, the behind application does not work saying that double quotation is required ("Call-service API error. Error Message: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)").

If I make the call-service from HA, there is no problem but I would like to use it in Node-red.

Any help would be very welcomed. Thanks!!

"json object" is a misnomer.

JSON stands for "JavaSctipt Object Notation" It is the STRING version of an object. So it is either an object or a JSON string.


That ↑ is not valid JSON.

The best way to make valid JSON is to generate your data as an object then use the JSON node to convert to JSON.

To make a JS object, you can do this sort of thing...

msg.payload = {
  entity_id: "media_player.plex_plex_for_lg_lg_oled65c9pla",
  media_content_type: "PLAYLIST"
}
msg.payload.media_content_id = {}; 
msg.payload.media_content_id.playlist_name = msg.playlist;
msg.payload.media_content_id.shuffle = 1; 

return msg;

or this ...

msg.payload = {
  "entity_id": "media_player.plex_plex_for_lg_lg_oled65c9pla",
  "media_content_type": "PLAYLIST",
  "media_content_id": { "playlist_name": "Dance", "shuffle": "1" }
}

... then send it through a JSON node then to your HA node.


NOTE: I would ask why you dont just send a JS object - but as I understand it, the HA nodes ONLY accept JSON? (I dont use HA)

1 Like

Hi Steve, thanks, interesting aproach. Sorry I am newby with Node-Red. I have tried both proposed solutions but still not working. It seems like I need to send a JSON string inside other JSON but I do not know if it is possible. The result of the json node is:

{"entity_id":"media_player.plex_plex_for_lg_lg_oled65c9pla","media_content_type":"PLAYLIST","media_content_id":{"playlist_name":"Dance","shuffle":"1"}}

while the error is:

Call-service API error. Error Message: required key not provided @ data['media_content_id']

Calling this service from HA works well. Here an image if it can help:

It seems that the second JSON is passed as an string because it has single quotes but I do not know.

Any other idea or workaround?

Sorry, I don't use HA. You might have to ask on their forum.

Thanks Steve. Finally I get it working but your answer put me on the track. The problem was the formatting of the message, I need to put data: and the media_content_id is a string. Here it was the correct format:

msg.payload = {
    data:{
        "entity_id": "media_player.plex_plex_for_lg_lg_oled65c9pla",
        "media_content_type": "PLAYLIST",
        "media_content_id": '{ "playlist_name": "Dance", "shuffle": "1"}'
    }
}
return msg;

Hope this can be helpful for other people. It can be closed.

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