2018年5月12日

JSON查询工具jq

测试数据JSON文件 users.json

[{"Age":"21","Name":"Alice"},{"Age":"22","Name":"Bob"},{"Age":"21","Name":"Eve"}]

下文省略pipe输入cat users.json | jq

.

[
  {
    "Age": "21",
    "Name": "Alice"
  },
  ...
]

.[]

{
  "Age": "21",
  "Name": "Alice"
}
...

.[].Age

"21"
"22"
"21"

.[] | select(.Age == "21")

{
  "Age": "21",
  "Name": "Alice"
}
{
  "Age": "21",
  "Name": "Eve"
}

-r .[] | [.Name, .Age] | join(",")

Alice,21
Bob,22
Eve,21

相关链接