Home > Net >  Converting Multiple JSON into an array of objects in JavaScript
Converting Multiple JSON into an array of objects in JavaScript

Time:09-21

I have multiple JSONs and I would like to convert to an array of objects.

peopleJSONData = 
    {"name":"Nicholas","age":"30"}
    {"name":"Daniel","age":45"}

I would like to convert it to an array of objects in JavaScript.

peopleArrayObjects = 
    [
        {name: "Nicholas", age: 30}
        {name: "Daniel", age: 45}
    ]

Please help! :)

CodePudding user response:

If you have multiple JSON Objects, I assume it would be in a different variable. Like

var json1 = {"name":"Nicholas","age":"30"};
    
var json2 = {"name":"Daniel","age":45"};

What you can do is :

let peopleArrayObjects = [];
peopleArrayObjects.push(json1);
peopleArrayObjects.push(json2);

AND if you have a single JSON having multiple

CodePudding user response:

I think this will work :


const peopleArrayObjects = [];

peopleArrayObjects.push(JSON.parse(peopleJSONData));

  • Related