JQ 1.4如何同时使用竖线(|)和逗号(,)?
How to use pipe (|) and comma (,) together in JQ 1.4?
我在 Windows 64 位机器上使用 JQ 1.4。
下面是输入文件的内容(JSON.txt
)
{
"name": "Google",
"location":
{
"street": "1600 Amphitheatre Parkway",
"city": "Mountain View",
"state": "California",
"country": "US"
},
"employees":
[
{
"name": "Michael",
"division": "Engineering"
},
{
"name": "Laura",
"division": "HR"
},
{
"name": "Elise",
"division": "Marketing"
}
]
}
在输出中我希望看到两个结果 "Google"
和 "Laura"
我可以通过个人过滤器获得它们。
1) jq ."name" JSON.txt
"Google"
2) jq ."employees|map(select(.division==\"HR\"))"[0].name JSON.txt
"Laura"
当我使用逗号 ,
组合这两个过滤器时,我收到以下错误:
3) jq ."name",."employees|map(select(.division==\"HR\"))"[0].name JSON.txt
jq: error: Cannot iterate over string
null
"Laura"
有人可以帮我得到以下结果吗:
"Google"
"Laura"
在表达式的第二部分使用方括号,shorthand 关键过滤器语法如下:
jq ".name, (.employees|map(select(.division==\"HR\"))[0].name)" JSON.txt