Jpa*_*WPD 23 bash ubuntu array shell-script json
我有一个 JSON 输出,其中包含存储在变量中的对象列表。(我可能措辞不对)
[
{
"item1": "value1",
"item2": "value2",
"sub items": [
{
"subitem": "subvalue"
}
]
},
{
"item1": "value1_2",
"item2": "value2_2",
"sub items_2": [
{
"subitem_2": "subvalue_2"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
我需要数组中 item2 的所有值,以便在 ubuntu 14.04.1 上运行 bash 脚本。
我找到了很多方法来将整个结果放入一个数组中,而不仅仅是我需要的项目
Gil*_*not 26
使用jq:
$ cat json
[
{
"item1": "value1",
"item2": "value2",
"sub items": [
{
"subitem": "subvalue"
}
]
},
{
"item1": "value1_2",
"item2": "value2_2",
"sub items_2": [
{
"subitem_2": "subvalue_2"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
arr=( $(jq -r '.[].item2' json) )
printf '%s\n' "${arr[@]}"
Run Code Online (Sandbox Code Playgroud)
value2
value2_2
Run Code Online (Sandbox Code Playgroud)
以下实际上是错误的:
# BAD: Output line of * is replaced with list of local files; can't deal with whitespace
arr=( $( curl -k "$url" | jq -r '.[].item2' ) )
Run Code Online (Sandbox Code Playgroud)
如果您有 bash 4.4 或更高版本,则可以使用世界上最好的选项:
# BEST: Supports bash 4.4+, with failure detection and newlines in data
{ readarray -t -d '' arr && wait "$!"; } < <(
set -o pipefail
curl --fail -k "$url" | jq -j '.[].item2 | (., "\u0000")'
)
Run Code Online (Sandbox Code Playgroud)
...而使用 bash 4.0,您可以以失败检测和文字换行支持为代价获得简洁:
# OK (with bash 4.0), but can't detect failure and doesn't support values with newlines
readarray -t arr < <(curl -k "$url" | jq -r '.[].item2' )
Run Code Online (Sandbox Code Playgroud)
...或 bash 3.x 兼容性和故障检测,但不支持换行:
# OK: Supports bash 3.x; no support for newlines in values, but can detect failures
IFS=$'\n' read -r -d '' -a arr < <(
set -o pipefail
curl --fail -k "$url" | jq -r '.[].item2' && printf '\0'
)
Run Code Online (Sandbox Code Playgroud)
...或 bash 3.x 兼容性和换行支持,但没有故障检测:
# OK: Supports bash 3.x and supports newlines in values; does not detect failures
arr=( )
while IFS= read -r -d '' item; do
arr+=( "$item" )
done < <(curl --fail -k "$url" | jq -j '.[] | (.item2, "\u0000")')
Run Code Online (Sandbox Code Playgroud)
用于jq生成您评估的 shell 语句:
eval "$( jq -r '@sh "arr=( \([.[].item2]) )"' file.json )"
Run Code Online (Sandbox Code Playgroud)
给定问题中的 JSON 文档,调用jq将生成字符串
arr=( 'value2' 'value2_2' )
Run Code Online (Sandbox Code Playgroud)
然后由您的 shell 对其进行评估。arr计算该字符串将创建包含两个元素value2和的命名数组value2_2:
$ eval "$( jq -r '@sh "arr=( \([.[].item2]) )"' file.json )"
$ printf '"%s"\n' "${arr[@]}"
"value2"
"value2_2"
Run Code Online (Sandbox Code Playgroud)
运算@sh符 injq会注意正确引用 shell 的数据。
或者,将该arr=( ... )部分移出表达式jq:
eval "arr=( $( jq -r '@sh "\([.[].item2])"' file.json ) )"
Run Code Online (Sandbox Code Playgroud)
现在,jq仅生成引用的元素列表,然后将其插入arr=( ... )并求值。
如果需要从curl命令中读取数据,请在上面的命令中使用curl ... | jq -r ...代替。jq -r ... file.json
| 归档时间: |
|
| 查看次数: |
65975 次 |
| 最近记录: |