-6

I want to get the values of each keyword:

var keyword = '{"coconut sugar", "healthy and natural sweetener", "low glycemic index sweetener"}';

enter image description here

To this:

coconut sugar
healthy and natural sweetener
low glycemic index sweetener
7
  • 4
    That's an invalid "json object". Use an array instead and join by \n Commented Jul 26, 2018 at 6:10
  • object contains key and value pair Commented Jul 26, 2018 at 6:10
  • You mean the keyword should be an array not object? Commented Jul 26, 2018 at 6:13
  • Please post code instead of or with the picture Commented Jul 26, 2018 at 6:14
  • 1
    Okay I will fix my json object Commented Jul 26, 2018 at 6:19

1 Answer 1

1

That's an invalid "object". It is supposed to be an array with []. If that's how your server is giving, you have to change the response there. In case, if you can't do that, you may parse it right in JavaScript.

var keyword = '{"coconut sugar", "healthy and natural sweetener", "low glycemic index sweetener"}';
keyword = JSON.parse("[" + keyword.substr(1, keyword.length - 2) + "]");
console.log(keyword);

// After this use any array walker to process it.
for (var i = 0; i < keyword.length; i++)
  console.log(keyword[i]);

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.