# Problem with a json string inside a json

**URL:** <https://discourse.nodered.org/t/problem-with-a-json-string-inside-a-json/38339>\
**Category:** General\
**Created:** [31 December 2020 19:04 UTC](https://discourse.nodered.org/t/problem-with-a-json-string-inside-a-json/38339 "2020-12-31T19:04:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![villah](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@villah](https://discourse.nodered.org/u/villah)\
**Post date:** [31 December 2020 19:04 UTC](https://discourse.nodered.org/t/problem-with-a-json-string-inside-a-json/38339/1 "2020-12-31T19:04:18Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 December 2020 19:13 UTC](https://discourse.nodered.org/t/problem-with-a-json-string-inside-a-json/38339/2 "2020-12-31T19:13:47Z")

</div>

> [@villah](#):
>
> json object

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

  

> [@villah](#):
>
> {  
> "entity\_id": "media\_player.plex\_plex\_for\_lg\_lg\_oled65c9pla",  
> "media\_content\_type": "PLAYLIST",  
> "media\_content\_id": '{ "playlist\_name": "Dance", "shuffle": "1" }'  
> }

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...

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

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

---

<div class="post-metadata">

**Author:** ![villah](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@villah](https://discourse.nodered.org/u/villah)\
**Post date:** [1 January 2021 20:30 UTC](https://discourse.nodered.org/t/problem-with-a-json-string-inside-a-json/38339/3 "2021-01-01T20:30:06Z")

</div>

> [@Steve-Mcl](#):
>
> `return msg;`

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:

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

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?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 January 2021 21:04 UTC](https://discourse.nodered.org/t/problem-with-a-json-string-inside-a-json/38339/4 "2021-01-01T21:04:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![villah](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@villah](https://discourse.nodered.org/u/villah)\
**Post date:** [1 January 2021 23:08 UTC](https://discourse.nodered.org/t/problem-with-a-json-string-inside-a-json/38339/5 "2021-01-01T23:08:01Z")

</div>

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:

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

---

<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:** [15 January 2021 23:08 UTC](https://discourse.nodered.org/t/problem-with-a-json-string-inside-a-json/38339/6 "2021-01-15T23:08:07Z")

</div>

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