I have a NodeJS App that accepts a a string (uploaded input!) I have No Control over the input I am merely building a REST Service to Processes the data.
That string is meant to be an Array of JSON Objects that I can then loop through to extract each element ...
I'm receiving the following (as a string):
[
{Name: 'Jane',
Id: '005b0000000MGa7AAG'},
{Name: 'Tom',
Id: '005b0000000MGa7AAF'}
]
When I try to JSON.parse this I get
SyntaxError: Unexpected token N
Understandably so, because I know this is Invalid JSON
whereas This next string is valid JSON and passes http://jsonlint.com/:
[
{"Name": "Jack",
"Id": "005b0000000MGa7AAA"},
{"Name": "Jill",
"Id": "005b0000000MGa7AAB"}
]
My question is: How can I accept the first input and parse it to allow:
parsed[0]['Name'] == 'Jane'
>> true
My first instinct is to string replace the keys (e.g. Name to "Name") and then try parsing it. But if anyone else has a solution, I'd be grateful.
Use the JSON. parse() Expression to Convert a String Into an Array. The JSON. parse() expression is used to parse the data received from the web server into the objects and arrays.
The string in JavaScript can be converted into a character array by using the split() and Array. from() functions.
To convert an array to an object, use the reduce() method to iterate over the array, passing it an object as the initial value. On each iteration, assign a new key-value pair to the accumulated object and return the result. Copied! const arr = ['zero', 'one', 'two']; const obj4 = arr.
You can do this with a bit of Regex replacing:
var json = "[ {Name: 'Jane', Id: '005b0000000MGa7AAG'}, {Name: 'Tom', Id: '005b0000000MGa7AAF'} ]";
var newJson = json.replace(/([a-zA-Z0-9]+?):/g, '"$1":');
newJson = newJson.replace(/'/g, '"');
var data = JSON.parse(newJson);
alert(data[0]["Name"]);
First of all we wrap the propertie names with quotes, then we replace the single-quotes with double-quotes. This is enough to make it valid JSON which can then be parsed.
Here is a working example
NOTE: Using RegEx in general for things like this is not ideal, and this solution will only work under specific circumstances (like this example). As mentioned in comments, one problem would be if the data values contained a colon :
With that in mind, this is a more reliable solution:
var json = $("div").html();
var newJson = json.replace(/'/g, '"');
newJson = newJson.replace(/([^"]+)|("[^"]+")/g, function($0, $1, $2) {
if ($1) {
return $1.replace(/([a-zA-Z0-9]+?):/g, '"$1":');
} else {
return $2;
}
});
var data = JSON.parse(newJson);
alert(data[0]["Name"]);
This will match any variables (alphanumeric) that end with a colon :
, but will ingore any matches that are found between quotes (i.e. data string values)
Here is an example
Create a javascript class, populate the object with your input and then convert it in a clean json using: JSON.stringify(object)
to avoid errors . My full post here .
NOTE: if you use IE7 or less version you must add the JSON.js
library to use JSON.stringify()
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With