0

The server returns me a JSON string like this:

{"payload":{"data":"{\"notification_type\":\"{\"type1\":\"{\"type2\":\"type2 value\"}\"}\"}"}}

This string, as I understand, cannot be parsed with JSON.parse() API because the nested JSON strings within the string should be properly escaped. If the string is not properly escaped I get the below error:

Uncaught SyntaxError: Unexpected token n in JSON at position 22

So, the string should be properly escaped respecting the nested nature like below so that JSON.parse() can process it:

var properString = "{\"payload\":{\"data\":\"{\\\"notification_type\\\":\\\"{\\\\\\\"InternalKey\\\\\\\":\\\\\\\"InternalValue\\\\\\\"}\\\"}\"}}";
console.log("Proper String = ");
console.log(properString);
var firstLevelObject = JSON.parse(properString);
console.log("First Level Object = ");
console.log(firstLevelObject);
var secondLevelObject = JSON.parse (firstLevelObject.payload.data);
console.log("Second Level Object = ");
console.log(secondLevelObject);
var thirdLevelObject = JSON.parse(secondLevelObject.notification_type);
console.log("Third Level Object = ");
console.log(thirdLevelObject);

This is how google chrome's console outputs the same: enter image description here

I however am unable to convert the improper string from server to properly escaped string as defined in the variable properString so that the JSON is properly constructed which can be traversable. How can the string be converted with proper escape characters?

Reference sources: I have referred to this answer to understand how nested escape characters should be added, but the answer does not state how conversion can be done.

2
  • 2
    First question: can you push back on the one who maintains the server code? Ideally you would never receive data like this! (I know, I know!) Commented Oct 15, 2019 at 14:04
  • 1
    @ScottSauyet - Yes bad code from server end, it will take months if not years to reach the team and convince them of this error. Not possible :( Commented Oct 15, 2019 at 14:51

1 Answer 1

1

Actually, the JSON string what you get is not exactly right. It's not a right JSON syntax in the nested JSON String. But you can rebuilt it to a correct JSON syntax string via regular expression or native JS.

var obj = '{"payload":{"data":"{\"notification_type\":\"{\"type1\":\"{\"type2\":\"type2 value\"}\"}\"}"}}';
var payloadString = obj.substring(0, 20).concat('"}}');
obj = obj.substring(20, obj.length - 3).replace("\"{", "{").replace("}\"}", "}}").replace("}\"}", "}}").replace("\"{", "{");

var data = JSON.parse(obj);
var obj2 = JSON.parse(payloadString);

//console.log(payload);
//console.log(data);
obj2.payload.data = data;
console.log(obj2);

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

1 Comment

Thanks for the answer, but isn't it too much of hardcoding? I was looking for a generic solution.

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.