Filter in json array

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:

may you help me?

Based on your image, shouldnt that be...

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)

My project is a device cloud connector for Smartthings.

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

{
  "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

That's what 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

1 Like

Thank's !

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

No, you cannot use JSONata in the Function node.

1 Like

And if i want delete this element?

There are quite a few ways.

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

Here is some ideas

1 Like

Thank's for your help :wink:

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

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