# JSON parsing issues due to locale?

**URL:** https://discourse.nodered.org/t/json-parsing-issues-due-to-locale/58911
**Category:** General
**Created:** [24 February 2022 09:01 UTC](https://discourse.nodered.org/t/json-parsing-issues-due-to-locale/58911 "2022-02-24T09:01:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)
#### Post date: [24 February 2022 09:01 UTC](https://discourse.nodered.org/t/json-parsing-issues-due-to-locale/58911/1 "2022-02-24T09:01:53Z")

</div>

I'm trying to use the json node to parse the string from a GET request, which is properly formatted.

The problem seems to be that I get some floating point numbers with decimal point ".", but my locale is using decimal comma "," instead, so I get a JSON parsing error.

Is there no option in the node (or in the JSON.parse() function) to solve that? I'm assuming that replacing the points by commas in the JSON string will mess up the whole thing.

I had a similar issue with the text input node set to input numbers, when I try to type a number with a decimal fraction using the keyboard it will type a point "." despite my locale using a comma, so NR will not accept it as a valid number. I must use the comma in the regular keyboard (next to the "M") so the number is accepted.

My NR server might need an exorcism.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [24 February 2022 09:16 UTC](https://discourse.nodered.org/t/json-parsing-issues-due-to-locale/58911/2 "2022-02-24T09:16:06Z")

</div>

What is it that is putting the invalid JSON there in the first place?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [24 February 2022 09:16 UTC](https://discourse.nodered.org/t/json-parsing-issues-due-to-locale/58911/3 "2022-02-24T09:16:13Z")

</div>

A simple regex should replace the commas  
e.g.

```auto
[{"id":"e84eb60a.92a638","type":"inject","z":"bf9e1e33.030598","name":"Switch ON","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{ \"name\": \"artesium\", \"amount_final\": 20, \"tincture_con\": 2,00, \"tincture_abv\": 60, \"tincture_needed\": 0.1, \"ethanol_fraction\": 0.6, \"ethanol_absolute\": 0.06 }]","payloadType":"str","x":150,"y":2020,"wires":[["c5bfcd6b.8ceed"]]},{"id":"c5bfcd6b.8ceed","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"(?<=\\d+),(?=\\d+)","fromt":"re","to":".","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":2020,"wires":[["add8d674.50adf8"]]},{"id":"add8d674.50adf8","type":"json","z":"bf9e1e33.030598","name":"","property":"payload","action":"","pretty":false,"x":570,"y":2040,"wires":[["9c2dee49.d0ea98"]]},{"id":"9c2dee49.d0ea98","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":1960,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)
#### Post date: [24 February 2022 09:23 UTC](https://discourse.nodered.org/t/json-parsing-issues-due-to-locale/58911/4 "2022-02-24T09:23:44Z")

</div>

It's a GET request to an API, it sends a JSON with two arrays that contain a command history for mobile robots.

If I don't do anything, the object is usually valid, I can fetch the last command from the array, and get the data for that one.

However, if I reboot the server, what I get is an object with two empty arrays, and that starts causing problems.

msg.payload.historicalEntries.length returns _ **undefined** _ (shouldn't it be zero?).

If I try to process the feedback from the API as a string and then parse it in NR, then I get this JSON parse error.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [24 February 2022 10:22 UTC](https://discourse.nodered.org/t/json-parsing-issues-due-to-locale/58911/5 "2022-02-24T10:22:51Z")

</div>

> [@OriolFM](#):
>
> msg.payload.historicalEntries.length returns _ **undefined** _ (shouldn't it be zero?).

Show us what it looks like in a debug node.

---

<div class="post-metadata">

### Author: ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)
#### Post date: [24 February 2022 11:06 UTC](https://discourse.nodered.org/t/json-parsing-issues-due-to-locale/58911/6 "2022-02-24T11:06:09Z")

</div>

Okay, I took a different approach now:

I use the json node to handle the output, and a change node to set msg.payload = msg.payload.historicalEntries[0] with deep copy.

If the array is not empty, it works well, I get the object with the last command data, add the details to the machine object and continue with the flow.

If the array is empty, then msg.payload will be undefined. In this case, I can ignore the historical command array, specify that the server has been rebooted and there is no historical data, and continue along the flow.

This solves my original issue.

As for the JSON parsing issue, now it does seem to work properly. Strange 😕

---

<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: [25 April 2022 11:06 UTC](https://discourse.nodered.org/t/json-parsing-issues-due-to-locale/58911/7 "2022-04-25T11:06:44Z")

</div>

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