0

Can we make an array of list of Json For example: I have three JSON want to convert them into array.

{"region":"valore","price":"valore2"}
{"region":"valore","price":"valore2"}
{"region":"valore","price":"valore2"}

like

[
 {"region":"valore","price":"valore2"},
 {"region":"valore","price":"valore2"},
 {"region":"valore","price":"valore2"}
]

Can this is Possible????

asked Jun 6, 2016 at 13:50
8
  • array.push(threeJSON1);array.push(threeJSON2);array.push(threeJSON3); Commented Jun 6, 2016 at 13:57
  • 1
    So you want to combine those three strings into one JSON? Or do you want to convert these strings into javascript objects and add them to a javascript array? Commented Jun 6, 2016 at 13:58
  • Just a note, you do not have JSON! JSON is a string format. JSON is a string representation of data, that resembles (read: is a superset of) JavaScript object syntax. What you have are 3 JavaScript objects! Commented Jun 6, 2016 at 14:16
  • If you really do have JSON strings, you could just concat the strings together. Or you'd can use JSON.parse() (and then JSON.stringify()) to convert the JSON into an object then create the structure you wanted. Commented Jun 6, 2016 at 14:22
  • Do you really have JSON or do you have three JavaScript objects? Commented Jun 6, 2016 at 15:09

1 Answer 1

1

Going with your requirements, use array.push() function,

var array = [];
jsonOne = {"region":"valore","price":"valore2"};
jsonTwo = {"region":"valore","price":"valore2"};
jsonThree = {"region":"valore","price":"valore2"};
array.push(jsonOne);
array.push(jsonTwo);
array.push(jsonThree);

Update-1:

As Azamentes pointed out in the answer, I think the following is a clean way to get your desired output mainly in your case.

array = [jsonOne, jsonTwo, jsonThree];

But if you have many number of jsons, you will have to loop through them or something, then push() function will be handy;

answered Jun 6, 2016 at 14:05
Sign up to request clarification or add additional context in comments.

6 Comments

Why not just do var array = [jsonOne, jsonTwo, jsonThree]'? :)
Just a head's up -- nothing in this answer is JSON, except for your variable names.
thanks but if this json are coming from api then what to do ??
Then you might have to use push() function
Using push function, not giving the right answer which i needed
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.