使用 jq 遍历 JSON 对象
Loop through JSON object using jq
这是我的 JSON 对象数组的样子:
[
{
"Description": "Description 1",
"OutputKey": "OutputKey 1",
"OutputValue": "OutputValue 1"
},
{
"Description": "Description 2",
"OutputKey": "OutputKey 2",
"OutputValue": "OutputValue 2"
},
{
"Description": "Description 3",
"OutputKey": "OutputKey 3",
"OutputValue": "OutputValue 3"
},
{
"Description": "Description 4",
"OutputKey": "OutputKey 4",
"OutputValue": "OutputValue 4"
},
{
"Description": "Description 5",
"OutputKey": "OutputKey 5",
"OutputValue": "OutputValue 5"
},
{
"Description": "Description 6",
"OutputKey": "OutputKey 6",
"OutputValue": "OutputValue 6"
}
]
我如何使用 jq 对此进行迭代,以便我可以在其他命令中使用 OutputKey 和 OutputValue 的值?
恐怕你的问题不是很清楚。如果您想生成供 jq 以外的某些工具或应用程序使用的值,那么了解该工具的期望会很有帮助。如果你想使用 jq 本身的值,那么你可以使用 map
或 reduce
;或者,您可以使用具有以下形式的过滤器:.[] | ...
或 [.[] ...]
,其中 ...
是一些访问感兴趣值的 jq 代码,例如
[.[] | [.OutputKey, .OutputValue] ]
如果你想执行一些归约操作,那么你可能想使用以下形式:reduce .[] as $x (_; _)
还有其他选择,具体取决于您要执行的操作。
假设您的内容来自 in.json
:
#!/usr/bin/env bash
case $BASH_VERSION in (""|[123].*) echo "Bash 4.0 or newer required" >&2; exit 1;; esac
declare -A values=( ) descriptions=( )
while IFS= read -r description &&
IFS= read -r key &&
IFS= read -r value; do
values[$key]=$value
descriptions[$key]=$description
echo "Read key $key, with value $value and description $description" >&2
done < <(jq -r '.[] | (.Description, .OutputKey, .OutputValue)' <in.json)
根据您的输入,这会向 stderr 发出以下内容:
Read key OutputKey 1, with value OutputValue 1 and description Description 1
Read key OutputKey 2, with value OutputValue 2 and description Description 2
Read key OutputKey 3, with value OutputValue 3 and description Description 3
Read key OutputKey 4, with value OutputValue 4 and description Description 4
Read key OutputKey 5, with value OutputValue 5 and description Description 5
Read key OutputKey 6, with value OutputValue 6 and description Description 6
另外,这段代码在运行之后,你可以接着执行:
key_to_look_up="OutputKey 1"
echo "${values[$key_to_look_up]}"
echo "${descriptions[$key_to_look_up]}"
...并得到输出:
OutputValue 1
Description 1
这是我的 JSON 对象数组的样子:
[
{
"Description": "Description 1",
"OutputKey": "OutputKey 1",
"OutputValue": "OutputValue 1"
},
{
"Description": "Description 2",
"OutputKey": "OutputKey 2",
"OutputValue": "OutputValue 2"
},
{
"Description": "Description 3",
"OutputKey": "OutputKey 3",
"OutputValue": "OutputValue 3"
},
{
"Description": "Description 4",
"OutputKey": "OutputKey 4",
"OutputValue": "OutputValue 4"
},
{
"Description": "Description 5",
"OutputKey": "OutputKey 5",
"OutputValue": "OutputValue 5"
},
{
"Description": "Description 6",
"OutputKey": "OutputKey 6",
"OutputValue": "OutputValue 6"
}
]
我如何使用 jq 对此进行迭代,以便我可以在其他命令中使用 OutputKey 和 OutputValue 的值?
恐怕你的问题不是很清楚。如果您想生成供 jq 以外的某些工具或应用程序使用的值,那么了解该工具的期望会很有帮助。如果你想使用 jq 本身的值,那么你可以使用
map
或reduce
;或者,您可以使用具有以下形式的过滤器:.[] | ...
或[.[] ...]
,其中...
是一些访问感兴趣值的 jq 代码,例如[.[] | [.OutputKey, .OutputValue] ]
如果你想执行一些归约操作,那么你可能想使用以下形式:
reduce .[] as $x (_; _)
还有其他选择,具体取决于您要执行的操作。
假设您的内容来自 in.json
:
#!/usr/bin/env bash
case $BASH_VERSION in (""|[123].*) echo "Bash 4.0 or newer required" >&2; exit 1;; esac
declare -A values=( ) descriptions=( )
while IFS= read -r description &&
IFS= read -r key &&
IFS= read -r value; do
values[$key]=$value
descriptions[$key]=$description
echo "Read key $key, with value $value and description $description" >&2
done < <(jq -r '.[] | (.Description, .OutputKey, .OutputValue)' <in.json)
根据您的输入,这会向 stderr 发出以下内容:
Read key OutputKey 1, with value OutputValue 1 and description Description 1
Read key OutputKey 2, with value OutputValue 2 and description Description 2
Read key OutputKey 3, with value OutputValue 3 and description Description 3
Read key OutputKey 4, with value OutputValue 4 and description Description 4
Read key OutputKey 5, with value OutputValue 5 and description Description 5
Read key OutputKey 6, with value OutputValue 6 and description Description 6
另外,这段代码在运行之后,你可以接着执行:
key_to_look_up="OutputKey 1"
echo "${values[$key_to_look_up]}"
echo "${descriptions[$key_to_look_up]}"
...并得到输出:
OutputValue 1
Description 1