简体   繁体   中英

Error Unexpected end of JSON input when looking at changes

So what i'm trying to do is getting what has been modified in a JSON file and the exact path to it. The thing is, that JSON file is getting modifed by another program. Everytime i run my second program modifying my JSON file, i am getting the following error. Do anybody know why would this happen and has a fix ? (Thiss error is only printed when using the program modifying the JSON file. I can also tell you that the modifier program works perfectly fine.)

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at getCurrent (E:\letssee2\app\testor.js:5:31)
    at FSWatcher.fs.watch (E:\letssee2\app\testor.js:11:20)
    at emitTwo (events.js:126:13)
    at FSWatcher.emit (events.js:214:7)
    at FSEvent.FSWatcher._handle.onchange (fs.js:1364:12)

I have the following :

const fs = require('fs') 
const diff = require('deep-diff')

const filepath = '../temp/listings2.json' // File to watch
const getCurrent = () => JSON.parse(fs.readFileSync(filepath, {}))

let currObj = getCurrent()

fs.watch(filepath, {}, (eventType, filename) => {

const newObj = getCurrent()
const differences = diff(currObj, newObj)
var listings2 = JSON.parse(fs.readFileSync("../temp/listings2.json"))


if (differences == undefined) {
    return;
}

console.log(JSON.stringify(differences[0]["path"][0]))
console.log(JSON.stringify(differences[0]["path"][1]))
console.log(JSON.stringify(differences[0]["path"][2]))
console.log(JSON.stringify(differences[0]["path"][3]))

var path1 = String(differences[0]["path"][1])

//console.log(`\n\n${path1}\n\n`)

var fullpath = `${String(differences[0]["path"][0])}.${String(differences[0]["path"][1])}.${String(differences[0]["path"][2])}.${String(differences[0]["path"][3])}`

console.log(fullpath)

console.log(listings2[(differences[0]["path"][0])][differences[0]["path"][1]][differences[0]["path"][2]][differences[0]["path"][3]])

currObj = newObj
})

Assuming that the second program is writing the file correctly:

Is it possible that the modification event is triggered during modification rather than after the second program is finished writing the file?

If this is the case, you can be lazy and ignore the errors and wait for a proper modification to finish as long as you are absolutely certain the other program will be modifying the file correctly.

or

You could also go about "de-bouncing" the detection as well. more info on that here

fs.watch() and moreso fs.watchFile() can be tempermental to work with regardless due to the different ways OS's constitute a proper file change.

You should start debugging by just printing or echoing the JSON file and looking for any errors before the parse commands. Post the file contents if you would like me to take a look

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM