Quick question about JSON

Hello, Glitch Community!
Long time since last time I’ve posted anything here!

So, at the moment I have a massive project, that will require JSON databases. Though, I’ve heard that Glitch apparently doesn’t allow you to edit specific things in a JSON file.
Is this the case? And if not, then what am I supposed to use to edit information on a specific item, store information, and get information?

I am imagining, to get the information in JavaScript, it would be like

const db = require(`\databases\${message.guild.id}.json`);

if (db.server.premium === true) {
// Actions
}

Though, I am not sure, as I’ve never used JSON in the past.
If JSON is not possible to; edit information from specific items, get information from specific items, and create + store loads of information at once, we will have to move our project to a paid host before opening our bot to the public. However, if we can do those things, we will actually be able to keep on Glitch while it’s in the betas.

Thank you for your time.

Hi @Aprixia :slight_smile:

If you’d like to de-mystify JSON, https://json.org/ is an easy read and fully describes it.

In a nutshell, JSON is a text format which can represent structures (objects, lists, strings, etc).

A JSON file is a text file storing the string of JSON, for example package.json.

A JSON database is a database that can store and retrieve JSON strings. It is much easier to manage than dealing with file locks, etc.

This isn’t correct, a text file can be read, modified and saved. What you’ve likely heard is there can be corruption under certain conditions, if not handled well.

const db = require(`\databases\${message.guild.id}.json`);

require is used to import modules of code. To read JSON files, you’d use file functions the same as for any text file.

So, considering databases, if you have a look at Glitch: The friendly community where everyone builds the web, there are two JSON databases that can be hosted inside the Glitch project, lowdb and NeDB. The storage space will compete with your code space and other files.

An option for hosting outside of the Glitch project is MLAB’s MongoDB, which gives 500MB storage on their free tier. There is even a free course on FreeCodeCamp showing how to connect it all together.

How much data needs to be stored will influence your decision here, have you got some numbers for what you need in dev vs production?

2 Likes

Just for your information, if you are planning to create a new account for MongoDB, you need to create an account for MongoDB Atlas, as MLAB is now a part of MongoDB.

Here is the template that I’ve made for the JSON databases.

{
    "server": {
        "bans": {
            "id": {
                "ban": {
                    "endAt": "time",
                    "reason": "reason",
                    "bannedBy": "modid"
                },
                "ban": {
                    "endAt": "time",
                    "reason": "reason",
                    "bannedBy": "modid"
                }
            },
            "id": {
                "ban": {
                    "endAt": "time",
                    "reason": "reason",
                    "bannedBy": "modid"
                },
                "ban": {
                    "endAt": "time",
                    "reason": "reason",
                    "bannedBy": "modid"
                }
            }
        },
        "mutes": {
            "id": {
                "mute": {
                    "endAt": "time",
                    "reason": "reason",
                    "mutedBy": "modid"
                },
                "mute": {
                    "endAt": "time",
                    "reason": "reason",
                    "mutedBy": "modid"
                }
            },
            "id": {
                "mute": {
                    "endAt": "time",
                    "reason": "reason",
                    "mutedBy": "modid"
                },
                "mute": {
                    "endAt": "time",
                    "reason": "reason",
                    "mutedBy": "modid"
                }
            }
        },
        "kicks": {
            "id": {
                "kick": {
                    "reason": "reason",
                    "kickBy": "modid"
                },
                "kick": {
                    "reason": "reason",
                    "kickedBy": "modid"
                }
            },
            "id": {
                "kick": {
                    "reason": "reason",
                    "kickedBy": "modid"
                },
                "kick": {
                    "reason": "reason",
                    "kickedBy": "modid"
                }
            }
        },
        "warns": {
            "id": {
                "warn": {
                    "reason": "reason",
                    "warnedBy": "modid"
                },
                "warn": {
                    "reason": "reason",
                    "warnedBy": "modid"
                }
            },
            "id": {
                "warn": {
                    "reason": "reason",
                    "warnedBy": "modid"
                },
                "warn": {
                    "reason": "reason",
                    "warnedBy": "modid"
                }
            }
        },
        "configs": {
            "punishLogs": "true/false",
            "punishLogchannel": "id",
            "actionLogs": "true/false",
            "actionsLogsChannel": "id",
            "xprate": "number",
            "levelRewards": {
                "level": {
                    "rewards": {
                        "rolename": "id",
                        "rolename": "id"
                    }
                }
            },
            "premium": "true/false"
        },
        "levels": {
            "id": {
                "level": "level",
                "xp": "xp"
            },
            "id": {
                "level": "level",
                "xp": "xp"
            }
        }
    }
}

The file size is ~3.32KB.

Should fit easily into a project-hosted database, projects have diskspace of 200MB. :slight_smile:

One note: Remeber to use forward slashes / when specifying a path as Glitch uses Linux. I believe Node.js fixes it for you at runtime anyway but it’s kind of good practise.

i know a way to make it so it saves,

use fs.readfile on the file,

save it to a variable with JSON.parse,

and then when you are done manipulating it, write it back to the json file with JSON.stringify and fs