[英]Convert a nested JSON of objects into array into a bash array using jq
I am doing something fundamentally wrong but just can't see what, could some kind person point out my fault with jq or JSON here? 我在做一些根本上是错误的事情,但看不到是什么,有人可以在这里指出我的jq或JSON错误吗?
I have the the following child objects contained within an array “ entries
” 我在数组“ entries
”中包含以下子对象
{
"profile": {
"name": "TesterRun1",
"download": {
"entries": [{
"ENTRY_A": "testserver1_place_com",
"store": "A",
"type": "direct"
},
{
"ENTRY_B": "testserver2_anotherplace_com",
"store": "B",
"type": "bypass"
},
{
"ENTRY_B": "testserver2_anotherplace_com",
"store": "A",
"type": "bypass"
}
]
}
}
}
I wish to convert these to an array accessible by bash via the jq function “ to_entries
” using the below query but so far nothing! 我希望使用以下查询将它们转换为可以通过jq函数“ to_entries
”通过bash访问的数组,但到目前为止没有任何内容!
jq 'to_entries|.[]|.profile.download.entries|select(.store=="A")|.[]'
You can see here that nothing is returned on JQ Play - enter link description here 您可以在此处看到JQ Play上没有返回任何内容- 在此处输入链接描述
Please help save my sanity, what am I doing wrong 请帮助保存我的理智,我在做什么错
to_entries
has nothing whatsoever to do with exposing JQ results to bash. to_entries
与将JQ结果暴露给bash无关。 Rather, it takes each entry in a JSON object and emits a {"key": key, "value": value}
pair. 而是,它接受JSON对象中的每个条目并发出一个{"key": key, "value": value}
对。
That can be useful, if you want to identify and extract arbitrary keys. 如果您要标识和提取任意键,这可能会很有用。 For example: 例如:
#!/usr/bin/env bash
jq_script='
.profile.download.entries[]
| select(.store == "A")
| to_entries[]
| select(.key != "store")
| select(.key != "type")
| [.key, .value]
| @tsv
'
declare -A array=( )
while IFS=$'\t' read -r key value; do
array[$key]=$value
done < <(jq -r "$jq_script")
# print array output
declare -p array
...will, when given your input on stdin, emit (albeit on a single line, without the whitespace changes): ...将在您在stdin上提供输入时发出(尽管仅一行,而空白没有更改):
declare -A array=([ENTRY_A]="testserver1_place_com"
[ENTRY_B]="testserver2_anotherplace_com" )
...which I assume , for lack of any better description in the question, is what you actually want. ...由于缺乏任何更好的描述,我认为这是您真正想要的。
Here is a slightly different approach (with some cleaned-up data) which captures the output from jq
into separate column arrays. 这是一种略有不同的方法(带有一些清理的数据),该方法将jq
的输出捕获到单独的列数组中。
#!/bin/bash
data='{
"profile": {
"name": "TesterRun1",
"download": {
"entries": [
{
"entry": "testserver1_place_com",
"store": "A",
"type": "direct"
},
{
"entry": "testserver2_anotherplace_com",
"store": "B",
"type": "bypass"
},
{
"entry": "testserver2_anotherplace_com",
"store": "A",
"type": "bypass"
}
]
}
}
}'
filter='
.profile.download.entries[]
| select(.store == "A")
| .entry, .store, .type
'
declare -a ENTRY
declare -a STORE
declare -a TYPE
i=0
while read -r entry; read -r store; read -r type; do
ENTRY[$i]="$entry"
STORE[$i]="$store"
TYPE[$i]="$type"
i=$((i + 1))
done < <(jq -Mr "$filter" <<< "$data")
declare -p ENTRY STORE TYPE
Output 输出量
declare -a ENTRY='([0]="testserver1_place_com" [1]="testserver2_anotherplace_com")'
declare -a STORE='([0]="A" [1]="A")'
declare -a TYPE='([0]="direct" [1]="bypass")'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.