【发布时间】:2020-06-30 06:06:10
【问题描述】:
我正在尝试创建一个包含国家、地区、城市、组织、入侵尝试 IP 的 JSON 集合。
我的 JSON 测试信息:
[
{
"total": 0,
"country": [
{
"name": "CN",
"nr": 0,
"region": [
{
"name": "Beijing",
"nr": 0,
"City": [
{
"name": "Haidian",
"nr": 0,
"Organisation": [
{
"name": "AS45090 Shenzhen Tencent Computer Systems Company Limited",
"nr": 0,
"IPS": [
{
"192.144.207.22": 0
}
]
}
]
}
]
}
]
},
{
"name": "NL",
"nr": 0,
"region": [
{
"name": "Noord Holland",
"nr": 0,
"City": [
{
"name": "Amsterdam",
"nr": 0,
"Organisation": [
{
"name": "FEAS",
"nr": 0,
"IPS": [
{
"192.162.1.1": 0
}
]
}
]
}
]
}
]
}
]
}
]
我将现有的 json(测试)字符串加载到 $geoInfo。现在我正在尝试更改对象 where"name": "CN" 中的 nr 值
我已经测试了两种解决方案:
geoInfo="$( jq --arg country ${tmpGeo[0]} --arg count $count -r '.country | map( if .name == $country then . + { .nr=$count } else . end )'<<<"${geoInfo}" )"
和
geoInfo="$( jq --arg country ${tmpGeo[0]} --arg count $count -r '.country | select(.[].name == "CN") | .nr) = $count'<<<"${geoInfo}" )"
通过这两种解决方案,我得到:
jq: error (at <stdin>:1): Cannot index array with string "name"
我使用 jq 1.6 版。
出了什么问题?
【问题讨论】:
-
tmpGeo的内容是什么?count?向我们展示您期望达到的输出的确切输入
标签: arrays json string bash jq