仅保留具有字符串值的字段
Leave only fields with string values
我有这样的 JSON 结构:
{
"1": "a-secret",
"A": "b-secret",
"2": {
"3": "ab-secret",
"4": {
"5": "adc-secret"
},
"6": {
"7": "abdc-secret"
}
}
}
我正在尝试为 return 个只有字符串值的密钥对创建一个命令(最好是一个衬垫)。所以对于上面的内容,它会 return:
{
"1": "a-secret",
"A": "b-secret"
}
我发现 .[]|strings
return 只是字符串值,但我需要键和值,这就是我难过的地方!
您正在寻找 map_values
.
$ jq 'map_values(strings)' file
{
"1": "a-secret",
"A": "b-secret"
}
作为替代解决方案,尤其是在 jq-1.6
之前的版本中,您可以使用 with_entries
来过滤类型为 string
的值
with_entries(select(.value | type == "string"))
我有这样的 JSON 结构:
{
"1": "a-secret",
"A": "b-secret",
"2": {
"3": "ab-secret",
"4": {
"5": "adc-secret"
},
"6": {
"7": "abdc-secret"
}
}
}
我正在尝试为 return 个只有字符串值的密钥对创建一个命令(最好是一个衬垫)。所以对于上面的内容,它会 return:
{
"1": "a-secret",
"A": "b-secret"
}
我发现 .[]|strings
return 只是字符串值,但我需要键和值,这就是我难过的地方!
您正在寻找 map_values
.
$ jq 'map_values(strings)' file
{
"1": "a-secret",
"A": "b-secret"
}
作为替代解决方案,尤其是在 jq-1.6
之前的版本中,您可以使用 with_entries
来过滤类型为 string
with_entries(select(.value | type == "string"))