仅保留具有字符串值的字段 [英] Leave only fields with string values
本文介绍了仅保留具有字符串值的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的JSON结构:
I have a JSON structure like this:
{
"1": "a-secret",
"A": "b-secret",
"2": {
"3": "ab-secret",
"4": {
"5": "adc-secret"
},
"6": {
"7": "abdc-secret"
}
}
}
我正在尝试创建一个命令(最好是一个划线员)以返回仅具有字符串值的密钥对.因此,对于以上内容,它将返回:
I am trying to create a command (preferable one liner) to return key pairs that only have string values. So for the above it would return:
{
"1": "a-secret",
"A": "b-secret"
}
我发现.[]|strings
仅返回字符串值,但是我既需要键又需要值,这就是我的绊脚石!
I have found .[]|strings
that returns only the string values but I need both the key and value and that's where I'm stumped!
推荐答案
You are looking for map_values
.
$ jq 'map_values(strings)' file
{
"1": "a-secret",
"A": "b-secret"
}
这篇关于仅保留具有字符串值的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文