1

I'm attempting to parse the following JSON string (not in control of the format, I know it's hideous).

    var json = '{"what.1.does":"anything", "nestedjsonstr":"{\"whatup\":\"nada\"}"}';
    obj = JSON.parse(json);

I'm getting Error: Unexpected token w in JSON at position 43 which is where the first value of nestedjsonstr begins. Is there any elegant way to parse this?

1 Answer 1

1

Maybe this can help you out. You replace the curly braces inside your string without the ", and remove the \.

var json = '{"what.1.does":"anything", "nestedjsonstr":"{\"whatup\":\"nada\"}"}';

json = json.replace('\"{', '{').replace('}\"', '}').replace('\\"', '"');

obj = JSON.parse(json);
console.log(obj);

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. This works. Is it safe enough to clear all the escape characters though (as that would clear it in the entire json string)?
Maybe try json = json.replace('\"{', '{').replace('}\"', '}').replace('\\"', '"'). I'll edit the answer

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.