JDA: How to create MessageEmbed from JSON file?

Viewed 597

I want to send two embeds in one message. This should be doable with MessageChannel#sendMessageEmbeds(java.util.Collection).
Also, I have embed as JSON file like this:

{
    "author": {
      "name": "I use this as title because why not",
      "icon_url": "url for icon"
    },
    "description": "description that goes top of the embed",
    "color": 65535,
    "fields": [
      {
        "name": "field name 01",
        "value": "field value 01 which *can* ||have|| __some__ markdown stuff",
        "inline": false
      },
      {
        "name": "field name 02",
        "value": "yes, thie fileds can have more than one filed",
        "inline": false
      },
      {
        "name": "field name 03",
        "value": "blahblah some texts",
        "inline": true
      },
    ],
    "thumbnail": {
      "url": "url for thumbnail"
    },
    "footer": {
      "text": "footer text because it is cool",
      "icon_url": "this would be same as author icon_url but this may vary sometimes"
    }
  }

The problem is, I cannot think of 'how to convert JSON file to MessageEmbed object'. I asked in JDA discord server, searched Google for some hint, but still cannot think of proper way to do it.

Long story short, I have JSON file for embed and I want to convert JSON file to MessageEmbed object so that I can sent it to Discord channel.

1 Answers

maybe there's a built-in way to convert JSON to MessageEmbeds and if so then I wasted my time, I wasn't able to find one myself.

Below is a method that takes a JsonObject and converts its valid members to a MessageEmbed with all the categories you listed in your Json and title because I noticed you didn't add it. If you want to add more you're welcome to use it and do so.

Note: I used Google's GSON library to do this, if you want the method I used to work you must add it to your project. To read more about it see here

Method Code:

/**
 * Converts a {@link JsonObject} to {@link MessageEmbed}.
 * Supported Fields: Title, Author, Description, Color, Fields, Thumbnail, Footer.
 * 
 * @param json The JsonObject
 * @return The Embed
 */
public static MessageEmbed jsonToEmbed(JsonObject json){
    EmbedBuilder embedBuilder = new EmbedBuilder();

    JsonPrimitive titleObj = json.getAsJsonPrimitive("title");
    if (titleObj != null){ // Make sure the object is not null before adding it onto the embed.
        embedBuilder.setTitle(titleObj.getAsString());
    }

    JsonObject authorObj = json.getAsJsonObject("author");
    if (authorObj != null) {
        String authorName = authorObj.get("name").getAsString();
        String authorIconUrl = authorObj.get("icon_url").getAsString();
        if (authorIconUrl != null) // Make sure the icon_url is not null before adding it onto the embed. If its null then add just the author's name.
            embedBuilder.setAuthor(authorName, authorIconUrl);
        else
            embedBuilder.setAuthor(authorName);
    }

    JsonPrimitive descObj = json.getAsJsonPrimitive("description");
    if (descObj != null){
        embedBuilder.setDescription(descObj.getAsString());
    }

    JsonPrimitive colorObj = json.getAsJsonPrimitive("color");
    if (colorObj != null){
        Color color = new Color(colorObj.getAsInt());
        embedBuilder.setColor(color);
    }

    JsonArray fieldsArray = json.getAsJsonArray("fields");
    if (fieldsArray != null) {
        // Loop over the fields array and add each one by order to the embed.
        fieldsArray.forEach(ele -> {
            String name = ele.getAsJsonObject().get("name").getAsString();
            String content = ele.getAsJsonObject().get("value").getAsString();
            boolean inline = ele.getAsJsonObject().get("inline").getAsBoolean();
            embedBuilder.addField(name, content, inline);
        });
    }

    JsonPrimitive thumbnailObj = json.getAsJsonPrimitive("thumbnail");
    if (thumbnailObj != null){
        embedBuilder.setThumbnail(thumbnailObj.getAsString());
    }

    JsonObject footerObj = json.getAsJsonObject("footer");
    if (footerObj != null){
        String content = footerObj.get("text").getAsString();
        String footerIconUrl = footerObj.get("icon_url").getAsString();

        if (footerIconUrl != null)
            embedBuilder.setFooter(content, footerIconUrl);
        else
            embedBuilder.setFooter(content);
    }

    return embedBuilder.build();
}

How to use:

textChannel.sendMessageEmbeds(jsonToEmbed((JsonObject) JsonParser.parseReader(new FileReader("jsonfile.json")))).queue();

What it prints using my test JSON: enter image description here

Related