Getting Error "{"errorMessages":["There was an error parsing JSON. Check that your request body is valid."]}
curl --request POST
--url 'https://bankofloyal.atlassian.net/rest/api/2/issue'
--user aditya.singh@lji.io:$JIRA_TOKEN
--header 'Accept: application/json'
--header 'Content-Type: application/json'
--data '{
"fields": {
"project": {
"key": "'$JIRA_PROJECT'"
},
"parent": {
"key": "'$PARENT_ISSUE_KEY'"
},
"components": [
{
"id": "'$COMPONENT_ID'"
}
],
"summary": "Directories with migration changes: '"${DIR_JOINED}"'",
"description": "Name of models: '"${MODEL_NAMES}"'",
"issuetype": {
"name": "Sub-task"
}
}
}'
I wanted a ticket generated using the cURL but getting "JSON Parse error". While debugging, I found the problem is in the summary and description of the body, wrongly using the variables DIR_CHANGED
and MODEL_NAME
. Can you suggest some other ways?
This is too long for a comment. (Edit: I will get down voted to invisibility it seems, please give some recommendations to straighten myself up then. ) (This was never meant to farm upvotes, please consider this a comment and go upvote the answer up there instead.)
First things first. Please syntax highlight unless code block is trivial (short, not quote intensive, etc.) See: What is syntax highlighting and how does it work?. I wrote ```shell
, rather than ```
to start code block below. It is all green due to variables being green as well, that doesn’t help much.
I updated first few variable expansions for consistency.
This looks good, it was not an expansion issue to begin with.
curl --request POST
--url 'https://bankofloyal.atlassian.net/rest/api/2/issue'
--user "aditya.singh@lji.io:${JIRA_TOKEN}"
--header 'Accept: application/json'
--header 'Content-Type: application/json'
--data '{
"fields": {
"project": {
"key": "'"${JIRA_PROJECT}"'"
},
"parent": {
"key": "'"${PARENT_ISSUE_KEY}"'"
},
"components": [
{
"id": "'"${COMPONENT_ID}"'"
}
],
"summary": "Directories with migration changes: '"${DIR_JOINED}"'",
"description": "Name of models: '"${MODEL_NAMES}"'",
"issuetype": {
"name": "Sub-task"
}
}
}'
If you want to make sure, run an echo
of quoted data block. Better yet pipe it to jq
, that should give a pretty JSON output.
echo '
{
"fields": {
"project": {
"key": "'"${JIRA_PROJECT}"'"
},
"parent": {
"key": "'"${PARENT_ISSUE_KEY}"'"
},
"components": [
{
"id": "'"${COMPONENT_ID}"'"
}
],
"summary": "Directories with migration changes: '"${DIR_JOINED}"'",
"description": "Name of models: '"${MODEL_NAMES}"'",
"issuetype": {
"name": "Sub-task"
}
}
}' | jq
It might be better to dive into Jira Rest Api Exaples or ask on StackOverflow, as issue is not a BASH nor a Linux issue. I checked your flags, they match what is on Atlassian’s site, although with their long forms.
Addendum:
I had evaluated my strictly-quoted version of the command, so @Kusalananda is right on that there indeed could be a quotation error due to white space or special characters.
However, this error message is the response from the Atlassian server, curl did work properly. Better to use the quoted version though.
lso one Atlassian Community thread suggests that it could be the @
in the username.
Yet Another Addendum
curl would always work anyway, so long as credentials and api resource were correct. It must have been a new line or similar that pre-terminated the data block. Thus the error in the title. @Kusalananda was on point.
Not knowing the actual contents of your shell variables, I assume at least one contains a literal newline, tab, double quote, or possibly some other character that needs to be encoded in JSON documents. In general, avoid injecting shell strings into JSON. Use jq
to create valid JSON from shell variables. This ensures that the strings are properly encoded.
In the script fragment below, I assume the five shell variables used with jq
have already been set to their intended values. The script then creates a JSON document by giving the shell variables to jq
to insert at the appropriate places in the provided "template" as JSON-encoded strings. This result is assigned to the payload
shell variable, which is later used with curl
as the option argument of the --data
option.
payload=$(
jq -n -c
--arg project_key "$JIRA_PROJECT"
--arg parent_key "$PARENT_ISSUE_KEY"
--arg components_id "$COMPONENT_ID"
--arg dirs "$DIR_JOINED"
--arg models "$MODEL_NAMES" '
{
fields: {
project: { key: $project_key },
parent: { key: $parent_key },
components: [{ id: $components_id }],
summary: ("Directories with migration changes: " + $dirs),
description: ("Name of models: " + $models),
issuetype: { name: "Sub-task" }
}
}'
)
curl --request POST
--user "aditya.singh@lji.io:$JIRA_TOKEN"
--header 'Accept: application/json'
--header 'Content-Type: application/json'
--data "$payload"
'https://bankofloyal.atlassian.net/rest/api/2/issue'
Note that the variables, like $dirs
and $project_key
, used within the jq
expression, are jq
variables, not shell variables. These get their values from the --arg
option on the jq
command line.
The parenthesised expressions that are the values of the summary
and description
keys are parenthesised because they are expressions that need to be evaluated to get at their final values.
If you need to create jq
variables containing data that aren’t strings, for example, if you need to insert booleans, numbers, or JSON document fragments, then use --argjson
in place of --arg
(i.e. jq ... --argjson components_id "$COMPONENT_ID" ...
if the component’s ID is supposed to be a number rather than a string).