# Selecting an object from within an object

**URL:** <https://discourse.nodered.org/t/selecting-an-object-from-within-an-object/56205>\
**Category:** General\
**Created:** [6 January 2022 15:37 UTC](https://discourse.nodered.org/t/selecting-an-object-from-within-an-object/56205 "2022-01-06T15:37:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [6 January 2022 15:37 UTC](https://discourse.nodered.org/t/selecting-an-object-from-within-an-object/56205/1 "2022-01-06T15:37:09Z")

</div>

In a flow, I have a msg payload object similar to;

```auto
{
    "a2c61946a883218c": {
        "online": true,
        "status": "on",
        "name": "sensor1"
    },
    "a2c61946a883443c": {
        "online": true,
        "status": "off",
        "name": "sensor2"
    },
    "a2c61946a765218c": {
        "online": false,
        "status": "on",
        "name": "sensor3"
    }
}

```

I would like to retrieve the object that contains a specific value in the key `name`.  
So for example, if I wanted `sensor2`, it would produce a payload output;

```auto
{
     "online": true,
     "status": "off",
     "name": "sensor2"
}

```

The node producing the flow may not include the value (sensor2) at all, or may produce an object with a different format, in which cases I would expect no match, and therefore no output. Also the position of "name": "sensor2" may not be in the same order within the objects.

I've been tying by using `Object.entries` but failing so far...

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [6 January 2022 15:47 UTC](https://discourse.nodered.org/t/selecting-an-object-from-within-an-object/56205/2 "2022-01-06T15:47:43Z")

</div>

You could use something like:

```auto
let obj = msg.payload
let filtered = Object.entries(obj).filter(x => x[1].name === "sensor1")
msg.filtered = filtered
return msg;

```

output:

```auto
{ "filtered": [
    [
      "a2c61946a883218c",
      {
        "online": true,
        "status": "on",
        "name": "sensor1"
      }
    ]}

```

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [6 January 2022 15:51 UTC](https://discourse.nodered.org/t/selecting-an-object-from-within-an-object/56205/3 "2022-01-06T15:51:34Z")

</div>

If you are just wanting to replace the payload with a single object having that specific name, you can use the JSONata expression `payload.*[name="sensor2"]` in a `change` node.

To use another field (e.g. 'topic') as the lookup key, try `payload.*[name=$$.topic]` -- the $$. prefix in this case is necessary in order to reset the context to look for the topic field in the root of the msg object.

---

<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:** [6 January 2022 16:02 UTC](https://discourse.nodered.org/t/selecting-an-object-from-within-an-object/56205/4 "2022-01-06T16:02:55Z")

</div>

find() will find the first match  
e.g

```auto
let filtered = Object.values(msg.payload).find(value => value.name === "sensor1");
msg.payload = filtered;
return msg;

```

Output

```auto
{
"online":true,
"status":"on",
"name":"sensor1"
}

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [6 January 2022 16:25 UTC](https://discourse.nodered.org/t/selecting-an-object-from-within-an-object/56205/5 "2022-01-06T16:25:48Z")

</div>

@E1cid Thanks, this produces the output format exactly how I want it.  
The only slight problem is that in the cases where;

> [@Paul-Reed](#):
>
> The node producing the flow may not include the value (sensor2) at all, or may produce an object with a different format, in which cases I would expect no match, and therefore no output.

...the code produces a 'undefined' payload, so is it a case of adding something like;

```auto
if (filtered != undefined) {
msg.payload = filtered;
node.send(msg);
}

```

---

<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:** [6 January 2022 16:27 UTC](https://discourse.nodered.org/t/selecting-an-object-from-within-an-object/56205/6 "2022-01-06T16:27:25Z")

</div>

try this

```auto
let filtered = Object.values(msg.payload).find(value => value.name === "sensor1");
msg.payload = filtered;
if(msg.payload === undefined) msg = null;
return msg;

```

or

```auto
let filtered = Object.values(msg.payload).find(value => value.name === "sensor1");
msg = filtered === undefined ? null : {payload:filtered};
return msg;

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [6 January 2022 16:31 UTC](https://discourse.nodered.org/t/selecting-an-object-from-within-an-object/56205/7 "2022-01-06T16:31:42Z")

</div>

Thanks, that's better than my effort!

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [6 January 2022 16:34 UTC](https://discourse.nodered.org/t/selecting-an-object-from-within-an-object/56205/8 "2022-01-06T16:34:13Z")

</div>

@bakman2 @shrickus Thanks for helping.

---

<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:** [20 January 2022 16:34 UTC](https://discourse.nodered.org/t/selecting-an-object-from-within-an-object/56205/9 "2022-01-20T16:34:52Z")

</div>

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