我有一个包含以下格式的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 :(得分:3)
这是一个只需要调用jq
和awk
之一的解决方案,假设输入在文件(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的每次额外调用都会很快,所以这可能是可以接受的。
答案 1 :(得分: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'));
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="feature"></div>
&#13;
答案 2 :(得分: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
}