连接 JSON 文件

发布于 2025-01-10 13:15:48 字数 766 浏览 0 评论 0原文

我想将所有 json 文件连接到特定文件夹中。

我安装了 jq 工具并尝试了不同的方法,但到目前为止没有成功。

感谢您的帮助!

例子 : 1.json

[{
     "id": "1",
     "name": "Product 1"
},{
     "id": "2",
     "name": "Product 2"
},{
     "id": "3",
     "name": "Product 3"
}]

2.json

[{
     "id": "10",
     "name": "Product 10"
},{
     "id": "11",
     "name": "Product 11"
},{
     "id": "12",
     "name": "Product 12"
}]

输出.json

[{
     "id": "1",
     "name": "Product 1"
},{
     "id": "2",
     "name": "Product 2"
},{
     "id": "3",
     "name": "Product 3"
},{
     "id": "10",
     "name": "Product 10"
},{
     "id": "11",
     "name": "Product 11"
},{
     "id": "12",
     "name": "Product 12"
}]

I would like to concatenate all the json files in a specific folder.

I installed jq tools and tried different things but no success so far.

Thanks for your help!

Example :
1.json

[{
     "id": "1",
     "name": "Product 1"
},{
     "id": "2",
     "name": "Product 2"
},{
     "id": "3",
     "name": "Product 3"
}]

2.json

[{
     "id": "10",
     "name": "Product 10"
},{
     "id": "11",
     "name": "Product 11"
},{
     "id": "12",
     "name": "Product 12"
}]

output.json

[{
     "id": "1",
     "name": "Product 1"
},{
     "id": "2",
     "name": "Product 2"
},{
     "id": "3",
     "name": "Product 3"
},{
     "id": "10",
     "name": "Product 10"
},{
     "id": "11",
     "name": "Product 11"
},{
     "id": "12",
     "name": "Product 12"
}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两人的回忆 2025-01-17 13:15:48

通过使用 inputs 读取这两个文件(如果您还添加 --null-input (或 -n)选项,效果最佳):

jq -n '[inputs[]] ' 1.json 2.json 
[
  {
    "id": "1",
    "name": "Product 1"
  },
  {
    "id": "2",
    "name": "Product 2"
  },
  {
    "id": "3",
    "name": "Product 3"
  },
  {
    "id": "10",
    "name": "Product 10"
  },
  {
    "id": "11",
    "name": "Product 11"
  },
  {
    "id": "12",
    "name": "Product 12"
  }
]

Read in both files through using inputs (which works best if you also add the --null-input (or -n) option):

jq -n '[inputs[]] ' 1.json 2.json 
[
  {
    "id": "1",
    "name": "Product 1"
  },
  {
    "id": "2",
    "name": "Product 2"
  },
  {
    "id": "3",
    "name": "Product 3"
  },
  {
    "id": "10",
    "name": "Product 10"
  },
  {
    "id": "11",
    "name": "Product 11"
  },
  {
    "id": "12",
    "name": "Product 12"
  }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文