jq查询名称为'此'的CSV输出事情

时间:2018-03-12 17:10:10

标签: jq

example.json

{
  "foo": {
    "bar": {
      "name": "bob",
      "role": "gopherthis"
    },
    "baz": {
      "name": "tom",
      "role": "gopherthat"
    }
  }
}

示例查询:

jq -r '.foo[] | " \(.name),\(.role) " ' example.json
 bob,gopherthis 
 tom,gopherthat 

我的问题是如何让bar和baz与他们的名字和角色相关联?输出的位置是:

bar,bob,gopherthis
baz,tom,gopherthat

1 个答案:

答案 0 :(得分:1)

jq 解决方案:

jq -r '.foo | to_entries[] | "\(.key),\(.value.name),\(.value.role)"' example.json

输出:

bar,bob,gopherthis
baz,tom,gopherthat