Generation

generate functionSat, 17 Dec 2022

read local file of json. split the json list into 6 lists and output to 6 files

const fs = require('fs') const jsonString = fs.readFile('./file.txt', 'utf8', function (err, data) { if (err) { console.log("Error reading file from disk:", err) return } try { const jsonObject = JSON.parse(data) const props = ['b', 'c', 'd', 'e', 'f', 'g'] props.forEach(prop => { const newObject = { [prop]: jsonObject[prop] } fs.writeFile(`./${prop}.txt`, JSON.stringify(newObject), err => { if (err) { console.log('Error writing file', err) } else { console.log(`Successfully wrote file ${prop}.txt`) } }) }) } catch(err) { console.log('Error parsing JSON string:', err) } })

Javascript
Generate More

Questions about programming?Chat with your personal AI assistant