小编Waf*_*ffy的帖子

合并JSON数组

所以我的目标是合并json文件获取这种格式:

{
  "title": "NamesBook",
  "list": [
    {
      "name": "Ajay"
    },
    {
      "name": "Al"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我的文件看起来像这样的格式:

blahblah.json

{
  "title": "NamesBook",
  "list": [
    {
      "name": "Ajay"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

blueblue.json

{
  "title": "NamesBook",
  "list": [
    {
      "name": "Al"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我可以使用以下内容将所有名称的列表数组存储在变量中:

x = jq -s '.[].list' *.json
Run Code Online (Sandbox Code Playgroud)

然后我计划将变量附加到我创建的文件out.json中的空数组中,如下所示:

{
  "type": "NamesBook",
  "list": []
}
Run Code Online (Sandbox Code Playgroud)

但是,当我的脚本在线上运行时

jq '.list[] += "$x"' out.json'
Run Code Online (Sandbox Code Playgroud)

它会出现jq错误:

无法迭代null.

即使我添加一个随机元素,也会出现相同的错误.关于我应该如何进行的提示?jq中还有其他工具可以帮助实现合并数组吗?

arrays bash json jq

11
推荐指数
3
解决办法
8701
查看次数

将 JSON 文件对象拆分为多个文件

我有一个包含过多数据对象的 JSON 文件,格式如下:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -37.880859375,
              78.81903553711727
            ],
            [
              -42.01171875,
              78.31385955743478
            ],
            [
              -37.6171875,
              78.06198918665974
            ],
            [
              -37.880859375,
              78.81903553711727
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -37.6171875,
              78.07107600956168
            ],
            [
              -35.48583984375,
              78.42019327591201
            ],
            [
              -37.880859375,
              78.81903553711727
            ],
            [
              -37.6171875,
              78.07107600956168
            ]
          ]
        ]
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我想拆分大文件,以便每个特征对象都有自己的文件,其中包含其类型对象和特征(坐标)对象。所以基本上,我试图获得其中的许多:

{
  "type": "FeatureCollection",
  "features": [ …
Run Code Online (Sandbox Code Playgroud)

javascript python powershell json jq

0
推荐指数
1
解决办法
5054
查看次数

标签 统计

jq ×2

json ×2

arrays ×1

bash ×1

javascript ×1

powershell ×1

python ×1