jq:json对象的输出数组 [英] jq: output array of json objects

查看:74
本文介绍了jq:json对象的输出数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有输入:

{
    "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"
    }
]

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

I also saw no options for an array output in the documentations, any help appreciated

推荐答案

使用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"
  }
]

这篇关于jq:json对象的输出数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆