i have 2 json objects inside large .txt files in electron enviroment. What's the proper way of:
- reading first json object in file
- ignoring the second
- looping this behaviour for each file in asynchronous way
This is how i'm accessing files with 1 json objects inside:
async readFile() {
try{
await fs.promises.access(filename, fs.constants.F_OK)
try {
const data = await fs.promises.readFile(`${dir}/${filename}`, 'utf8')
let obj = JSON.parse(data)
return obj
}
catch(err) {
console.log(err)
}
} catch (err) {
console.log("File Doesn\'t Exist. Creating new file.")
fs.writeFile(filename, '', err => { err ? console.log(err) : null })
}
}
I thought about reading it line by line and checking for first ':' character and '}' character, parsing content between these lines and closing file. However, it won't work with nested objects.