# Help Parsing JSON

**URL:** <https://discourse.nodered.org/t/help-parsing-json/38008>\
**Category:** General\
**Created:** [25 December 2020 18:25 UTC](https://discourse.nodered.org/t/help-parsing-json/38008 "2020-12-25T18:25:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chura](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chura/32/32051_2.png) [@Chura](https://discourse.nodered.org/u/Chura)\
**Post date:** [25 December 2020 18:25 UTC](https://discourse.nodered.org/t/help-parsing-json/38008/1 "2020-12-25T18:25:09Z")

</div>

I have this data returned from node (Openhab2)

```auto
{

  "PRNRS_Shutter": {
    "link": "http://172.17.0.1:9000/rest/items/PRNRS_Shutter",
    "state": "0",
    "stateDescription": {
      "pattern": "%d %%",
      "readOnly": false,
      "options": []
    },
    "editable": false,
    "type": "Rollershutter",
    "name": "PRNRS_Shutter",
    "label": "Parent Room North",
    "category": "rollershutter",
    "tags": [
      "Blinds"
    ],
    "groupNames": [
      "gShutters",
      "gShuttersPR"
    ]
  },
  "PRWRS_Shutter": {
    "link": "http://172.17.0.1:9000/rest/items/PRWRS_Shutter",
    "state": "0",
    "stateDescription": {
      "pattern": "%d %%",
      "readOnly": false,
      "options": []
    },
    "editable": false,
    "type": "Rollershutter",
    "name": "PRWRS_Shutter",
    "label": "Parent Room West",
    "category": "rollershutter",
    "tags": [
      "Blinds"
    ],
    "groupNames": [
      "gShutters",
      "gShuttersPR"
    ]
  }
}

```

I'm trying to build something that will get state and label, and if state is grater then 0, write the number and label (so I can send it over telegram.

I'm not sure how to do that ☹  
Tried this way, but... not working obiously  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a11638d917cbbd7fcf32b96197547a25fdb96194.png)

---

<div class="post-metadata">

**Author:** ![Chura](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chura/32/32051_2.png) [@Chura](https://discourse.nodered.org/u/Chura)\
**Post date:** [25 December 2020 18:33 UTC](https://discourse.nodered.org/t/help-parsing-json/38008/2 "2020-12-25T18:33:07Z")

</div>

I've managed to get it working, if the input was only 1 state+label  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d3ad1a174140362acfe0862ecf51c9535891232e.png)

but no clue how to make it on multiple inputs like the original data

```auto
[{"id":"4c65b3f5.762f5c","type":"switch","z":"b3aeb474.f69408","name":"8+","property":"payload.*.state.$number()","propertyType":"jsonata","rules":[{"t":"gte","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":950,"y":320,"wires":[["47127edf.7d2af"],["7bb9dcb8.6f8d44"]]},{"id":"7bb9dcb8.6f8d44","type":"debug","z":"b3aeb474.f69408","name":"false","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":360,"wires":[]},{"id":"4824e897.8f1568","type":"debug","z":"b3aeb474.f69408","name":"true","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1310,"y":280,"wires":[]},{"id":"47127edf.7d2af","type":"change","z":"b3aeb474.f69408","name":"","rules":[{"t":"set","p":"payload.state","pt":"msg","to":"payload.*.state.$number()","tot":"jsonata"},{"t":"set","p":"payload.label","pt":"msg","to":"payload.*.label","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1140,"y":280,"wires":[["4824e897.8f1568"]]}]

```

---

<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:** [25 December 2020 19:17 UTC](https://discourse.nodered.org/t/help-parsing-json/38008/3 "2020-12-25T19:17:14Z")

</div>

Hi Chura and Merry Christmas,

Im not sure if you can use a Jsonata expression in the switch node in the case that the Jsonata produces more than one element in its result but i could be wrong.

Also i think the use of `$number` should have the state inside the brackets like `$number(state)` to convert the string into number.

My approach would be to create a new array msg with jsonata, filtering the fields you need and also converting the state to number  
Pass it from a Split node to test each msg separately in the switch node.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/c/4c54ac5e81ddc8132be93d0b5ef0078ea88b39a7.png)

**Test Flow:**

```auto
[{"id":"fead03c6.fd3e18","type":"switch","z":"3b79f9f0.c819be","name":"","property":"payload.state","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":910,"y":440,"wires":[["25cc2cf6.4a4a14"]]},{"id":"f66f1eb.90331e","type":"inject","z":"3b79f9f0.c819be","name":"data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"PRNRS_Shutter\":{\"link\":\"http://172.17.0.1:9000/rest/items/PRNRS_Shutter\",\"state\":\"0\",\"stateDescription\":{\"pattern\":\"%d %%\",\"readOnly\":false,\"options\":[]},\"editable\":false,\"type\":\"Rollershutter\",\"name\":\"PRNRS_Shutter\",\"label\":\"Parent Room North\",\"category\":\"rollershutter\",\"tags\":[\"Blinds\"],\"groupNames\":[\"gShutters\",\"gShuttersPR\"]},\"PRWRS_Shutter\":{\"link\":\"http://172.17.0.1:9000/rest/items/PRWRS_Shutter\",\"state\":\"0\",\"stateDescription\":{\"pattern\":\"%d %%\",\"readOnly\":false,\"options\":[]},\"editable\":false,\"type\":\"Rollershutter\",\"name\":\"PRWRS_Shutter\",\"label\":\"Parent Room West\",\"category\":\"rollershutter\",\"tags\":[\"Blinds\"],\"groupNames\":[\"gShutters\",\"gShuttersPR\"]}}","payloadType":"json","x":300,"y":440,"wires":[["9217d91b.46b75"]]},{"id":"9217d91b.46b75","type":"change","z":"3b79f9f0.c819be","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.*.{\t \"state\": $number(state),\t \"label\" : label\t }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":440,"wires":[["d06024c8.70ef48","f5a511ef.98a2c"]]},{"id":"d06024c8.70ef48","type":"split","z":"3b79f9f0.c819be","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":730,"y":440,"wires":[["fead03c6.fd3e18","9039addc.57f648"]]},{"id":"f5a511ef.98a2c","type":"debug","z":"3b79f9f0.c819be","name":"1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":360,"wires":[]},{"id":"9039addc.57f648","type":"debug","z":"3b79f9f0.c819be","name":"2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":360,"wires":[]},{"id":"25cc2cf6.4a4a14","type":"debug","z":"3b79f9f0.c819be","name":"> 0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1050,"y":440,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Chura](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chura/32/32051_2.png) [@Chura](https://discourse.nodered.org/u/Chura)\
**Post date:** [25 December 2020 19:41 UTC](https://discourse.nodered.org/t/help-parsing-json/38008/4 "2020-12-25T19:41:23Z")

</div>

> [@UnborN](#):
>
> ```auto
> [{"id":"fead03c6.fd3e18","type":"switch","z":"3b79f9f0.c819be","name":"","property":"payload.state","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":910,"y":440,"wires":[["25cc2cf6.4a4a14"]]},{"id":"f66f1eb.90331e","type":"inject","z":"3b79f9f0.c819be","name":"data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"PRNRS_Shutter\":{\"link\":\"http://172.17.0.1:9000/rest/items/PRNRS_Shutter\",\"state\":\"0\",\"stateDescription\":{\"pattern\":\"%d %%\",\"readOnly\":false,\"options\":[]},\"editable\":false,\"type\":\"Rollershutter\",\"name\":\"PRNRS_Shutter\",\"label\":\"Parent Room North\",\"category\":\"rollershutter\",\"tags\":[\"Blinds\"],\"groupNames\":[\"gShutters\",\"gShuttersPR\"]},\"PRWRS_Shutter\":{\"link\":\"http://172.17.0.1:9000/rest/items/PRWRS_Shutter\",\"state\":\"0\",\"stateDescription\":{\"pattern\":\"%d %%\",\"readOnly\":false,\"options\":[]},\"editable\":false,\"type\":\"Rollershutter\",\"name\":\"PRWRS_Shutter\",\"label\":\"Parent Room West\",\"category\":\"rollershutter\",\"tags\":[\"Blinds\"],\"groupNames\":[\"gShutters\",\"gShuttersPR\"]}}","payloadType":"json","x":300,"y":440,"wires":[["9217d91b.46b75"]]},{"id":"9217d91b.46b75","type":"change","z":"3b79f9f0.c819be","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.*.{\t \"state\": $number(state),\t \"label\" : label\t }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":440,"wires":[["d06024c8.70ef48","f5a511ef.98a2c"]]},{"id":"d06024c8.70ef48","type":"split","z":"3b79f9f0.c819be","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":730,"y":440,"wires":[["fead03c6.fd3e18","9039addc.57f648"]]},{"id":"f5a511ef.98a2c","type":"debug","z":"3b79f9f0.c819be","name":"1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":360,"wires":[]},{"id":"9039addc.57f648","type":"debug","z":"3b79f9f0.c819be","name":"2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":360,"wires":[]},{"id":"25cc2cf6.4a4a14","type":"debug","z":"3b79f9f0.c819be","name":"> 0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1050,"y":440,"wires":[]}]
> 
> ```

Thanks, And Merry Xmas to you as well.  
I actually managed to do that, a bit different from your approach. And to be honest, I'll be using yours probably 🙂

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe99bfc8aef57bf42df2046b586e4fbc2f7e880b.png)

```auto
[{"id":"fead03c6.fd3e18","type":"switch","z":"b3aeb474.f69408","name":"","property":"payload.state","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":850,"y":620,"wires":[["25cc2cf6.4a4a14"]]},{"id":"f66f1eb.90331e","type":"inject","z":"b3aeb474.f69408","name":"data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"PRNRS_Shutter\":{\"link\":\"http://172.17.0.1:9000/rest/items/PRNRS_Shutter\",\"state\":\"10\",\"stateDescription\":{\"pattern\":\"%d %%\",\"readOnly\":false,\"options\":[]},\"editable\":false,\"type\":\"Rollershutter\",\"name\":\"PRNRS_Shutter\",\"label\":\"Parent Room North\",\"category\":\"rollershutter\",\"tags\":[\"Blinds\"],\"groupNames\":[\"gShutters\",\"gShuttersPR\"]},\"PRWRS_Shutter\":{\"link\":\"http://172.17.0.1:9000/rest/items/PRWRS_Shutter\",\"state\":\"10\",\"stateDescription\":{\"pattern\":\"%d %%\",\"readOnly\":false,\"options\":[]},\"editable\":false,\"type\":\"Rollershutter\",\"name\":\"PRWRS_Shutter\",\"label\":\"Parent Room West\",\"category\":\"rollershutter\",\"tags\":[\"Blinds\"],\"groupNames\":[\"gShutters\",\"gShuttersPR\"]}}","payloadType":"json","x":240,"y":620,"wires":[["9217d91b.46b75","91d0c4e3.9bcff8"]]},{"id":"9217d91b.46b75","type":"change","z":"b3aeb474.f69408","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.*.{\t \"state\": $number(state),\t \"label\" : label\t }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":620,"wires":[["d06024c8.70ef48","f5a511ef.98a2c"]]},{"id":"d06024c8.70ef48","type":"split","z":"b3aeb474.f69408","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":670,"y":620,"wires":[["fead03c6.fd3e18","9039addc.57f648"]]},{"id":"f5a511ef.98a2c","type":"debug","z":"b3aeb474.f69408","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":540,"wires":[]},{"id":"9039addc.57f648","type":"debug","z":"b3aeb474.f69408","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":540,"wires":[]},{"id":"25cc2cf6.4a4a14","type":"debug","z":"b3aeb474.f69408","name":"> 0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":990,"y":620,"wires":[]},{"id":"a0625e87.21058","type":"function","z":"b3aeb474.f69408","name":"MSG","func":"var v = msg.payload.shutterDetails\nvar text = '';\nfor (var i = 0; i < v.length; i++) {\n if (v[i].state >= 7) {\n text += v[i].label + ' is opened ' + v[i].state + '%\\n';\n }\n}\nmsg.payload.array = text\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":670,"y":740,"wires":[["ec0fe5b1.529e98"]]},{"id":"91d0c4e3.9bcff8","type":"change","z":"b3aeb474.f69408","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"ToBeDeleted","tot":"msg"},{"t":"set","p":"payload.shutterDetails","pt":"msg","to":"ToBeDeleted.*","tot":"jsonata"},{"t":"delete","p":"ToBeDeleted","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":740,"wires":[["a0625e87.21058","91d1951e.600d48"]]},{"id":"91d1951e.600d48","type":"debug","z":"b3aeb474.f69408","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":680,"wires":[]},{"id":"ec0fe5b1.529e98","type":"debug","z":"b3aeb474.f69408","name":"4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":680,"wires":[]}]

```

---

<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 January 2021 19:41 UTC](https://discourse.nodered.org/t/help-parsing-json/38008/5 "2021-01-08T19:41:25Z")

</div>

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