0

This string is received from database:

"[{\"country_code\":\"IN\",\"gdpr_fields\":{\"policy\":\"yes\",\"profiling\":\"na\",\"age\":\"yes\",\"subscription\":\"na\"}}]"

I have been trying to convert this string to a JSON value but it always ends up throwing an error

undefined:1
[{\"country_code\":\"IN\",\"gdpr_fields\":{\"policy\":\"yes\",\"profiling\":\"na\",\"age\":\"yes\",\"subscription\":\"na\"}}]
  ^
Unexpected token \ in JSON at position 2

Is there a way to convert this string to JSON using javascript?

Expected Result:

[{"country_code":"IN","gdpr_fields":{"policy":"yes","profiling":"na","age":"yes","subscription":"na"}}]
13
  • 2
    I have been trying to convert this string to a JSON value ... How? It looks like you are parsing JSON instead of stringifying it. Commented Jul 8, 2021 at 10:37
  • Why do you have a string like that in the first place? Can't you fix the issue with whatever's creating it? Commented Jul 8, 2021 at 10:39
  • is that string coming from database ? looks like you have double time json stringified it. Commented Jul 8, 2021 at 10:44
  • 1
    @ChrisG No, the string is not valid JSON. The backslashes are actual characters in the string. You can see it in the error message. They're not escape symbols. Commented Jul 8, 2021 at 11:31
  • 1
    @ChrisG I showed you a fiddle that proves that JSON.parse can't handle actual backslashes: jsfiddle.net/egm5vhtu Often databases are connected to many systems and it's not possible to fix all systems at once. A workaround is sometimes necessary. Commented Jul 8, 2021 at 11:46

3 Answers 3

1

You can replace all \" with " with replaceAll or replace with a regex (for older browsers/engines):

const invalidJson = '[{\\"country_code\\":\\"IN\\",\\"gdpr_fields\\":{\\"policy\\":\\"yes\\",\\"profiling\\":\\"na\\",\\"age\\":\\"yes\\",\\"subscription\\":\\"na\\"}}]';

try {
  JSON.parse(invalidJson);
} catch(err) {
  console.log('Parse error');
}
const json = invalidJson.replaceAll('\\"', '\"');
console.log(JSON.parse(json));

const invalidJson = '[{\\"country_code\\":\\"IN\\",\\"gdpr_fields\\":{\\"policy\\":\\"yes\\",\\"profiling\\":\\"na\\",\\"age\\":\\"yes\\",\\"subscription\\":\\"na\\"}}]';

try {
  JSON.parse(invalidJson);
} catch(err) {
  console.log('Parse error');
}
const json = invalidJson.replace(/\\"/g, '\"');
console.log(JSON.parse(json));

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

Comments

1

var temp = "[{\"country_code\":\"IN\",\"gdpr_fields\":{\"policy\":\"yes\",\"profiling\":\"na\",\"age\":\"yes\",\"subscription\":\"na\"}}]";   


temp = JSON.parse(temp);

console.log(temp);

Please use something like this, it is working for me.

1 Comment

Hey, @user12551649 use this, it is working well.
0

This could be only possible by first cleaning the string

jsonArr = "[{\\\"country_code\\\":\\\"IN\\\",\\\"gdpr_fields\\\":{\\\"policy\\\":\\\"yes\\\",\\\"profiling\\\":\\\"na\\\",\\\"age\\\":\\\"yes\\\",\\\"subscription\\\":\\\"na\\\"}}]".split("\\").join("")

and then

JSON.parse(jsonArr)

Comments

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.