Help Parsing JSON

I have this data returned from node (Openhab2)

{

  "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 :frowning:
Tried this way, but... not working obiously
image

I've managed to get it working, if the input was only 1 state+label
image

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

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

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.

Test Flow:

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

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

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