How to convert an object object object to object[].object

So I've been beating my head against the wall and need some help. I have this object:

{
    "cameras": {
        "cam1": {
            "label": "Front Door",
            "rotation": "0deg"
        },
        "cam2": {
            "label": "Driveway",
            "rotation": "0deg"
        },
        "cam3": {
            "label": "Garden",
            "rotation": "0deg"
        },
        "cam4": {
            "label": "Shed",
            "rotation": "0deg"
        },
        "cam5": {
            "label": "Backyard",
            "rotation": "0deg"
        }
    }
}

that I want to convert to this object:

{
    "cameras": [
        {
            "name": "cam1",
            "label": "Front Door",
            "rotation": "0deg"
        },
        {
            "name": "cam2",
            "label": "Driveway",
            "rotation": "0deg"
        },
        {
            "name": "cam3",
            "label": "Garden",
            "rotation": "0deg"
        },
        {
            "name": "cam4",
            "label": "Shed",
            "rotation": "0deg"
        },
        {
            "name": "cam5",
            "label": "Backyard",
            "rotation": "0deg"
        }
    ]
}

and I can't figure it out. Here is a flow with an inject node with the objects in it, an empty function node and a debug node anyone can play with.

[{"id":"23605468e7d390f7","type":"inject","z":"96bf579fe7b64086","name":"","props":[{"p":"payload"},{"p":"want","v":"{\"cameras\":[{\"name\":\"cam1\",\"label\":\"Front Door\",\"rotation\":\"0deg\"},{\"name\":\"cam2\",\"label\":\"Driveway\",\"rotation\":\"0deg\"},{\"name\":\"cam3\",\"label\":\"Garden\",\"rotation\":\"0deg\"},{\"name\":\"cam4\",\"label\":\"Shed\",\"rotation\":\"0deg\"},{\"name\":\"cam5\",\"label\":\"Backyard\",\"rotation\":\"0deg\"}]}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"cameras\":{\"cam1\":{\"label\":\"Front Door\",\"rotation\":\"0deg\"},\"cam2\":{\"label\":\"Driveway\",\"rotation\":\"0deg\"},\"cam3\":{\"label\":\"Garden\",\"rotation\":\"0deg\"},\"cam4\":{\"label\":\"Shed\",\"rotation\":\"0deg\"},\"cam5\":{\"label\":\"Backyard\",\"rotation\":\"0deg\"}}}","payloadType":"json","x":270,"y":200,"wires":[["507955f615e523b1"]]},{"id":"e1ea330e059e2fbe","type":"debug","z":"96bf579fe7b64086","name":"debug 33","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":740,"y":200,"wires":[]},{"id":"507955f615e523b1","type":"function","z":"96bf579fe7b64086","name":"function 2","func":"\n// ???????\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":200,"wires":[["e1ea330e059e2fbe"]]}]

Thanks in advance for anyone how can offer a solution!

Using JSONata
set msg. payload.cameras
to

$keys($$.payload.cameras).$merge(
   [
       {"name": $},
       $lookup($$.payload.cameras, $)
   ]
)
2 Likes

See I knew I should have asked earlier!

Thanks @E1cid, you are my hero of the day!

3 Likes

Javascript

let output = {cameras: []};
let cam_keys =  Object.keys(msg.payload.cameras);
cam_keys.forEach(str => {
    output.cameras.push(Object.assign({name: str}, msg.payload.cameras[str]))
})
msg.payload = output;
return msg;

As silicon pixie haters are going to hate.

1 Like

Or in javascript (which will be more run-time efficient, at least at the moment)

let camerasArray = []
const cameras = msg.payload.cameras
Object.keys(cameras).forEach(camera => {
    camerasArray.push({name: camera, label: cameras[camera].label, rotation: cameras[camera].rotation})
})
msg.payload = {cameras: camerasArray}
return msg;
1 Like

A veritable cornucopia of solutions!

And one more :slight_smile:

msg.payload = Object.entries(msg.payload.cameras).map(v => { return { name: v[0], ...v[1] } })
return msg

REPL...

4 Likes

And to round off a low code option

[{"id":"23605468e7d390f7","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"want","v":"{\"cameras\":[{\"name\":\"cam1\",\"label\":\"Front Door\",\"rotation\":\"0deg\"},{\"name\":\"cam2\",\"label\":\"Driveway\",\"rotation\":\"0deg\"},{\"name\":\"cam3\",\"label\":\"Garden\",\"rotation\":\"0deg\"},{\"name\":\"cam4\",\"label\":\"Shed\",\"rotation\":\"0deg\"},{\"name\":\"cam5\",\"label\":\"Backyard\",\"rotation\":\"0deg\"}]}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"cameras\":{\"cam1\":{\"label\":\"Front Door\",\"rotation\":\"0deg\"},\"cam2\":{\"label\":\"Driveway\",\"rotation\":\"0deg\"},\"cam3\":{\"label\":\"Garden\",\"rotation\":\"0deg\"},\"cam4\":{\"label\":\"Shed\",\"rotation\":\"0deg\"},\"cam5\":{\"label\":\"Backyard\",\"rotation\":\"0deg\"}}}","payloadType":"json","x":270,"y":4800,"wires":[["d5de6a35661403e0"]]},{"id":"d5de6a35661403e0","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.cameras","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":4800,"wires":[["9f9090ce51286740"]]},{"id":"9f9090ce51286740","type":"split","z":"da8a6ef0b3c9a5c8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":590,"y":4800,"wires":[["dae0f60808e1cc65"]]},{"id":"dae0f60808e1cc65","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload.name","pt":"msg","to":"parts.key","tot":"msg"},{"t":"set","p":"parts.type","pt":"msg","to":"array","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":4800,"wires":[["98c0ae11620d1838"]]},{"id":"98c0ae11620d1838","type":"join","z":"da8a6ef0b3c9a5c8","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":470,"y":4860,"wires":[["a4c32e28a0777158"]]},{"id":"a4c32e28a0777158","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.cameras","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":4860,"wires":[["e1ea330e059e2fbe"]]},{"id":"e1ea330e059e2fbe","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 33","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":940,"y":4860,"wires":[]}]

[edit]
It is always a pleasure to help, specially when the OP provides data examples already loaded in an inject node, and provides output examples.

2 Likes

Indeed. I've been mulling over an idea for some time - for the debug panel: "copy as inject" where you can then paste into the editor or forum :slight_smile:

4 Likes

And if you could just automatically add the triple backticks…

3 Likes

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