Anshul Singhal
Anshul Singhal

Reputation: 2201

How to convert Json (from JIRA's webhook) to Custom Java object

I have some Java REST APIs which will be invoked via JIRA Webhook configuration. Now, when JIRA webhook is invoking REST API, there are large no. of custom fields (like customfield_17270) which contain useful data.

For example, I have configured "Create Issue" event in JIRA webhook i.e. whenever any issue will be created in JIRA, my REST API will be invoked. While creating issue in JIRA, for example, there is a field named "Issue Title" whose value is "XXX". In JSON payload, ideally key-value pair should be "Issue Title":"XXX" but it is like "Custom_Field109":"XXX".

Now the problem is how to map this dynamic JSON to Java Object.

Is there anyone who has faced similar problem.

Upvotes: 2

Views: 1649

Answers (2)

Anshul Singhal
Anshul Singhal

Reputation: 2201

I was able to discuss this issue with JIRA internal team and they provided me custom fields mapping to their JIRA display name. Basically, when we receive Json keys like Custom_field109 then it means 109 is internal database id for this attribute.
Now, based on given mapping, I parsed JSON to get required keys and then through Jackson library, I was able to map JSOn to Java.

Upvotes: 0

dvdsmpsn
dvdsmpsn

Reputation: 2869

Each time you receive the webhook, you'll need to map each custom id (e.g. customfield_10070) to it's name by querying the field JIRA REST API at GET: /rest/api/2/field

...which will give you something like this:

[
    {
        "id": "issuetype",
        "name": "Issue Type",
        "custom": false,
        "orderable": true,
        "navigable": true,
        "searchable": true,
        "clauseNames": [
            "issuetype",
            "type"
        ],
        "schema": {
            "type": "issuetype",
            "system": "issuetype"
        }
    },
    {
        "id": "customfield_10070",
        "name": "FAQ Necessary?",
        "custom": true,
        "orderable": true,
        "navigable": true,
        "searchable": true,
        "clauseNames": [
            "cf[10070]",
            "FAQ Necessary?"
        ],
        "schema": {
            "type": "string",
            "custom": "com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons",
            "customId": 10070
        }
    },
    ...
]

You should then easily be able to iterate over the fields from the webhook JSON and map the custom field id to its display name.

Upvotes: 1

Related Questions