Search using a keyword. DISCORDJS JSON

Hello all, today I need some help with a search command!

I want to grab a name and waifuURL when someone specifys a specific keyword(The waifus name).
This is how the json is formatted:

{
    "waifuID": {
        "1": {
            "names": "Megumin",
            "img_url": "https://s4.anilist.co/file/anilistcdn/character/large/b89361-xH1t0q5WeAN8.png",
            "claims": "#1"
        },
        "2": {
            "names": "Mai Sakurajima",
            "img_url": "https://s4.anilist.co/file/anilistcdn/character/large/b127222-IY5iDRuXLY8i.png",
            "claims": "#2"
        },
        "3": {
            "names": "Zero Two",
            "img_url": "https://s4.anilist.co/file/anilistcdn/character/large/b124381-pkTKi6HHNuVR.png",
            "claims": "#3"
        }
}
}

I want to be able to do !search and it would give the image url + the name of the waifu.
EX.
Input: !search Zero Two

Response:

Zero Two

img_url.

Input: !search Megumin

Megumin

img_url

(These responses are in an embed)

Thats the gist of it, thanks!

Are these stored in a database? If so, you can use something like

var search = message.slice.whatever.whatever
col.find({"names"}: search }).toArray.then(x => message.send "x")
//please don't copy this, it's absolutely terrible 

If this is a local JSON file, check out this link: https://stackoverflow.com/questions/49221446/search-in-json-file-with-node-js

It’ll hopefully give you some ideas to solve your problem.

Good luck with this bot! I really admire people who can build discord bots because I was never able to do it, haha.

Thanks so much!

1 Like

If you don’t mind, could you mark my response as “solution” ?

3 Likes

I’ve found a way better way to do this.

console.log(Object.values(obj.waifuID)
  .find(value => value.names === "Zero Two").img_url)

(The value of obj is the actual JSON database.)