1

I am creating a localStorage variable and always push data to it where I am collecting.

Here is my data set and it is dynamically generating.

{ 'my_id' : '79', 'cc_id' : '57223' }, { 'my_id' : '79', 'cc_id' : '57249' }, { 'my_id' : '79', 'cc_id' : '57250' }, { 'my_id' : '79', 'cc_id' : '57221' }, { 'my_id' : '79', 'cc_id' : '57220' }

I need to convert this to a json array.

How could I do so. I tried but couldn't find out a way.

3
  • 1
    Possible duplicate of Convert JS object to JSON string If you check out the answer to that question, you'll see there is a built-in method in JavaScript to convert objects and arrays to JSON. Commented Jun 9, 2016 at 12:14
  • Does this help? stackoverflow.com/questions/2250953/… Commented Jun 9, 2016 at 12:15
  • @forgivenson This is not a duplicate question and I searched in the link you provided too and tried the ways on it before ask this question. Commented Jun 9, 2016 at 12:22

1 Answer 1

7

Surround your data with [ and ].

Also single quotes are not allowed, only double quotes.

So here, what you'll have:

[ { "my_id" : "79", "cc_id" : "57223" }, { "my_id" : "79", "cc_id" : "57249" }, { "my_id" : "79", "cc_id" : "57250" }, { "my_id" : "79", "cc_id" : "57221" }, { "my_id" : "79", "cc_id" : "57220" } ]
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.