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

**URL:** <https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613>\
**Category:** General\
**Created:** [2 February 2023 16:31 UTC](https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613 "2023-02-02T16:31:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 February 2023 16:31 UTC](https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613/1 "2023-02-02T16:31:25Z")

</div>

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

```auto
{
    "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:

```auto
{
    "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.

```auto
[{"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!

---

<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:** [2 February 2023 16:38 UTC](https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613/2 "2023-02-02T16:38:51Z")

</div>

Using JSONata  
set `msg.` `payload.cameras`  
to

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

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 February 2023 16:42 UTC](https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613/3 "2023-02-02T16:42:48Z")

</div>

See I knew I should have asked earlier!

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

---

<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:** [2 February 2023 16:59 UTC](https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613/4 "2023-02-02T16:59:55Z")

</div>

Javascript

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 February 2023 17:01 UTC](https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613/5 "2023-02-02T17:01:04Z")

</div>

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

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 February 2023 17:22 UTC](https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613/6 "2023-02-02T17:22:11Z")

</div>

A veritable cornucopia of solutions!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 February 2023 17:29 UTC](https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613/7 "2023-02-02T17:29:29Z")

</div>

And one more 🙂

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

```

REPL...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/2/121f3b37c4dea8af9b2e032c7cdd83acde233542.png)

---

<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:** [2 February 2023 17:38 UTC](https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613/8 "2023-02-02T17:38:24Z")

</div>

And to round off a low code option

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 February 2023 19:42 UTC](https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613/9 "2023-02-02T19:42:40Z")

</div>

> [@E1cid](#):
>
> It is always a pleasure to help, specially when the OP provides data examples already loaded in an inject node, and provides output examples

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 🙂

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [2 February 2023 23:05 UTC](https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613/10 "2023-02-02T23:05:28Z")

</div>

> [@Steve-Mcl](#):
>
> "copy as inject"

And if you could just automatically add the triple backticks…

---

<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:** [16 February 2023 23:05 UTC](https://discourse.nodered.org/t/how-to-convert-an-object-object-object-to-object-object/74613/11 "2023-02-16T23:05:54Z")

</div>

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