# Filter only some fields in a json

**URL:** <https://discourse.nodered.org/t/filter-only-some-fields-in-a-json/85823>\
**Category:** General\
**Created:** [23 February 2024 15:52 UTC](https://discourse.nodered.org/t/filter-only-some-fields-in-a-json/85823 "2024-02-23T15:52:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fikerplunk](https://avatars.discourse-cdn.com/v4/letter/f/e19b73/32.png) [@fikerplunk](https://discourse.nodered.org/u/fikerplunk)\
**Post date:** [23 February 2024 15:52 UTC](https://discourse.nodered.org/t/filter-only-some-fields-in-a-json/85823/1 "2024-02-23T15:52:22Z")

</div>

Hello!

I'm receiving the following JSON from an http request:

```auto
{
   "rows":[
      {
         "id":"AAAA",
         "number":38,
         "numberInColumn":11,
         "numberTwo":1,
         "size":"P",
         "status":"AVAILABLE",
         "enable":true,
         "updatedAt":"2023-03-15T20:48:37.000Z",
         "companyId":"XXX",
         "company":{
            "id":"XXXX",
            "name":"TEST",
            "type":"carrier"
         }
      },
      {
         "id":"BBB",
         "number":40,
         "numberInColumn":12,
         "numberTwo":2,
         "size":"P",
         "status":"AVAILABLE",
         "enable":true,
         "updatedAt":"2023-03-15T20:48:37.000Z",
         "companyId":"XXX",
         "company":{
            "id":"XXXX",
            "name":"TEST",
            "type":"carrier"
         }
      }
   ]
}

```

I would like to filter this json and remove some fields so I can send it to an mqtt topic, looking like this:

```auto
{
   "rows":[
      {
         "id":"AAAA",
         "number":38,
         "numberInColumn":11,
         "numberTwo":1,
         "size":"P",
         "status":"AVAILABLE"
      },
      {
         "id":"BBB",
         "number":40,
         "numberInColumn":12,
         "numberTwo":2,
         "size":"P",
         "status":"AVAILABLE"
      }
   ]
}

```

I'm new to node-red and I have no idea how I could do this filter. I found some similar questions here on the forum but none of them helped me.

Could anyone 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:** [23 February 2024 16:02 UTC](https://discourse.nodered.org/t/filter-only-some-fields-in-a-json/85823/2 "2024-02-23T16:02:01Z")

</div>

The node-red way would be to pass the msg through a split node, then a change nide where you can delete the properties you don't want, then pass it through a join node. That's it.

However, if you are a js guy, try this in a function node

```auto
msg.payload = msg.payload.map(e => {
     return {
          id: msg.payload.id,
         number: msg.payload.number,
         numberInColumn: msg.payload.numberInColumn,
         numberTwo: msg.payload.number,
         size: msg.payload.size,
         status: msg.payload.status
    }
})
return msg

```

_untested_

---

<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:** [23 February 2024 16:02 UTC](https://discourse.nodered.org/t/filter-only-some-fields-in-a-json/85823/3 "2024-02-23T16:02:59Z")

</div>

> [@fikerplunk](#):
>
> I'm new to node-red and I have no idea

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<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:** [23 February 2024 16:16 UTC](https://discourse.nodered.org/t/filter-only-some-fields-in-a-json/85823/4 "2024-02-23T16:16:07Z")

</div>

> [@Steve-Mcl](#):
>
> `msg.payload = msg.payload.map(e => {`

Possibly

```auto
msg.payload.rows = msg.payload.rows.map(e => {
     return {
          id: e.id,
         number: e.number,
         numberInColumn: e.numberInColumn,
         numberTwo: e.number,
         size: e.size,
         status: e.status
    }
})
return msg

```

Also untested though.

---

<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:** [23 February 2024 16:37 UTC](https://discourse.nodered.org/t/filter-only-some-fields-in-a-json/85823/5 "2024-02-23T16:37:59Z")

</div>

You can also do so using the transform function of JSONata in a change node, which is pretty comparable in time to map ( Just for the haters, lol. I think JS is marginally faster by 1 millisecond).  
expression

```auto
$$.payload~> |$.rows|{},["enable","updatedAt","companyId","company"]|

```

examples with time comparisions

```auto
[{"id":"2a36f7a1b6e292d0","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"time","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"rows\":[{ \"id\":\"AAAA\", \"number\":38, \"numberInColumn\":11, \"numberTwo\":1, \"size\":\"P\", \"status\":\"AVAILABLE\", \"enable\":true, \"updatedAt\":\"2023-03-15T20:48:37.000Z\", \"companyId\":\"XXX\", \"company\":{ \"id\":\"XXXX\", \"name\":\"TEST\", \"type\":\"carrier\" } }, { \"id\":\"BBB\", \"number\":40, \"numberInColumn\":12, \"numberTwo\":2, \"size\":\"P\", \"status\":\"AVAILABLE\", \"enable\":true, \"updatedAt\":\"2023-03-15T20:48:37.000Z\", \"companyId\":\"XXX\", \"company\":{ \"id\":\"XXXX\", \"name\":\"TEST\", \"type\":\"carrier\" } }] }","payloadType":"json","x":310,"y":6720,"wires":[["b238c57b0e98b8c9"]]},{"id":"b655094cd22e9012","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"time","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"rows\":[{ \"id\":\"AAAA\", \"number\":38, \"numberInColumn\":11, \"numberTwo\":1, \"size\":\"P\", \"status\":\"AVAILABLE\", \"enable\":true, \"updatedAt\":\"2023-03-15T20:48:37.000Z\", \"companyId\":\"XXX\", \"company\":{ \"id\":\"XXXX\", \"name\":\"TEST\", \"type\":\"carrier\" } }, { \"id\":\"BBB\", \"number\":40, \"numberInColumn\":12, \"numberTwo\":2, \"size\":\"P\", \"status\":\"AVAILABLE\", \"enable\":true, \"updatedAt\":\"2023-03-15T20:48:37.000Z\", \"companyId\":\"XXX\", \"company\":{ \"id\":\"XXXX\", \"name\":\"TEST\", \"type\":\"carrier\" } }] }","payloadType":"json","x":330,"y":6820,"wires":[["67211d097361febe"]]},{"id":"67211d097361febe","type":"function","z":"d1395164b4eec73e","name":"function 154","func":"msg.payload.rows = msg.payload.rows.map(e => {\n return {\n id: e.id,\n number: e.number,\n numberInColumn: e.numberInColumn,\n numberTwo: e.number,\n size: e.size,\n status: e.status\n }\n})\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":6820,"wires":[["f1dc9963d0f7e4f6"]]},{"id":"f1dc9963d0f7e4f6","type":"debug","z":"d1395164b4eec73e","name":"debug 2477","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"$millis()-$$.time","statusType":"jsonata","x":690,"y":6780,"wires":[]},{"id":"b238c57b0e98b8c9","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload~> |$.rows|{},[\"enable\",\"updatedAt\",\"companyId\",\"company\"]|","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":6720,"wires":[["ea5edc65218d93a4"]]},{"id":"ea5edc65218d93a4","type":"debug","z":"d1395164b4eec73e","name":"debug 2475","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"$millis()-$$.time","statusType":"jsonata","x":670,"y":6700,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![fikerplunk](https://avatars.discourse-cdn.com/v4/letter/f/e19b73/32.png) [@fikerplunk](https://discourse.nodered.org/u/fikerplunk)\
**Post date:** [23 February 2024 17:15 UTC](https://discourse.nodered.org/t/filter-only-some-fields-in-a-json/85823/6 "2024-02-23T17:15:03Z")

</div>

Thank you!

---

<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:** [8 March 2024 17:15 UTC](https://discourse.nodered.org/t/filter-only-some-fields-in-a-json/85823/7 "2024-03-08T17:15:22Z")

</div>

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