如何使用jq复制JSON数组中的现有对象? [英] How can I duplicate an existing object within a JSON array using jq?
本文介绍了如何使用jq复制JSON数组中的现有对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下geojson文件:
I have the following geojson file:
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"LINE": "RED",
"STATION": "Harvard"
},
"geometry": {
"type": "Point",
"coordinates": [-71.118906072378209, 42.37402923068516]
}
},
{
"type": "Feature",
"properties": {
"LINE": "RED",
"STATION": "Ashmont"
},
"geometry": {
"type": "Point",
"coordinates": [-71.063430144389983, 42.283883546225319]
}
}
]
}
我想将功能"数组内的第二个对象附加到其末尾,创建总共3个对象.使用以下代码段错误,无法添加数组([{"type":"F ...)和对象({" type:" Fe ...)".有没有一种方法可以使用jq而不对key:value对进行硬编码,如
I would like to append the second object within the "features" array to the end of it, creating 3 total objects. Using the below snippet errors out with "array ([{"type":"F...) and object ({"type":"Fe...) cannot be added". Is there a way to do this using jq without hardcoding the key:value pairs as seen here?
cat red_line_nodes.json | jq '.features |= . + .[length-1]' > red_line_nodes_2.json
推荐答案
短 jq
解决方案:
Short jq
solution:
jq '.features |= . + [.[-1]]' red_line_nodes.json
输出:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"LINE": "RED",
"STATION": "Harvard"
},
"geometry": {
"type": "Point",
"coordinates": [
-71.11890607237821,
42.37402923068516
]
}
},
{
"type": "Feature",
"properties": {
"LINE": "RED",
"STATION": "Ashmont"
},
"geometry": {
"type": "Point",
"coordinates": [
-71.06343014438998,
42.28388354622532
]
}
},
{
"type": "Feature",
"properties": {
"LINE": "RED",
"STATION": "Ashmont"
},
"geometry": {
"type": "Point",
"coordinates": [
-71.06343014438998,
42.28388354622532
]
}
}
]
}
这篇关于如何使用jq复制JSON数组中的现有对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文