0

I am Using web service in with Jquery Ajax this returning me the following string

{"d":"[{\"username\":\"ABC\",\"designation\":\"\"}]"}

but when I am trying to parse this it is giving me error

var response = '{"d":"[{\"username\":\"ABC\",\"designation\":\"\"}]"}';

console.log(JSON.parse(response));

4
  • That's invalid json. The [] should not have quotes around them, and quotes should not be escaped. How was this json generated? Commented Apr 11, 2018 at 14:59
  • Can you please share more code (what have you tried so far) and exact error message? Commented Apr 11, 2018 at 14:59
  • I am calling web service given by other customer ....and I have to extract data from it by giving Jquery Ajax Call Commented Apr 11, 2018 at 15:02
  • var obj = jQuery.parseJSON(result); //result Means {"d":"[{\"username\":\"ABC\",\"designation\":\"\"}]"} Commented Apr 11, 2018 at 15:05

1 Answer 1

2

As others have noted, you need to remove the quotes around the braces.

const PATTERNS = [/"(\[)/g, /(\])"/g]; // Invalid patterns
const JsonFixer = json => PATTERNS.reduce((s, re) => s.replace(re, '$1'), json);

var rawJsonResponse = '{"d":"[{\"username\":\"ABC\",\"designation\":\"\"}]"}';

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

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

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.