You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用CURL创建JIRA子任务遇JSON解析错误求解决方案

解决JIRA API创建子任务时的JSON解析错误

你的问题核心是变量$DIR_JOINED和$MODEL_NAMES中可能包含特殊字符(比如双引号、反斜杠、换行),直接拼接进JSON会破坏语法结构,导致解析失败。以下是几个可靠的解决方法:

方法1:用printf构造合法JSON

printf会自动处理变量中的特殊字符转义,确保生成的JSON格式合规:

# 构造JSON payload
JSON_PAYLOAD=$(printf '{
  "fields": {
    "project": { "key": "%s" },
    "parent": { "key": "%s" },
    "components": [ { "id": "%s" } ],
    "summary": "Directories with migration changes: %s",
    "description": "Name of models: %s",
    "issuetype": { "name": "Sub-task" }
  }
}' "$JIRA_PROJECT" "$PARENT_ISSUE_KEY" "$COMPONENT_ID" "$DIR_JOINED" "$MODEL_NAMES")

# 发起请求
curl --request POST \
  --url 'https://bankofloyal.atlassian.net/rest/api/2/issue' \
  --user user_email:"$JIRA_TOKEN" \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data "$JSON_PAYLOAD"

方法2:用jq工具生成JSON(推荐)

jq是专门处理JSON的工具,能彻底避免格式错误,适合复杂场景:

# 用jq生成标准化JSON
JSON_PAYLOAD=$(jq -n \
  --arg project "$JIRA_PROJECT" \
  --arg parent "$PARENT_ISSUE_KEY" \
  --arg component "$COMPONENT_ID" \
  --arg dirs "$DIR_JOINED" \
  --arg models "$MODEL_NAMES" \
  '{
    fields: {
      project: { key: $project },
      parent: { key: $parent },
      components: [ { id: $component } ],
      summary: "Directories with migration changes: \($dirs)",
      description: "Name of models: \($models)",
      issuetype: { name: "Sub-task" }
    }
  }')

# 发起请求
curl --request POST \
  --url 'https://bankofloyal.atlassian.net/rest/api/2/issue' \
  --user user_email:"$JIRA_TOKEN" \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data "$JSON_PAYLOAD"

方法3:手动转义变量特殊字符(不推荐)

如果无法使用前两种工具,可以手动转义变量中的双引号和反斜杠:

# 转义变量中的特殊字符
ESCAPED_DIR=$(echo "$DIR_JOINED" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g')
ESCAPED_MODELS=$(echo "$MODEL_NAMES" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g')

# 发起请求
curl --request POST \
  --url 'https://bankofloyal.atlassian.net/rest/api/2/issue' \
  --user user_email:"$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: '"$ESCAPED_DIR"'",
      "description": "Name of models: '"$ESCAPED_MODELS"'",
      "issuetype": { "name": "Sub-task" }
    }
  }'

内容的提问来源于stack exchange,提问作者Abhishek Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 08:31:01