I'm trying to combine a GeoJSON map file with key values from a JSON file to use for a choropleth map.
Here is what the files look like:
data1.json
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"STATE": "06",
"ZIPCODE": "94601"
},
"geometry": {
"type": "Polygon",
"coordinates": [...]
}
},
{
"type": "Feature",
"properties": {
"STATE": "06",
"ZIPCODE": "94501"
},
"geometry": {
"type": "Polygon",
"coordinates": [...]
}
}
]
}
data2.json
{
"94501": {
"crime": 172,
"income": 9456,
},
"94601": {
"crime": 118,
"income": 28097,
}
Here's what I'd like the combined object to look like:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"STATE": "06",
"ZIPCODE": "94601",
"crime": 118,
"income": 28097
},
"geometry": {
"type": "Polygon",
"coordinates": [...]
}
},
{
"type": "Feature",
"properties": {
"STATE": "06",
"ZIPCODE": "94501",
"crime": 172,
"income": 9456
},
"geometry": {
"type": "Polygon",
"coordinates": [...]
}
}
]
}
Currently, my code looks like:
d3.json("data1.json", function (geoData) {
d3.json("data2.json", function (zipdata) {
var geoFeatures = geoData.features;
for (var i = 0; i < geoFeatures.length; i++) {
Object.keys(zipdata).forEach(function (key) {
if (geoFeatures[i].properties.ZIPCODE == key) {
var combined = Object.assign({}, geoFeatures[i], zipdata[key]);
console.log(combined);
}
})
}
})
})
This gets me close to what I want, but I'd like to retain the GeoJSON map format shown in data1.json
.