I encountered an issue when converting a string to an array:
var data = "[[1, 2, 3,], [3, 2, 1]]";
var array = JSON.parse(data.replace(/,\s*]/g, ']'));
This problem arises when handling floating point numbers in the input:
var data = "[[2.,23.,1.5904], [4.,23,1.6208]]";
Upon attempting this code in the browser console:
var ar = JSON.parse(data.replace(/,\s*]/g, ']'));
An error occurs with the message:
SyntaxError: JSON.parse: unterminated fractional number at line 1 column 5 of the JSON data
The root of this issue seems to lie within the regexp used by JSON.parse()
, which is challenging for me as I am not well-versed in regex.
I've experimented with tools like for better understanding, but alas, the problem persists.
This question serves as an extension to a previous one that I had resolved:
Convert string of array, with array of array(s)
At that time, I overlooked the need to accommodate floating points in addition to other data types.