# Search in a Json

**URL:** <https://discourse.nodered.org/t/search-in-a-json/49358>\
**Category:** General\
**Created:** [4 August 2021 23:32 UTC](https://discourse.nodered.org/t/search-in-a-json/49358 "2021-08-04T23:32:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [4 August 2021 23:32 UTC](https://discourse.nodered.org/t/search-in-a-json/49358/1 "2021-08-04T23:32:47Z")

</div>

Hi

I need to make a search in a Json to find a number, and then return an other number, i did try to make a little flow to explane my quest.

my json is converted from a xml and imported from a file, but this is not in this demo flow, so it is not intended to be in a inject node

I uploaded the flow because it is to big to insert here.

[flows.json](https://discourse.nodered.org/uploads/short-url/1RWRysZySSqqxd9aKhDay8H5IEG.json) (171.8 KB)

Can someone please help me in the right direction ?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [4 August 2021 23:58 UTC](https://discourse.nodered.org/t/search-in-a-json/49358/2 "2021-08-04T23:58:35Z")

</div>

Hello Zamzon,

```auto
msg.payload = msg.payload.DeviceDetails.find(el => el.DeviceInfo.box_number === "46")
return msg;

```

this will return the first matching object from the array of `DeviceDetails` where its `DeviceInfo.box_number` equals with string 46 (note that your values are stored as strings so check for strings)

afterwords i didnt understand the criteria to return the `logical_area` since `logical_area` is in the array `LoadControlChannelList`

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [5 August 2021 00:05 UTC](https://discourse.nodered.org/t/search-in-a-json/49358/3 "2021-08-05T00:05:07Z")

</div>

Thanks,

It should look under LoadControlChannelList under the right Deviceinfo for "channel" (2) and then return Logical\_area for the right LoadControlChannelList

I updated the function.

```auto
var box_number = 46;
var channel = 2; // LoadControlChannelList

//Search in DeviceDetails 0-26 for box_number 46 

//It's found here:
//payload.DeviceDetails[3].DeviceInfo.box_number

//And then print the logical_area for the right DeviceDetails:

//msg.payload = msg.payload.DeviceDetails[3].LoadControlChannelList[2].logical_area
msg.payload = msg.payload.DeviceDetails.find(el => el.DeviceInfo.box_number === "46")

return msg;

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [5 August 2021 00:19 UTC](https://discourse.nodered.org/t/search-in-a-json/49358/4 "2021-08-05T00:19:55Z")

</div>

ok .. this makes more sense .. so you need from channel (2)

```auto
msg.payload = msg.payload.DeviceDetails.find(el => el.DeviceInfo.box_number === "46")
msg.payload = msg.payload.LoadControlChannelList[2].logical_area // channel(2)

return msg;

```

> **[Array.prototype.find() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)**
>
> The find() method returns the value of the first
> element in the provided array that satisfies the provided testing function. If no
> values satisfy the testing function, undefined is returned.

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [5 August 2021 09:04 UTC](https://discourse.nodered.org/t/search-in-a-json/49358/5 "2021-08-05T09:04:05Z")

</div>

Thanks,  
it works perfect 🙂

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [5 August 2021 10:49 UTC](https://discourse.nodered.org/t/search-in-a-json/49358/6 "2021-08-05T10:49:15Z")

</div>

Hi

I'm trying to insert the new code into the rest of my code, then I can't get box\_number from my float

this dosn't work and return 46

```auto
boxnr = flow.get("boxnr");  

```

this works perfect and return "46"

```auto
boxnr = "46"; 

```

```auto
boxnr = flow.get("boxnr"); 
var channel = 2;

msg.payload = msg.payload.DeviceExport.DeviceDetails.find(el => el.DeviceInfo[0].$.box_number === boxnr)
msg.payload = msg.payload.LoadControlChannelList[0].LoadControlChannel[channel].$.logical_area

```

I'm receiveing 46 from my float.get, but i can only get it to work if I insert "" so it looks line "46"

---

<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:** [5 August 2021 10:53 UTC](https://discourse.nodered.org/t/search-in-a-json/49358/7 "2021-08-05T10:53:27Z")

</div>

> [@zamzon](#):
>
> `msg.payload = msg.payload.DeviceExport.DeviceDetails.find(el => el.DeviceInfo[0].$.box_number === boxnr)`

The `===` means exact match. I.e. 46 is not === equal to "46"

You could use `parseInt` around the values or change to == or coerce the boxnr to a string e.g. `var boxnr = "" + flow.get("boxnr")`

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [6 August 2021 14:31 UTC](https://discourse.nodered.org/t/search-in-a-json/49358/8 "2021-08-06T14:31:08Z")

</div>

Thanks again, it works perfectly 🥳

---

<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 August 2021 14:31 UTC](https://discourse.nodered.org/t/search-in-a-json/49358/9 "2021-08-20T14:31:45Z")

</div>

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