json 如何使用jq为jira问题子字段重新赋值

2cmtqfgy  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(157)

我在json文件中更改子字段的值时遇到了困难。json文件是一个jira问题字段的提取(其中一些字段)。
下面是我的json的一个子集的结构:

{
  "fields": {
    "summary": "",
    "issuetype": {
      "self": "",
      "id": "",
      "description": "",
      "iconUrl": "",
      "name": "",
      "subtask": true,
      "avatarId": 17116
    }
  }
}

我可以使用以下命令重新分配像summary这样的字段:

jq --arg field "$field" --arg value "$value" '.fields[$field] = $value' issue_fields.json > tmp.json && mv tmp.json issue_fields.json

但是我在issuetype字段中更改像name这样的子字段时遇到了麻烦。

jq --arg field "$field" --arg value "$value" --arg subfield "$subfield" '.fields[$field][$subfield] = $value' issue_fields.json > tmp.json && mv tmp.json issue_fields.json

我总是遇到这样的错误:Cannot index array with string "name"

wooyq4lh

wooyq4lh1#

看看你的代码,一切看起来都很好,但正如knittl提到的,你可能有你的值设置不正确。当我第一次学习json和jq时,我挣扎着学习语法,因为它可能是喜怒无常的,直到你摸索到模式(我仍然没有完全-大多数都没有哈哈)。幸运的是,man jq有很多好的信息,互联网上有一些很棒的教程。
通过您提供的内容,我能够获得预期的结果-这里我想将summary字段更改为“hello world”,因此我设置了以下值:

field="summary"
value="hello world"

# check vars with:
echo "field: '$field', value: '$value'"

当我跑的时候,我得到了预期的结果:

jq --arg field "$field" --arg value "$value" '.fields[$field] = $value' issue_fields.json

当我想修改issuetype字段下的一个子字段时也是如此

field="issuetype"
subfield="name"
value="issue type hello world"

# check vars
echo "field: '$field', subfield: '$subfield', value: '$value'"

运行列出的修改子字段的命令,我得到了预期的结果。

$ jq --arg field "$field" --arg value "$value" --arg subfield "$subfield" '.fields[$field][$subfield] = $value' issue_fields.json

输出:

{
  "fields": {
    "summary": "",
    "issuetype": {
      "self": "",
      "id": "",
      "description": "",
      "iconUrl": "",
      "name": "issue type hello world",
      "subtask": true,
      "avatarId": 17116
    }
  }
}

如果变量设置不正确,我能够重现你的错误。想知道你是否忘记将field变量设置为issuetype,(也许它仍然设置为summary,从第一个命令,你正在搞乱)..(使用set -x之前的命令,以查看更多的调试,键入set +x后,以禁用额外的调试):

# change the field to a field we know doesn't have subfields
$ field="summary"

# show the var values
$ echo "field: '$field', subfield: '$subfield', value: '$value'"
=>field: 'summary', subfield: 'name', value: 'issue type hello world'

$ set -x
=> + set -x
# the field 'summary' has no subfield (named 'name') so it will fail
$ jq --arg field "$field" --arg value "$value" --arg subfield "$subfield" '.fields[$field][$subfield] = $value' issue_fields.json
=> + jq --arg field summary --arg value 'issue type hello world' --arg subfield name '.fields[$field][$subfield] = $value' issue_fields.json
=> jq: error (at issue_fields.json:14): Cannot index string with string "name"

# turn off the debugging
$ set +x

为了确保没有误导您,我精心制作了一个示例脚本,它使用case语句来确定是否要更改summary字段或issuetype下的子字段,并运行相应的命令。

myscript.sh内容:

#!/bin/bash

# set a filename to use
fname="issue_fields.json"

# populate a file with example contents
cat > "${fname}" << EOF
{
  "fields": {
    "summary": "",
    "issuetype": {
      "self": "",
      "id": "",
      "description": "",
      "iconUrl": "",
      "name": "",
      "subtask": true,
      "avatarId": 17116
    }
  }
}
EOF

# arg1: field.subfield
# arg2: value

# field is the first value (before the dot)
field=${1%%.*}
# subfield is the next value (after the dot)
subfield=${1##*.}
value=${2}

# ensure 2 args
if [[ $# -ne 2 ]]; then
  echo "$0 takes exactly 2 args. $0 <field|field.subfield> <value>"
  exit 1
fi

# check if the field is summary
# check if the field is summary
case "${field}" in
  summary)
    jq --arg field "$field" \
       --arg value "$value" \
         '.fields[$field] = $value' "${fname}"
    ;;
  *)
    jq --arg field "$field" \
       --arg value "$value" \
       --arg subfield "$subfield" \
         '.fields[$field][$subfield] = $value' "${fname}"
    ;;
esac

示例用法:

$ ./myscript.sh issuetype.name "issue type name value"
{
  "fields": {
    "summary": "",
    "issuetype": {
      "self": "",
      "id": "",
      "description": "",
      "iconUrl": "",
      "name": "issue type name value",
      "subtask": true,
      "avatarId": 17116
    }
  }
}

# set summary
$ ./aa.sh summary hello
{
  "fields": {
    "summary": "hello",
    "issuetype": {
      "self": "",
      "id": "",
      "description": "",
      "iconUrl": "",
      "name": "",
      "subtask": true,
      "avatarId": 17116
    }
  }
}

使用上面的方法,摆弄一下东西,把它弄到你想要的地方。
您还可以使用bash -x myscript.sh运行它,以查看调试输出,从而更直观地了解它是如何工作的。

相关问题