# Filter in json array

**URL:** <https://discourse.nodered.org/t/filter-in-json-array/36484>\
**Category:** General\
**Created:** [24 November 2020 19:58 UTC](https://discourse.nodered.org/t/filter-in-json-array/36484 "2020-11-24T19:58:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ej3359](https://avatars.discourse-cdn.com/v4/letter/e/e47c2d/32.png) [@ej3359](https://discourse.nodered.org/u/ej3359)\
**Post date:** [24 November 2020 19:58 UTC](https://discourse.nodered.org/t/filter-in-json-array/36484/1 "2020-11-24T19:58:58Z")

</div>

Hello,

i would like extract an element in a json base on the externalDeviceId attribut

my code:  
var element = jsonState.deviceState.filter(el =\> el.externalDeviceId.includes("b827ebe81d30.3"));  
jsonState is my json object.

the variable elemet receive the total array (devices) but not the unique element that externalDeviceId contain "b827ebe81d30.3"

my json:

 ![json](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/52fe9d53be5067f2512f2d7d1e1b240ee3bb5b5a.png)

may you help me?

---

<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:** [24 November 2020 22:22 UTC](https://discourse.nodered.org/t/filter-in-json-array/36484/2 "2020-11-24T22:22:40Z")

</div>

> [@ej3359](#):
>
> var element = jsonState.deviceState.filter(el =\> el.externalDeviceId.includes("b827ebe81d30.3"));

Based on your image, shouldnt that be...

```auto
var element = jsonState.devices.filter(el => (el.externalDeviceId == "b827ebe81d30.3"));

```

Note also the array property name is `devices` not `deviceState`

(I am guessing some things as you haven't provided the data or any more context)

---

<div class="post-metadata">

**Author:** ![ej3359](https://avatars.discourse-cdn.com/v4/letter/e/e47c2d/32.png) [@ej3359](https://discourse.nodered.org/u/ej3359)\
**Post date:** [25 November 2020 05:14 UTC](https://discourse.nodered.org/t/filter-in-json-array/36484/3 "2020-11-25T05:14:09Z")

</div>

My project is a device cloud connector for Smartthings.

the json information from Samsung (the property name est "deviceState":

```auto
{
  "headers": {
    "schema": "st-schema",
    "version": "1.0",
    "interactionType": "stateRefreshResponse",
    "requestId": "abc-123-456"
  },
  "deviceState": [
    {
      "externalDeviceId": "pdevice-1",
      "deviceCookie": {},
      "states": [
        {
          "component": "main",
          "capability": "st.switch",
          "attribute": "switch",
          "value": "on"
        },
        {
          "component": "main",
          "capability": "st.switchLevel",
          "attribute": "level",
          "value": 80
        },
        {
          "component": "main",
          "capability": "st.healthCheck",
          "attribute": "healthStatus",
          "value": "online"
        },
        {
          "component": "main",
          "capability": "st.colorControl",
          "attribute": "hue",
          "value": 0
        },
        {
          "component": "main",
          "capability": "st.colorControl",
          "attribute": "saturation",
          "value": 0          
        }
        {
          "component": "main",
          "capability": "st.colorTemperature",
          "attribute": "colorTemperature",
          "value": 3500
        }        
      ]
    }
  ]
}

```

the documentation:  
https://smartthings.developer.samsung.com/docs/devices/smartthings-schema/smartthings-schema-reference.html

I need to find an element in the deviceState array based on the externalDeviceId

I have tried:  
var element = jsonState.deviceState.filter(el =\>( el.externalDeviceId.includes("b827ebe81d30.3")));  
i have the same problem, it return the array and not the element

---

<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:** [25 November 2020 07:23 UTC](https://discourse.nodered.org/t/filter-in-json-array/36484/4 "2020-11-25T07:23:54Z")

</div>

> [@ej3359](#):
>
> it return the array and not the element

That's what [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) function does.

> The filter() method creates a new array with all elements that pass the test implemented by the provided function.

If you are expecting 1 item, use [find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)

---

<div class="post-metadata">

**Author:** ![ej3359](https://avatars.discourse-cdn.com/v4/letter/e/e47c2d/32.png) [@ej3359](https://discourse.nodered.org/u/ej3359)\
**Post date:** [25 November 2020 08:13 UTC](https://discourse.nodered.org/t/filter-in-json-array/36484/5 "2020-11-25T08:13:12Z")

</div>

Thank's !

And is it possible to use jsonata in a node function, like this:  
var element = jsonState.deviceState[externalDeviceId="b827ebe81d30.3"]

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [25 November 2020 08:44 UTC](https://discourse.nodered.org/t/filter-in-json-array/36484/6 "2020-11-25T08:44:41Z")

</div>

No, you cannot use JSONata in the Function node.

---

<div class="post-metadata">

**Author:** ![ej3359](https://avatars.discourse-cdn.com/v4/letter/e/e47c2d/32.png) [@ej3359](https://discourse.nodered.org/u/ej3359)\
**Post date:** [25 November 2020 16:57 UTC](https://discourse.nodered.org/t/filter-in-json-array/36484/7 "2020-11-25T16:57:39Z")

</div>

And if i want delete this element?

---

<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:** [26 November 2020 00:07 UTC](https://discourse.nodered.org/t/filter-in-json-array/36484/8 "2020-11-26T00:07:01Z")

</div>

There are quite a few ways.

I would possibly use filter method where the item is NOT equal.

[Here](https://love2dev.com/blog/javascript-remove-from-array/) is some ideas

---

<div class="post-metadata">

**Author:** ![ej3359](https://avatars.discourse-cdn.com/v4/letter/e/e47c2d/32.png) [@ej3359](https://discourse.nodered.org/u/ej3359)\
**Post date:** [26 November 2020 07:04 UTC](https://discourse.nodered.org/t/filter-in-json-array/36484/9 "2020-11-26T07:04:40Z")

</div>

Thank's for your help 😉

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [26 November 2020 07:27 UTC](https://discourse.nodered.org/t/filter-in-json-array/36484/10 "2020-11-26T07:27:51Z")

</div>

Most likely you can also achieve this using a JSONata expression in a change node.

---

<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:** [10 December 2020 07:27 UTC](https://discourse.nodered.org/t/filter-in-json-array/36484/11 "2020-12-10T07:27:56Z")

</div>

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