Read coordinates from json string

Hey there,
i startet to use Node-RED and have a little probel.

I recieve a json String via MQTT. The thring includes coordinates like:

{
  "1": {
    "x": 5.4,
    "y": 12.5
  },
  "2": {
    "x": 20.0,
    "y": 1.2
  }
}

The numbers are IDs and depend on the number of recognised objects.

I now want to look if one of the "X" or "Y" values transcends a specific value.

Is there a way in Java or using a node to ask for "every number behind a 'X' " in the String?

I converted the string to an object and was able to get the numbers as a string, but it only works if i know the exact number of IDs.

sorry for my bad english, but i hope you understand the Problem :smiley:

It is not entierly clear what you want to do if any value is greater than a MAX limit.

Here is a demo that should get you started...

[{"id":"2e4caea4.70f522","type":"inject","z":"553814a2.1248ec","name":"BAD MQTT data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{   \"1\": {     \"x\": 5.4,     \"y\": 12.5   },   \"2\": {     \"x\": 20.0,     \"y\": 1.2   } }","payloadType":"json","x":900,"y":1800,"wires":[["2cac272b.7b8218"]]},{"id":"31dbcf3c.b18cd","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1260,"y":1820,"wires":[]},{"id":"2cac272b.7b8218","type":"function","z":"553814a2.1248ec","name":"","func":"\nconst MAX_X = 10;\nconst MAX_Y = 10;\nvar data = msg.payload;\nvar entries = Object.entries(data);\nvar err = false;\nfor (let index = 0; index < entries.length; index++) {\n    const el = entries[index];\n    let key = el[0];\n    let item = el[1];\n    if (item.x > MAX_X) {\n        node.error(`item ${key} x is greater than ${MAX_X}`, msg);\n        err = true;\n    }\n    if (item.y > MAX_Y) {\n        node.error(`item ${key} y is greater than ${MAX_Y}`, msg);\n        err = true;\n    }\n}\n\nif (!err) return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1080,"y":1820,"wires":[["31dbcf3c.b18cd"]]},{"id":"58fe94ec.b832bc","type":"inject","z":"553814a2.1248ec","name":"OK MQTT data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"1\":{\"x\":5.4,\"y\":7.5},\"2\":{\"x\":9,\"y\":1.2}}","payloadType":"json","x":900,"y":1840,"wires":[["2cac272b.7b8218"]]}]

example output...
image

1 Like

thanks for the quick answer.

Depends on the value i want want to send different outputs via MQTT.

For example:
"If any X is betwen 10 and 20, return "X-True". else return "X-False" on topic /X"
"If any Y is betwen 10 and 20, return "Y-True". else return "Y-False" on topic /Y"

I will try out your demo, i think it will helo a lot

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