# Insert variable inside a json array

**URL:** <https://discourse.nodered.org/t/insert-variable-inside-a-json-array/57643>\
**Category:** General\
**Created:** [2 February 2022 13:18 UTC](https://discourse.nodered.org/t/insert-variable-inside-a-json-array/57643 "2022-02-02T13:18:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablogomez](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pablogomez/32/10534_2.png) [@pablogomez](https://discourse.nodered.org/u/pablogomez)\
**Post date:** [2 February 2022 13:18 UTC](https://discourse.nodered.org/t/insert-variable-inside-a-json-array/57643/1 "2022-02-02T13:18:18Z")

</div>

Hi, I have this json:

```auto
{
    "transition": "5",
    "brightness": "100",
    "hs_color": [
        <HERE>,
        100
    ]
}

```

What I want is to replace `<HERE>` with a number that comes from `msg.payload` and it has to be without quotes of course. How would I do that?

---

<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:** [2 February 2022 13:24 UTC](https://discourse.nodered.org/t/insert-variable-inside-a-json-array/57643/2 "2022-02-02T13:24:11Z")

</div>

> [@pablogomez](#):
>
> replace `<HERE>` with a number that comes from `msg.payload`

#### use a function node...

```auto
msg.payload = {
    "transition": "5",
    "brightness": "100",
    "hs_color": [
        msg.payload,
        100
    ]
}
return msg;

```

> [@pablogomez](#):
>
> and it has to be without quotes of course

that all depends on what `msg.payload` contains.

---

<div class="post-metadata">

**Author:** ![pablogomez](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pablogomez/32/10534_2.png) [@pablogomez](https://discourse.nodered.org/u/pablogomez)\
**Post date:** [2 February 2022 13:29 UTC](https://discourse.nodered.org/t/insert-variable-inside-a-json-array/57643/3 "2022-02-02T13:29:05Z")

</div>

I cannot use a function node, the json is already inside the node that has a data parameter. All I can do is to write json in that box.

---

<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:** [2 February 2022 13:33 UTC](https://discourse.nodered.org/t/insert-variable-inside-a-json-array/57643/4 "2022-02-02T13:33:37Z")

</div>

> [@pablogomez](#):
>
> I cannot use a function node

why not?

> [@pablogomez](#):
>
> All I can do is to write json in that box.

You do realise we have no idea what node you are talking about and no idea that "All I can do is to write json in that box" - unless you show us or tell us?

Share a screenshot and tell us what node you are talking about please...

---

<div class="post-metadata">

**Author:** ![pablogomez](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pablogomez/32/10534_2.png) [@pablogomez](https://discourse.nodered.org/u/pablogomez)\
**Post date:** [2 February 2022 13:57 UTC](https://discourse.nodered.org/t/insert-variable-inside-a-json-array/57643/5 "2022-02-02T13:57:59Z")

</div>

Actually there's an option to pass the complete json. The node I'm talking about is the homeassistant call service. Switch Data to `expression` is the solution + using the function node of course.

---

<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:** [2 February 2022 17:30 UTC](https://discourse.nodered.org/t/insert-variable-inside-a-json-array/57643/6 "2022-02-02T17:30:14Z")

</div>

Home assistant nodes usually allow JSONata.  
If so use

```auto
{
    "transition": "5",
    "brightness": "100",
    "hs_color": [
       $$.payload,
        100
    ]
}

```

---

<div class="post-metadata">

**Author:** ![pablogomez](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pablogomez/32/10534_2.png) [@pablogomez](https://discourse.nodered.org/u/pablogomez)\
**Post date:** [2 February 2022 17:52 UTC](https://discourse.nodered.org/t/insert-variable-inside-a-json-array/57643/7 "2022-02-02T17:52:31Z")

</div>

Unexpected '$' ☹

---

<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:** [2 February 2022 18:06 UTC](https://discourse.nodered.org/t/insert-variable-inside-a-json-array/57643/8 "2022-02-02T18:06:36Z")

</div>

Vague response. you are using json you need to use JSONata. please post your flow and images of home assistant node config.

---

<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:** [2 February 2022 18:10 UTC](https://discourse.nodered.org/t/insert-variable-inside-a-json-array/57643/9 "2022-02-02T18:10:51Z")

</div>

This works as a JSONata expression in a change node, I cannot test with HA.

```auto
[{"id":"2d284e1347441e18","type":"inject","z":"08a01d3d77d9d931","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"111","payloadType":"num","x":250,"y":300,"wires":[["34c6580345abe8b6"]]},{"id":"34c6580345abe8b6","type":"change","z":"08a01d3d77d9d931","name":"","rules":[{"t":"set","p":"js","pt":"msg","to":"{ \"transition\": \"5\", \"brightness\": \"100\", \"hs_color\": [payload, 100] }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":300,"wires":[["24a7a0ac88579d41"]]},{"id":"24a7a0ac88579d41","type":"debug","z":"08a01d3d77d9d931","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":300,"wires":[]}]

```

---

<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:** [3 April 2022 18:11 UTC](https://discourse.nodered.org/t/insert-variable-inside-a-json-array/57643/10 "2022-04-03T18:11:11Z")

</div>

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