A double quote even if escaped is throwing parse error.
look at the code below
//parse the json in javascript
var testJson = \'{\"result\": [\"lunch\",
If the standard C escapes are added, then JSON.parse
will convert sequences like \"
into "
, \\
into \
, \n
into a line-feed, etc.
'foo\\bar\nbaz"' === JSON.parse('"foo\\\\bar\\nbaz\\""')
In our project's case:
original string ""{\"lat\":28.4594965,\"lng\":77.0266383}""
After passing to JSON.parse()
"{"lat":28.4594965,"lng":77.0266383}"
On 2nd pass to JSON.parse()
{lat: 28.4594965, lng: 77.0266383}
Notice that JSON.parse()
removed escaping characters instead of converting string
to object
.
After the escaping characters were removed, the string to object conversion worked.
Here is the demo:
while (typeof p1 != 'object') {
p1 = JSON.parse(p1);
pass++;
console.log('On pass ', pass, p1);
}