Insert into json object based on condition

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

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

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

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

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

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