2017-01-06 2 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の1のただ一つの呼び出しを必要とするソリューションですinput.json)とN番目の成分は、N = 1で始まるファイル/tmp/file$N.jsonに書き込まれるべきである。

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

に代わりますはsplit -l 1となります。

あなたは、出力ファイルのそれぞれは、そのような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 
    }