Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

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 94 views
2 answers 124 views
1 answer 108 views
2 answers 196 views
196 views asked Feb 27, 2020 by avibootz
3 answers 340 views
1 answer 123 views
...