0

I want to save a file containing request body in json and use it as in input for my nodejs application which makes api call to a soap service. I also want to save the response body in to mongodb. I am using the Node.js fs module for this. Can you please suggest how to loop through the json file using the fs module?

Here is how my file looks like

[{ 
    "name": "Sara",
    "age": 23,
    "gender": "Female",
    "department": "History",
    "car": "Honda"
},{}
]

Here is what I have for a starter.

'use strict';

const fs = require('fs');

let rawdata = fs.readFileSync('student.json');  
let student = JSON.parse(rawdata);  
console.log(student.name); 
temesgen
  • 161
  • 1
  • 2
  • 8
  • `student.forEach(student => console.log(student.name))` – Felix Kling Jun 12 '18 at 00:29
  • I am particularly interested in how looping works from a saved file using the fs module. Can you please provide solution using fs module? – temesgen Jun 12 '18 at 00:40
  • `fs` doesn't offer such functionality. You read the data, parse it (like you have already done) and loop over it just like any other array. Or are you looking for a streaming JSON parser? – Felix Kling Jun 12 '18 at 00:55

0 Answers0