A Guide to Discord Bots
Storing Data Using JSON
A JSON editor, formatter and viewer.
Definition
JSON, JavaScript Object Notation, is a data formating which is lightweight and easy to use.
It became a standard in JavaScript.
When saving JSON data to a file, we can use *.json
.
Here's an example:
// This works
var obj = {name:"John", age:20}; //property: value
// But here's a more readable formatting
obj = {
name: "John",
age: 20
}
Note: in JSON, name: 'John'
is invalid while name: "John"
is valid.
Adding elements
var obj = {}; // An empty object
obj.name = "John";
obj.age = 20;
obj.credentials = {}; // A nested object
// Using variables
let property = "username";
let value = "john12";
obj.credentials[property] = value;
// this will set obj.credentials.password to the correct value
// obj.credentials.property will not work!
Setting elements
var obj = {
name: "John",
age: 20,
credentials: {
username: "john12",
password: "$2a$08$jT5j9kxru4/qUv4PItExa.sl579k0SvrqEU8SKniawfiitAC/6Ua2"
}
}
obj.name = "David";
obj.credentials.password = "password"
obj.credentials = {username: "david12"};
// this will remove everything and set "username" to "david12"
// Using variables
let property = "password";
let value = "David";
obj.credentials[property] = value;
// this will set obj.credentials.password to the correct value
// obj.credentials.property will not work!
// Final result:
/* {
name: "David",
age: 20,
credentials: {
username: "david12",
password: "$2a$08$yndOdxl7QgloOwTrHima/u0pf5/ZXq0N9mHlzqGLXg7UY4caZaZCS"
}
} */
Getting elements
var obj = {
name: "John",
age: 20,
credentials: {
username: "john12",
password: "$2a$08$jT5j9kxru4/qUv4PItExa.sl579k0SvrqEU8SKniawfiitAC/6Ua2"
}
}
console.log('Name: ' + obj.name);
console.log('Age: ' + obj.age);
console.log('Credentials: ' + obj.credentials);
console.log('Username: ' + obj.credentials.username);
console.log('Object: ' + obj);
// You can output objects directly to the console
Checking if an element exists
var obj = {
name: "John",
age: 20,
credentials: {
username: "john12",
password: "$2a$08$jT5j9kxru4/qUv4PItExa.sl579k0SvrqEU8SKniawfiitAC/6Ua2"
}
}
if (obj.hasOwnProperty("name"))
console.log('Name: ' + obj.name);
if (obj.hasOwnProperty("credentials"))
if (obj.credentials.hasOwnProperty("username"))
console.log('Username: ' + obj.credentials.username);
Removing elements
var obj = {
name: "John",
age: 20,
credentials: {
nickname: "John",
username: "john12",
password: "$2a$08$jT5j9kxru4/qUv4PItExa.sl579k0SvrqEU8SKniawfiitAC/6Ua2"
}
}
delete obj.age;
delete obj.credentials.username;
// Using variables
let property = "password";
delete obj.credentials[property];
// Final result:
/* {
name: "John",
credentials: {
nickname: "John"
}
} */
Getting an object's size
var obj = {
name: "John",
age: 20
}
console.log('Length: ' + Object.keys(obj).length);
// Length: 2
obj = {
name: "John",
age: 20,
credentials: {
nickname: "John",
username: "john12",
password: "$2a$08$jT5j9kxru4/qUv4PItExa.sl579k0SvrqEU8SKniawfiitAC/6Ua2"
}
}
console.log('Length: ' + Object.keys(obj).length);
// Length: 3
// Because Object.keys(obj) = [ 'name', 'age', 'credentials' ]
For loops
var obj = {
name: "John",
age: 20
};
for (let property in obj)
console.log(`${property}: ${obj[property]}`);
// Output:
// name: John
// age: 20
Writing JSON to a file
const fs = require('fs');
var obj = {
name: "John",
age: 20
};
fs.writeFileSync('./data.json', JSON.stringify(obj));
Reading a JSON file
const fs = require('fs');
var obj = JSON.parse(fs.readFileSync('./data.json'));