I am new to jquery - I have a valid geojson file whose features
I would like to access and convert into an object of key-value pairs. My objective is to only use properties.cat
as the key and properties.name
as the value (all other data can be ignored). Here is a sample:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
"features": [
{ "type": "Feature", "properties": { "cat": "A", "name": "Aberdeen"}, "geometry": { "type": "Point", "coordinates": [ 16.37208, 48.20849 ] } },
{ "type": "Feature", "properties": { "cat": "B", "name": "Berlin"}, "geometry": { "type": "Point", "coordinates": [ 4.3517103, 50.8503396 ] } },
{ "type": "Feature", "properties": { "cat": "C", "name": "Copenhagen"}, "geometry": { "type": "Point", "coordinates": [ 4.3517103, 50.8503396 ] } },
{ "type": "Feature", "properties": { "cat": "D", "name": "Dublin" }, "geometry": { "type": "Point", "coordinates": [ 12.56553, 55.67594 ] } },
{ "type": "Feature", "properties": { "cat": "E", "name": "Edinburgh"}, "geometry": { "type": "Point", "coordinates": [ -3.7037902, 40.4167754 ] } }
]
}
$.getJSON("sample.geojson", function(json) {
console.log(json.features[0].properties.cat);
});
As was pointed out below, features
is an array.
How could one create an object of key value pairs directly from every feature property in the json so as to have the following output:
{A : Aberdeen, B: Berlin, C: Copenhagen, D: Dublin, E: Edinburgh}