![使用 jq 解析 JSON 以获取下面 JSON 中的名字](https://linux22.com/image/160920/%E4%BD%BF%E7%94%A8%20jq%20%E8%A7%A3%E6%9E%90%20JSON%20%E4%BB%A5%E8%8E%B7%E5%8F%96%E4%B8%8B%E9%9D%A2%20JSON%20%E4%B8%AD%E7%9A%84%E5%90%8D%E5%AD%97.png)
[
{
"firstname": "Alexander",
"lastname": "Smith",
"middlename": "Johnson",
"birthyear": 1918,
"deathyear": 1955,
"gender": "M",
"city": "Madison",
"state": "Wisconsin"
},
{
"firstname": "Lucy Jo Mary",
"lastname": "Carrie-Paulson",
"birthyear": 1967,
"deathyear": null,
"gender": "F",
"city": "Omaha",
"state": "Nebraska"
},
{
"firstname": "Jeremiah",
"lastname": "Springfield, Jr.",
"middlename": "Jebediah",
"birthyear": 1942,
"deathyear": 2003,
"gender": "M",
"city": "Kalamazoo",
"state": "Michigan"
}
]
如何获得名使用人的杰克从上面的json中,当没有中间名字存在于其中?
输出:露西·乔·玛丽
答案1
jq -r '.[]|select(.middlename|not).firstname'
这将为您提供没有middlename
属性或middlename
属性为 boolean 的条目的名字false
。
jq -r '.[]|select(has("middlename")|not).firstname'
将为您提供没有middlename
.