1

I've the following JSON(Valid) sting.

[["abc","{\"icon\":\"adjust\",\"prefix\":\"fa\",\"markerColor\":\"red\"}"],["xyz","{\"icon\":\"archive\",\"prefix\":\"fa\",\"markerColor\":\"green\"}"],["azs","{\"icon\":\"asterisk\",\"prefix\":\"fa\",\"markerColor\":\"darkred\"}"]]

it gives error when I try to Parse using the JSON.parse function here is the code that I'm using for parsing.

JSON.parse('[["abc","{\"icon\":\"adjust\",\"prefix\":\"fa\",\"markerColor\":\"red\"}"],["xyz","{\"icon\":\"archive\",\"prefix\":\"fa\",\"markerColor\":\"green\"}"],["azs","{\"icon\":\"asterisk\",\"prefix\":\"fa\",\"markerColor\":\"darkred\"}"]]');

and it gives an error in console Uncaught SyntaxError: Unexpected token i

here is the Correct Output by same string using online JSON viewer. Correct Output by same string using online JSON viewer

2
  • 1
    Double escape the quotes -- \\". Once for the contents of the JSON string, again for the JavaScript string literal it's wrapped in. Commented Apr 4, 2015 at 5:10
  • reducing \" to " didn't work. Although \" to \\" works Commented Apr 4, 2015 at 5:22

3 Answers 3

2

When you use JSON viewer, it's different from when you use the code in your JS code. Like @Jonathan stated, you should double escape you JSON string.

JSON.parse('[["abc","{\\"icon\\":\\"adjust\\",\\"prefix\\":\\"fa\\",\\"markerColor\\":\\"red\\"}"],["xyz","{\\"icon\\":\\"archive\\",\\"prefix\\":\\"fa\\",\\"markerColor\\":\\"green\\"}"],["azs","{\\"icon\\":\\"asterisk\\",\\"prefix\\":\\"fa\\",\\"markerColor\\":\\"darkred\\"}"]]');
Sign up to request clarification or add additional context in comments.

Comments

1

You need to escape (\) the escape (\") itself, within the string literal.

Additionally, you can parse this data in two passes.

const data = `[["abc","{\\"icon\\":\\"adjust\\",\\"prefix\\":\\"fa\\",\\"markerColor\\":\\"red\\"}"],["xyz","{\\"icon\\":\\"archive\\",\\"prefix\\":\\"fa\\",\\"markerColor\\":\\"green\\"}"],["azs","{\\"icon\\":\\"asterisk\\",\\"prefix\\":\\"fa\\",\\"markerColor\\":\\"darkred\\"}"]]`;

const firstPass = JSON.parse(data);
const secondPass = firstPass.map(([k, v]) => [k, JSON.parse(v)]);

console.log(secondPass);
.as-console-wrapper { top: 0; max-height: 100% !important; }

Comments

0

Your json structure is invalid. You should use this instead(without slashes):

  '[["abc",["icon":"adjust","prefix":"fa","markerColor":"red"]],["xyz",["icon":"archive","prefix":"fa","markerColor":"green"]],["azs",["icon":"asterisk","prefix":"fa","markerColor":"darkred"]]'

3 Comments

there is a JSON String inside the provided JSON. e.g: "{\"icon\":\"adjust\",\"prefix\":\"fa\",\"markerColor\":\"red\"}" You can treat the above JSON as Key pair value. ["abc","{\"icon\":\"adjust\",\"prefix\":\"fa\",\"markerColor\":\"red\"}"] here abc is key and "{\"icon\":\"adjust\",\"prefix\":\"fa\",\"markerColor\":\"red\"}" is value
It's not necessarily invalid, just uncommon. Some of the string values within the JSON just themselves contain more JSON: JSON.stringify([ 'abc', JSON.stringify({ icon: 'adjust' }) ])
replacing \" to \\" works for me. By the way, the above string works correctly Here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.