json 文档拆分工具_将JSON文件对象拆分为多个文件

本文介绍如何利用jq和awk命令行工具将包含多个Feature对象的大型JSON文件拆分成每个Feature对象一个单独的文件,每个文件都包含type和features坐标信息。
摘要由CSDN通过智能技术生成

I have a file with too many data objects in JSON of the following form:

{

"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

]

]

]

}

}

]

}

I would like to split the large file such that each features object would have its own file containing a its type object and features(coordinates) object. So essentially, I am trying to get many of these:

{

"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

]

]

]

}

}

]

}

解决方案

Here's a solution requiring just one invocation of jq and one of awk, assuming the input is in a file (input.json) and that the N-th component should be written to a file /tmp/file$N.json beginning with N=1:

jq -c '.features = (.features[] | [.]) ' input.json |

awk '{ print > "/tmp/file" NR ".json"}'

An alternative to awk here would be split -l 1.

If you want each of the output files to be "pretty-printed", then using a shell such as bash, you could (at the cost of n additional calls to jq) write:

N=0

jq -c '.features = (.features[] | [.])' input.json |

while read -r json ; do

N=$((N+1))

jq . <<< "$json" > "/tmp/file${N}.json"

done

Each of the additional calls to jq will be fast, so this may be acceptable.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值