user13917978
user13917978

Reputation:

How i will change the value in the string in json file?

I'm trying to change the value of a string in a JSON file, but it is not working like I need it to.

Example JSON file:

{'user1':{"name":"example","age":32,"coins":16}}

I've tried to use this:

let fs = require('fs')
let file = fs.readFileSync('./example.json','utf-8')
file.user1.name = "example example"
fs.writeFileSync('./example.json',JSON.stringify(file,null,1))

It doesn't return any errors, but it leaves the JSON file something like this:

{'user1':{'name':'example','age':32,'coins':16}},
  "name": "example example"
 }
}

and this JSON file updates every message in discord (yea, this is for discord bot ^_^)

Upvotes: 0

Views: 23

Answers (1)

antonku
antonku

Reputation: 7675

You should use require to load json modules because it parses JSON automatically:

const fs = require('fs')
const file = require('./example.json');
file.user1.name = "example example";
fs.writeFileSync('./example.json',JSON.stringify(file,null,1))

Upvotes: 1

Related Questions