Veda Playz
Veda Playz

Reputation: 11

How to post the JSON data in a embed in discord.js

I wrote a code and i got the data in a json format. Now i want it to convert that data into a discord embed so that when i enter a command . EX:- .data the json data to be sent from the URL.

Here is the input

request({
    url: "https://data.vatsim.net/v3/vatsim-data.json",
    json: true

}, (err, response, body) => {
    console.log(body)

})

The json data looks like this :

    {
      cid: 1435807,
      name: ' ',
      callsign: 'ZAP85LJ',
      flight_plan: [Object],
      last_updated: '2021-10-24T10:45:52.516736Z'
    },
    {
      cid: 1439854,
      name: ' ',
      callsign: 'DLH1ML',
      flight_plan: [Object],
      last_updated: '2021-10-24T10:46:13.4226778Z'
    }

Upvotes: 1

Views: 2554

Answers (1)

Bimoware
Bimoware

Reputation: 1362

You can use EmbedFields, you can use up to 25 fields on an embed. On this example I used .forEach so that for each element on the array I can create a field with its value:

request({
  url: "https://data.vatsim.net/v3/vatsim-data.json",
  json: true
}, (err, response, body) => {
      const embed = new Discord.MessageEmbed()
      const pilots = body.pilots.slice(0, 15)
      body.pilots.forEach(pilot => {
        embed.addField(p.name,
          `CID : ${p.cid}
Server : ${p.server}
etc...`)
      })
      message.channel.send(embed)
}

If you really want to show every single one of your elements on the array you can use discord-buttons to make the embed edit its message and change pages but for now this is the simplest and easiest solution for you.

Upvotes: 1

Related Questions