Generate JSON format based on payload array

This is over my head.
I want to format a JSON output based on a array input, but only where some of the parameters is build.

If I have this ex. array ["CAR1","CAR2"]
It will give me this output

    "id": "TEST",
    "time": "2024-06-24 13:56:36",
    "vehicles": [
        {
            "id": "CAR1",
            "car_time": "2024-06-24 13:56:36",
            "test": [
                {
                    "test_code": "40000",
                    "test_time": "2024-06-24 13:56:36"
                }
            ]
        },
        {
            "id": "CAR2",
            "car_time": "2024-06-24 13:56:36",
            "test": [
                {
                    "test_code": "40000",
                    "test_time": "2024-06-24 13:56:36"
                }
            ]
        }
    ]
}

The vehicles part needs to be looped, based on the array, where only the the "id" part will be filled with info from the array.

Hope this makes sence

Here is a "no-code" method:

[{"id":"350b2ac7fd8d0c02","type":"inject","z":"484e8001f595ac21","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1370,"y":100,"wires":[["d13e74230b22ea79"]]},{"id":"d13e74230b22ea79","type":"template","z":"484e8001f595ac21","name":"template","field":"template","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n    \"id\": \"\",\n    \"car_time\": \"2024-06-24 13:56:36\",\n    \"test\": [\n        {\n            \"test_code\": \"40000\",\n            \"test_time\": \"2024-06-24 13:56:36\"\n        }\n    ]\n}","output":"json","x":1530,"y":100,"wires":[["a36c01bcbdac4f42"]]},{"id":"a36c01bcbdac4f42","type":"change","z":"484e8001f595ac21","name":"populate car array","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\"CAR1\", \"CAR2\", \"CAR3\"]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1710,"y":100,"wires":[["4f6ee2101845991c"]]},{"id":"4f6ee2101845991c","type":"split","z":"484e8001f595ac21","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"payload","x":1370,"y":180,"wires":[["31d79af157a7d2ef"]]},{"id":"31d79af157a7d2ef","type":"change","z":"484e8001f595ac21","name":"apply template","rules":[{"t":"set","p":"car","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"template","tot":"msg","dc":true},{"t":"set","p":"payload.id","pt":"msg","to":"car","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1560,"y":180,"wires":[["14bb75521a04bd36"]]},{"id":"14bb75521a04bd36","type":"join","z":"484e8001f595ac21","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1740,"y":180,"wires":[["c0e88d03d870b545"]]},{"id":"7229b6af4a0552b4","type":"debug","z":"484e8001f595ac21","name":"debug 429","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1720,"y":260,"wires":[]},{"id":"c0e88d03d870b545","type":"change","z":"484e8001f595ac21","name":"finalise output","rules":[{"t":"set","p":"temp","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{\"id\":\"TEST\",\"time\":\"2024-06-24 13:56:36\",\"vehicles\":[]}","tot":"json"},{"t":"set","p":"payload.time","pt":"msg","to":"","tot":"date"},{"t":"move","p":"temp","pt":"msg","to":"payload.vehicles","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1420,"y":260,"wires":[["7229b6af4a0552b4"]]}]

It may require some tweaks but hopefully you get enough clues to move forward

I will take a look at that - thanks

Here is another low code way

[{"id":"350b2ac7fd8d0c02","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":4720,"wires":[["a36c01bcbdac4f42"]]},{"id":"a36c01bcbdac4f42","type":"change","z":"d1395164b4eec73e","name":"populate car array","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\"CAR1\", \"CAR2\", \"CAR3\"]","tot":"json"},{"t":"set","p":"timestamp","pt":"msg","to":"2024-06-24 13:56:36","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":4720,"wires":[["d13e74230b22ea79"]]},{"id":"d13e74230b22ea79","type":"template","z":"d1395164b4eec73e","name":"template","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n    \"id\":\"TEST\",\n    \"time\":\"{{timestamp}}\",\n    \"vehicles\":[\n        {}\n        {{#payload}},{\n            \"id\":\"{{.}}\",\n            \"car_time\": \"{{timestamp}}\",\n            \"test\":[\n                {\n                    \"test_code\":\"40000\",\n                    \"test_time\":\"{{timestamp}}\"\n                }\n            ]\n        }{{/payload}}\n    ]\n}","output":"json","x":440,"y":4720,"wires":[["4b06ef952d71c9a4"]]},{"id":"4b06ef952d71c9a4","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"delete","p":"payload.vehicles[0]","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":4720,"wires":[["7229b6af4a0552b4"]]},{"id":"7229b6af4a0552b4","type":"debug","z":"d1395164b4eec73e","name":"debug 429","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":4780,"wires":[]}]

More info on mustache here mustache(5) - Logic-less templates.

1 Like

I like both suggestions, but the last one fits my needs the best.

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

In my example - if you want to have more than 1 value that gets replaced.

If I have this ex. array ["CAR1","CAR2"], and a second array ["CODE1","CODE2"]
It will give me this output

"id": "TEST",
    "time": "2024-06-24 13:56:36",
    "vehicles": [
        {
            "id": "CAR1",
            "car_time": "2024-06-24 13:56:36",
            "test": [
                {
                    "test_code": "CODE1",
                    "test_time": "2024-06-24 13:56:36"
                }
            ]
        },
        {
            "id": "CAR2",
            "car_time": "2024-06-24 13:56:36",
            "test": [
                {
                    "test_code": "CODE2",
                    "test_time": "2024-06-24 13:56:36"
                }
            ]
        }
    ]
}

The vehicles part needs to be looped, based on the array, where "id" and "test_code" part will be filled with info from the arrays.

I can get one value to be build, but can't figure out with 2 value (arrays)

All the data would need to be in the single array. Otherwise you would have to move to a function node and Javascript, or a change node and use JSONata

[
    {
        "car": "CAR1",
        "code": "code1"
    },
    {
        "car": "CAR2",
        "code": "code2"
    },
    {
        "car": "CAR3",
        "code": "code3"
    }
]

then the mustache would be

{
    "id":"TEST",
    "time":"{{timestamp}}",
    "vehicles":[
        {}
        {{#payload}},{
            "id":"{{car}}",
            "car_time": "{{timestamp}}",
            "test":[
                {
                    "test_code":"{{code}}",
                    "test_time":"{{timestamp}}"
                }
            ]
        }{{/payload}}
    ]
}

Example flow

[{"id":"59754b0558eb72a8","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":5720,"wires":[["ea99fa2b1097c829"]]},{"id":"ea99fa2b1097c829","type":"change","z":"d1395164b4eec73e","name":"populate car array","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"car\":\"CAR1\",\"code\":\"code1\"},{\"car\":\"CAR2\",\"code\":\"code2\"},{\"car\":\"CAR3\",\"code\":\"code3\"}]","tot":"json"},{"t":"set","p":"timestamp","pt":"msg","to":"2024-06-24 13:56:36","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":5720,"wires":[["b2cc68a35508d8df"]]},{"id":"b2cc68a35508d8df","type":"template","z":"d1395164b4eec73e","name":"template","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n    \"id\":\"TEST\",\n    \"time\":\"{{timestamp}}\",\n    \"vehicles\":[\n        {}\n        {{#payload}},{\n            \"id\":\"{{car}}\",\n            \"car_time\": \"{{timestamp}}\",\n            \"test\":[\n                {\n                    \"test_code\":\"{{code}}\",\n                    \"test_time\":\"{{timestamp}}\"\n                }\n            ]\n        }{{/payload}}\n    ]\n}\n","output":"json","x":640,"y":5720,"wires":[["3032d31027ee9184"]]},{"id":"3032d31027ee9184","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"delete","p":"payload.vehicles[0]","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":850,"y":5720,"wires":[["d113f78eafbb3a8e"]]},{"id":"d113f78eafbb3a8e","type":"debug","z":"d1395164b4eec73e","name":"debug 429","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":5780,"wires":[]}]
1 Like