# Read coordinates from json string

**URL:** https://discourse.nodered.org/t/read-coordinates-from-json-string/45037
**Category:** General
**Created:** [1 May 2021 21:58 UTC](https://discourse.nodered.org/t/read-coordinates-from-json-string/45037 "2021-05-01T21:58:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Bene](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@Bene](https://discourse.nodered.org/u/Bene)
#### Post date: [1 May 2021 21:58 UTC](https://discourse.nodered.org/t/read-coordinates-from-json-string/45037/1 "2021-05-01T21:58:16Z")

</div>

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:

```auto
{
  "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 😃

---

<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: [1 May 2021 22:23 UTC](https://discourse.nodered.org/t/read-coordinates-from-json-string/45037/2 "2021-05-01T22:23:49Z")

</div>

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...

```auto
[{"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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/80fa5cd78bf5a51c143d4246a7be2af9a4aa6603.png)

---

<div class="post-metadata">

### Author: ![Bene](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@Bene](https://discourse.nodered.org/u/Bene)
#### Post date: [2 May 2021 10:25 UTC](https://discourse.nodered.org/t/read-coordinates-from-json-string/45037/3 "2021-05-02T10:25:15Z")

</div>

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

---

<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: [16 May 2021 10:25 UTC](https://discourse.nodered.org/t/read-coordinates-from-json-string/45037/4 "2021-05-16T10:25:51Z")

</div>

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