Insert variable inside a json array

Hi, I have this json:

{
    "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?

use a function node...

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

that all depends on what msg.payload contains.

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.

why not?

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

1 Like

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.

Home assistant nodes usually allow JSONata.
If so use

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

Unexpected '$' :frowning:

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

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

[{"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":[]}]

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