Filter only some fields in a json

Hello!

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

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

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

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

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

I recommend watching this playlist: Node-RED Essentials. 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.

Possibly

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.

1 Like

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

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

examples with time comparisions

[{"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":[]}]

Thank you!

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