# Insert into json object based on condition

**URL:** <https://discourse.nodered.org/t/insert-into-json-object-based-on-condition/67123>\
**Category:** General\
**Created:** [30 August 2022 11:21 UTC](https://discourse.nodered.org/t/insert-into-json-object-based-on-condition/67123 "2022-08-30T11:21:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [30 August 2022 11:21 UTC](https://discourse.nodered.org/t/insert-into-json-object-based-on-condition/67123/1 "2022-08-30T11:21:00Z")

</div>

I am creating through function and change nodes a quite large JSON object.  
Based on a condition (value of a variable) I need to insert or not some key/values in the "middle" of json.  
At the moment, I am using a switch node with two outputs and I create my large json with ou without some part of the json. I have two nodes where the entire JSON is identical except for the "conditional" part. However maintaining both nodes in sync with this large json is quite a challenge.  
Is there a way to "insert" in the JSON object some parts based on a condition?

Hopefully, through this example is will be simpler to understand...

 ![Screenshot 2022-08-30 at 13.10.47](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/6/56cd5bf92ec9ebc0ec452ad8fcd20c439e8c9e5c.png)

```auto
[
    {
        "id": "86d67ab3e9db1030",
        "type": "inject",
        "z": "2e301f7e.715e6",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 320,
        "y": 780,
        "wires": [
            [
                "8c388ad63d7d4843"
            ]
        ]
    },
    {
        "id": "8c388ad63d7d4843",
        "type": "switch",
        "z": "2e301f7e.715e6",
        "name": "",
        "property": "payload % 2",
        "propertyType": "jsonata",
        "rules": [
            {
                "t": "eq",
                "v": "0 ",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 490,
        "y": 780,
        "wires": [
            [
                "4080f32e181f862f"
            ],
            [
                "e55acd023fceb752"
            ]
        ]
    },
    {
        "id": "4080f32e181f862f",
        "type": "change",
        "z": "2e301f7e.715e6",
        "name": "Even",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{ \"one\": 1, \"two\": { \"insert\": 1, \"object\": 1 }, \"three\" : 3 }",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 690,
        "y": 720,
        "wires": [
            [
                "323c3cb09266e08c"
            ]
        ]
    },
    {
        "id": "e55acd023fceb752",
        "type": "change",
        "z": "2e301f7e.715e6",
        "name": "Odd",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{ \"one\": 1, \"three\" : 3 }",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 690,
        "y": 840,
        "wires": [
            [
                "323c3cb09266e08c"
            ]
        ]
    },
    {
        "id": "323c3cb09266e08c",
        "type": "debug",
        "z": "2e301f7e.715e6",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 890,
        "y": 780,
        "wires": []
    }
]

```

Even and Odd change node differs only with the inclusion of a bit of JSON or not....

Of course here, with this simplified version of the object, it is quite doable like that.  
If the object is larger and more complex, two _almost_ identical nodes to maintain in sync are a challenge...

Any idea to do this in a cleaner way?  
Thanks.

---

<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:** [30 August 2022 13:48 UTC](https://discourse.nodered.org/t/insert-into-json-object-based-on-condition/67123/2 "2022-08-30T13:48:01Z")

</div>

You can merge two objects in Jsonata, using a ternary conditional you can select the objects to merge.  
eg.

```auto
[{"id":"86d67ab3e9db1030","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":270,"y":1860,"wires":[["4080f32e181f862f"]]},{"id":"4080f32e181f862f","type":"change","z":"30af2d3e.d94ea2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$merge([{\t \"one\": 1,\t \"three\" : 3 \t},\t ($$.payload % 2 = 0 ?\t {\t \"two\": { \t \"insert\": 1, \t \"object\": 1 \t }\t } : \t {}\t )\t])\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":1860,"wires":[["323c3cb09266e08c"]]},{"id":"a4ecc5df.f941a","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":270,"y":1920,"wires":[["4080f32e181f862f"]]},{"id":"323c3cb09266e08c","type":"debug","z":"30af2d3e.d94ea2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":1920,"wires":[]}]

```

Expression

```auto
$merge([{
   "one": 1,
   "three" : 3 
},
    ($$.payload % 2 = 0 ?
        {
        "two": {     
            "insert": 1,     
            "object": 1   
            }
        } : 
          {}
    )
])

```

---

<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:** [13 September 2022 13:48 UTC](https://discourse.nodered.org/t/insert-into-json-object-based-on-condition/67123/3 "2022-09-13T13:48:06Z")

</div>

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