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
Pallavi
831 gold badge1 silver badge10 bronze badges
1 Answer 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
Chinni
1,2901 gold badge16 silver badges28 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
Azamantes
Why not just do
var array = [jsonOne, jsonTwo, jsonThree]'? :)Jeremy J Starcher
Just a head's up -- nothing in this answer is JSON, except for your variable names.
Pallavi
thanks but if this json are coming from api then what to do ??
Chinni
Then you might have to use
push() functionPallavi
Using
push function, not giving the right answer which i needed |
lang-js
array.push(threeJSON1);array.push(threeJSON2);array.push(threeJSON3);JSON.parse()(and thenJSON.stringify()) to convert the JSON into an object then create the structure you wanted.