Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Update a Checkbox through the REST API

Antoine _Klee Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 17, 2022

Hello,

I wish to update a checkbox field with the Jira REST API but I do not manage to set the proper "data" array.

Basically the checkbox has two values, value1 and value2. I want to update those without overwriting what was previously entered, so if the field already has value1 and I want to had value2, then it will add it without removing value1. If I want to enter value1, then it will do nothing.

Calling the api on the field tells me this:

 curl -X GET --url "https://<site>.atlassian.net/rest/api/3/issue/IPP-150" 
--user '<email>:<token>' --header 'Accept: appli
cation/json' | jq '.fields.customfield_14841'
[
{
"self": "https://<site>.atlassian.net/rest/api/3/customFieldOption/13860",
"value": "value1",
"id": "13860"
},
{
"self": "https://<site>.atlassian.net/rest/api/3/customFieldOption/13861",
"value": "value2",
"id": "13861"
}
]
So I got the ids and values of each field.
Let's say my field now has only value1 and I want to add value 2.
Here is the curl call I do.
curl --request PUT --url 'https://<site>.atlassian.net/rest/api/3/issue/IPP-150?notifyUsers=false' --user '<email>:<token>' --header 'Accept: application/json' --header 'Content-Type: application/json' \
--data '{
"update": {
"customfield_14841": [
"add": [
{
"id": "13861"
}
]
]
}
}'

Unfortunately this results in the following error

{"errorMessages":["Can not instantiate value of type [simple type, class com.atlassia
n.jira.rest.api.issue.FieldOperation] from JSON String; no single-String constructor/
factory method (through reference chain: com.atlassian.jira.rest.v2.issue.IssueUpdate
Bean[\"update\"])"]}

If I add brackets surrounding the "add" key (as seen in some docs), I get the following one:

{"errorMessages":["There was an error parsing JSON. Check that your request body is v
alid."]}
How to properly write the update block?
Thanks.

2 answers

0 votes
Maxime Lefebvre _Okapya_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 1, 2022

Bonjour Antoine,

If you are using Checklist for Jira, I suggest you to use the edit operation since you wish to modify the value of an existing checklist item.

I am not sure if your issue is about how the JSON in interpreted, but I usually use a file pointer instead of JSON directly in the curl execution. Example:

curl -u user:password -X PUT --data "@update.json" -H "Content-Type: application/json" http://localhost/rest/api/2/issue/PROJ-10

 There's a file in the same folder named `update.json` that contains the JSON payload.

I hope this helps!

Regards

0 votes
Sunny Ape
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 28, 2022

Hello @Antoine _Klee Group_ 

You said you are trying to update a checkbox field, but you have provided a link to the REST API documentation for the third party add-on Checklist for Jira, which is a totally different thing.

Also, in your question, you said you wanted to update (alter) the existing options, but then you said you wanted to add (create) a new option, so it's not clear which of the two you meant

Assuming you really meant Jira's built-in checkbox fields and you just want to update (change) one or both of the options and not add (create) a new option, in your cURL example you are sending the PUT request to the wrong endpoint using the wrong method.

The correct v3 endpoint for Jira Cloud is Update custom field options endpoint:

PUT /rest/api/3/field/{fieldId}/context/{contextId}/option

and the JSON in the request body to update one or more existing options for a checkbox field would look something like this:

 

{
"options": [
   {
"id""12345",
"value""First checkbox option"
    },
    {
      "id""12346",
      "value""Second checkbox option"
    }
  ]
}
 

Suggest an answer

Log in or Sign up to answer