2017-01-06 29 views
-1

我有以下形式的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 
      ] 
      ] 
     ] 
     } 
    } 
    ] 
} 

我想拆分大文件使得每个功能对象将有自己的包含其类型对象和特征(坐标)对象的文件。所以基本上,我想获得很多这些:

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

你给自己的目标,但你有什么要求?确切的JavaScipt来完成这个? – drkibitz

+0

什么是所有的随机标签...他们应该被用来缩小解决方案...挑选一些东西...... –

回答

0

我还没有正确测试过这段代码。但是应该提供你一些想法你如何能解决上述

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>

0

这里是需要的jqawk一个只是一个调用的解决方案中提到的问题,假设输入是在一个文件中( input.json),并且第N个部件应写入具有N个开头的文件/tmp/file$N.json = 1:

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

的替代方法awk这里是split -l 1

如果您希望每个输出文件的是“适合打印”,然后使用shell如bash,你可以(在给JQ N个附加呼叫的费用)写:

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

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

0

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 
    } 
相关问题