Ask coding questions

← Back to all posts
Get json file
Coder100 (16982)

I have successfully created a JSON database. Now how do I access it? I know that require() does not work*. Repl here.
*The error is:
SyntaxError: /home/runner/app/chat.json: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Object.Module._extensions..json (internal/modules/cjs/loader.js:673:27)
at Module.load (internal/modules/cjs/loader.js:566:32)
at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
at Function.Module._load (internal/modules/cjs/loader.js:498:3)
at Module.require (internal/modules/cjs/loader.js:598:17)
at require (internal/modules/cjs/helpers.js:11:18)
at evalmachine.<anonymous>:17:9
at Script.runInContext (vm.js:74:29)
at Object.runInContext (vm.js:182:6)

kpostal10 (31)

This is how I've done it in the past, and for your unexpected end of JSON input your file might not be setup correctly.
const guildProp = require("../../guildProperties.json");
var channelId=guildProp[].guildLogChannelId;
That is how you access stuff from it, its just like a normal json object, just stored in a file rather than a var (let) in the program
And to rite to it I make a simple json object
warn[] = {
warnamount: x+1,
rule : args2[0]
and then I do a fs.write to the file
fs.writeFile("./warn.json", JSON.stringify(warn, null, 4), err => {
if(err) throw err;

Maybe this can help you.