jq:json对象的输出数组

时间:2016-06-27 19:01:03

标签: bash jq

说我有输入:

{
    "name": "John",
    "email": "john@company.com"
}
{
    "name": "Brad",
    "email": "brad@company.com"
}

如何获得输出:

[
    {
        "name": "John",
        "email": "john@company.com"
    },
    {
        "name": "Brad",
        "email": "brad@company.com"
    }
]

我试过了两个:

jq '[. | {name, email}]'

jq '. | [{name, email}]'

这两个都给了我输出

[
    {
        "name": "John",
        "email": "john@company.com"
    }
]
[
    {
        "name": "Brad",
        "email": "brad@company.com"
    }
]

我也没有看到文档中数组输出的选项,任何帮助赞赏

1 个答案:

答案 0 :(得分:28)

使用slurp模式:

  o   --slurp/-s:

      Instead of running the filter for each JSON object
      in the input, read the entire input stream into a large
      array and run the filter just once.
$ jq -s '.' < tmp.json
[
  {
    "name": "John",
    "email": "john@company.com"
  },
  {
    "name": "Brad",
    "email": "brad@company.com"
  }
]