3

I have a response which contains an array which is present inside string(""). What I need I just the array. How do I get rid of the quotes?

I tried JSON.parse(). It gave me error message

SyntaxError: Unexpected token ' in JSON at position 1

What I have is as follows:

email_id: "['[email protected]', '[email protected]']"

What I need is as follows:

email_id: ['[email protected]', '[email protected]']

This is a key which a part of a large response I am getting from backend in my angular 7 app.

2
  • Use JSON.parse(response.email_id) to parse only email_id Commented Sep 25, 2019 at 10:44
  • 1
    Possible duplicate of Parsing string as JSON with single quotes? Commented Sep 25, 2019 at 10:45

2 Answers 2

4

Below solution will work for your case provided the strings does not contain /'

var a = "['[email protected]', '[email protected]', '[email protected]']";
a = a.replace(/'/g, '"');
var result = JSON.parse(a);

Considering its an email data, there's no possibility of having that escape character sequence

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

Comments

1

You have to send in backend the format with double quotes instead of one quotes or in front just replace quotes ' with double quotes " inside your array , otherwise the parse will fail ,

see below snippet :

let json = {
  email_id:  "['[email protected]', '[email protected]']"
}
json.email_id = json.email_id.replace(/'/g,"\"");
console.log(JSON.parse(json.email_id));

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.