'How do I get JSON data and load it in a JavaScript file? (node.js)

Oh? Hi there!

So, I had a little problem recently.

I'm probably too dumb to realize how to do this, but here it is. I need some sort of way to actually read JSON files. I am requiring them, but I have no idea how to actually use them.

Here is my JSON file:

{
    "help": "Displays this message"
}

And my code here:

const cmdh = require("../cmdhandle");
const Discord = require("discord.js");
const util = require("../../util");
const json = require("../help.json");

module.exports = {
    aliases: ["?"],
    execute:function(msg, cmd) {
        // Insert code here so that "def" is a list of elements and definitions in the json file
        msg.channel.send(util.genEmbed("Help", def, "#ff5c5c"));
    }
}

Edit: ;p i was an idiot back then, looks like this was wayyy easier than i thought lol



Solution 1:[1]

const cmdh = require("../cmdhandle");
const Discord = require("discord.js");
const util = require("../../util");
const json = require("../help.json");

module.exports = {
    aliases: ["?"],
    execute:function(msg, cmd) {
        // Get all entires
        Object.entries(json).forEach(([key, msg]) => {
            msg.channel.send(util.genEmbed(key, msg, "#ff5c5c"));
        })
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xdeepakv