【问题标题】:Split JSON File Objects Into Multiple Files将 JSON 文件对象拆分为多个文件
【发布时间】:2017-05-20 21:55:10
【问题描述】:

我有一个包含太多 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
            ]
          ]
        ]
      }
    }
  ]
}

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

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -37.6171875,
              78.07107600956168
            ],
            [
              -35.48583984375,
              78.42019327591201
            ],
            [
              -37.880859375,
              78.81903553711727
            ],
            [
              -37.6171875,
              78.07107600956168
            ]
          ]
        ]
      }
    }
  ]
}

【问题讨论】:

  • 你已经给了你的目标,但你要的是什么?完成此任务的确切 JavaScipt 是什么?
  • 所有随机标签是怎么回事...它们应该用于缩小解决方案的范围...选择一些东西...

标签: javascript python json powershell jq


【解决方案1】:

假设输入在文件 (input.json) 中并且第 N 个组件应写入文件 /tmp/file $N.json 以 N=1 开头:

jq -c '.features = (.features[] | [.]) ' input.json |
  awk '{ print > "/tmp/file" NR ".json"}'

这里awk 的替代品是split -l 1。

如果您希望每个输出文件都“漂亮打印”,那么使用诸如 bash 之类的 shell,您可以(以 n 次额外调用 jq 为代价)编写:

N=0
jq -c '.features = (.features[] | [.])' input.json |
  while read -r json ; do
  N=$((N+1))
  jq . <<< "$json"  > "/tmp/file${N}.json"
done

对 jq 的每个额外调用都会很快,因此这可能是可以接受的。

【讨论】:

    【解决方案2】:

    PowerShell 解决方案(需要 PowerShell v3 或更新版本):

    $i = 0
    Get-Content 'C:\path\to\input.json' -Raw |
      ConvertFrom-Json |
      Select-Object -Expand features |
      ForEach-Object {
        $filename = 'C:\path\to\feature{0:d5}.json' -f ($i++)
    
        $properties = [ordered]@{
          type     = 'FeatureCollection'
          features = $_
        }
    
        New-Object -Type PSObject -Property $properties |
          ConvertTo-Json -Depth 10 |
          Set-Content $filename
      }
    

    【讨论】:

      【解决方案3】:

      我没有正确测试这段代码。但是应该为您提供一些关于如何解决上述问题的想法

      var 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
                        ]
                      ]
                    ]
                  }
                }
              ]
            }
            $(document).ready(function(){
              var counter = 1;
              json.features.forEach(function(feature){
                var data = {type: json.type, features: [feature]}
                var newJson = JSON.stringify(data);
                var blob = new Blob([newJson], {type: "application/json"});
                var url  = URL.createObjectURL(blob);
                var a = document.createElement('a');
                a.download    = "feature_" + counter + ".json";
                a.href        = url;
                a.textContent = "Download feature_" + counter + ".json";
                counter++;
                document.getElementById('feature').appendChild(a);
                document.getElementById('feature').appendChild(document.createElement('br'));
              });
            });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div id="feature"></div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-31
        • 1970-01-01
        • 1970-01-01
        • 2016-11-29
        • 2013-08-24
        • 2017-09-05
        • 1970-01-01
        • 2014-12-13
        相关资源
        最近更新 更多