将 JSON 与数组合并以创建新文件
Merging JSON with array to create new file
我正在尝试制作 U.S 的地图。使用 Mapbox 按县显示房价中值。我有一个包含所有县的 .json 文件,并且已经被 Mapbox tileset 接受 -
{
"type": "Topology",
"transform": {
"scale": [
0.035896170617061705,
0.005347309530953095
],
"translate": [
-179.14734,
17.884813
]
},
"objects": {
"us_counties_20m": {
"type": "GeometryCollection",
"geometries": [
{
"type": "Polygon",
"arcs": [],
"id": "0500000US01001"
},
{
"type": "Polygon",
"arcs": [],
"id": "0500000US01009"
},
{
"type": "Polygon",
"arcs": [],
"id": "0500000US01017"
},
{
"type": "Polygon",
"arcs": [],
"id": "0500000US01021"
}
]
}
}
}
基本上,它是一个 json 文件,包含 "type"(多边形)、"arcs"(映射县)和 "id",它是县。
这很棒,被 Mapbox Tilesets 接受,可以按县为我提供可视化效果,但我需要按县添加房价中位数(以便根据价格按县获得颜色)。
我有第二个 json 文件,它更像是一个数组,其中有
[
{
"0500000US01001": 51289.0,
"0500000US01009": 46793.0,
"0500000US01017": 39857.0,
"0500000US01021": 48859.0
}
]
等等,但基本上它有 ID -> 每个县的房价中值。 这两个文件的 ID 相同,并且数量相同。 所以我需要从中获取第三个 json 文件,其中包含 "type", "arcs"、"id" 和 "PRICE"(加法)。
这些文件很大 - 有什么建议吗?我尝试使用 jq 但收到一个错误
jq: error ... object ({"type":"To...) and array ([{"0500000U...) cannot be multiplied
提前致谢!
一种直接的方法是将第二个文件保存到一个变量中,并在更新第一个文件时将其用作参考。例如:
jq 'add as $prices | input
| .objects.us_counties_20m.geometries[] |= . + {PRICE: $prices[.id]}' file2 file1
add
如果file2
中的数组只包含一个对象,可以用.[0]
代替。
我正在尝试制作 U.S 的地图。使用 Mapbox 按县显示房价中值。我有一个包含所有县的 .json 文件,并且已经被 Mapbox tileset 接受 -
{
"type": "Topology",
"transform": {
"scale": [
0.035896170617061705,
0.005347309530953095
],
"translate": [
-179.14734,
17.884813
]
},
"objects": {
"us_counties_20m": {
"type": "GeometryCollection",
"geometries": [
{
"type": "Polygon",
"arcs": [],
"id": "0500000US01001"
},
{
"type": "Polygon",
"arcs": [],
"id": "0500000US01009"
},
{
"type": "Polygon",
"arcs": [],
"id": "0500000US01017"
},
{
"type": "Polygon",
"arcs": [],
"id": "0500000US01021"
}
]
}
}
}
基本上,它是一个 json 文件,包含 "type"(多边形)、"arcs"(映射县)和 "id",它是县。
这很棒,被 Mapbox Tilesets 接受,可以按县为我提供可视化效果,但我需要按县添加房价中位数(以便根据价格按县获得颜色)。
我有第二个 json 文件,它更像是一个数组,其中有
[
{
"0500000US01001": 51289.0,
"0500000US01009": 46793.0,
"0500000US01017": 39857.0,
"0500000US01021": 48859.0
}
]
等等,但基本上它有 ID -> 每个县的房价中值。 这两个文件的 ID 相同,并且数量相同。 所以我需要从中获取第三个 json 文件,其中包含 "type", "arcs"、"id" 和 "PRICE"(加法)。
这些文件很大 - 有什么建议吗?我尝试使用 jq 但收到一个错误
jq: error ... object ({"type":"To...) and array ([{"0500000U...) cannot be multiplied
提前致谢!
一种直接的方法是将第二个文件保存到一个变量中,并在更新第一个文件时将其用作参考。例如:
jq 'add as $prices | input
| .objects.us_counties_20m.geometries[] |= . + {PRICE: $prices[.id]}' file2 file1
add
如果file2
中的数组只包含一个对象,可以用.[0]
代替。