How to parse a JSON array in Node.js

3 Answers

0 votes
const jsonArr = '[{"id":1, "name":"Fay"}, {"id":2, "name":"Dax"}, {"id":3, "name":"Oli"}]';

const jsonData = JSON.parse(jsonArr);

console.log(jsonData);

for (let i = 0; i < jsonData.length; i++) {
    console.log(jsonData[i].id + " " + jsonData[i].name);
}


  
  
/*
run:
  
[
  { id: 1, name: 'Fay' },
  { id: 2, name: 'Dax' },
  { id: 3, name: 'Oli' }
]
1 Fay
2 Dax
3 Oli
  
*/

 



answered May 24, 2022 by avibootz
0 votes
const jsonArr = '{"workers": [{"id":1, "name":"Fay"}, {"id":2, "name":"Dax"}, {"id":3, "name":"Oli"}]}';

const jsonData = JSON.parse(jsonArr);

console.log(jsonData);

for (let i = 0; i < jsonData.workers.length; i++) {
    console.log(jsonData.workers[i].id + " " + jsonData.workers[i].name);
}


  
  
/*
run:
  
{
  workers: [
    { id: 1, name: 'Fay' },
    { id: 2, name: 'Dax' },
    { id: 3, name: 'Oli' }
  ]
}
1 Fay
2 Dax
3 Oli
  
*/

 



answered May 24, 2022 by avibootz
0 votes
const jsonArr = '{"dp": {"workers": [{"id":1, "name":"Fay"}, {"id":2, "name":"Dax"}, {"id":3, "name":"Oli"}]}}';

const jsonData = JSON.parse(jsonArr);

console.log(jsonData);

for (let i = 0; i < jsonData.dp.workers.length; i++) {
    console.log(jsonData.dp.workers[i].id + " " + jsonData.dp.workers[i].name);
}


  
  
/*
run:
  
{ dp: { workers: [ [Object], [Object], [Object] ] } }
1 Fay
2 Dax
3 Oli
  
*/

 



answered May 24, 2022 by avibootz

Related questions

1 answer 112 views
2 answers 146 views
1 answer 133 views
2 answers 224 views
224 views asked Feb 27, 2020 by avibootz
3 answers 377 views
1 answer 142 views
...