我喜欢使用jq过滤json文件:
jq . some.json
给定json包含对象数组:
{
"theList": [
{
"id": 1,
"name": "Horst"
},
{
"id": 2,
"name": "Fritz"
},
{
"id": 3,
"name": "Walter"
},
{
"id": 4,
"name": "Gerhart"
},
{
"id": 5,
"name": "Harmut"
}
]
}
我想过滤该列表以仅显示id的值为2和4的元素,因此预期的输出为:
{
"id": 2,
"name": "Fritz"
},
{
"id": 4,
"name": "Gerhart"
}
如何使用jq过滤json?我玩过select和map,但是没有任何一个可以使用,例如:
$ jq '.theList[] | select(.id == 2) or select(.id == 4)' array.json
true
从文档:
jq '.[] | select(.id == "second")'
输入项
[{"id": "first", "val": 1}, {"id": "second", "val": 2}]
输出量
{"id": "second", "val": 2}
我认为您可以执行以下操作:
jq '.theList[] | select(.id == 2 or .id == 4)' array.json
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句