Unexpected End of Json Input (Json Files cleared in Glitch Project) (Discord Bot)

An interesting one. I have a Discord Bot with a Levels/XP System. It works absolutely fine but at some point. My XP Json File gets cleared for no reason and it gives me the Unexpected End of Json Input.

Code:

let xpAdd = Math.floor(Math.random() * 8) + 2;
  console.log(xpAdd)

if (!xp[message.author.id]) {
    xp[message.author.id] = {
        xp: 0,
        level: 1
    };
  if (charactis_talked_users.has(message.author.id)) return;
  
}
  
  if(message.author.bot) return

let curxp = xp[message.author.id].xp;
let curlvl = xp[message.author.id].level;
let nxtLvl = xp[message.author.id].level * 680;
xp[message.author.id].xp = curxp + xpAdd;
if (nxtLvl <= xp[message.author.id].xp) {
    xp[message.author.id].level = curlvl + 1;
    let lvlup = new Discord.RichEmbed()
        .setAuthor('Level 🆙', 'https://i.imgur.com/s6OpKNC.jpg')
        .setDescription(`**${message.author.username}** has Levelled up!`)
        .setThumbnail(message.author.displayAvatarURL)
        .setColor("#ddff00")
        .addField("**New Level**", `${curlvl + 1}`)
        .setFooter(`$120 Earned! | Run n!level or n!profile to check your progress!`)

    message.channel.send(lvlup).then(msg => {
          db.add(`userBalance_${message.author.id}`, 120)
      
    });
}
fs.writeFile("./database/xp.json", JSON.stringify(xp, null, 2), (err) => {
            if (err) console.log(err)

error

I think its something to do with the fs.writeFile. If anyone can give me a solution to this. That would be great.
Project: nate-bot

1 Like

can you please copy paste the json file and all information listed at the end of the process run>

to make it simpler, just tell me the console output.

Its a pretty big JSON file: here it is https://paste.ofcode.org/34b8hfZit4mQUp7HceGUR4m

The Console Output is it Unexpected end of JSON input

I wasn’t able to get all Console information yet. Will update this post when i see it!

1 Like

i will be on all night, i will watch :wink:

1 Like

However, would you have any idea what would be causing this JSON problem?

For temporary solution. I have to clear all JSON data in that file

… i think glitch may just be having a huge fit over writing to a json file, or the file is too large.
either way, i think sql should be a better bet to use while i figure the whole issue out

const servers = JSON.parse(fs.readFileSync("./servers.json"))

I saw on another Forum post that they used this as a solution. Im not sure if it will rectify this json issue on my bot.

SQL or MongoDB should be better than writing all that data into a json file.

For non relational data and easy to use I recommend you Mongo for your database

1 Like

Alright thanks but would it be possible if it can be stored on SQLITE? thanks

1 Like