1

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}
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
iskandarblue
  • 7,208
  • 15
  • 60
  • 130

1 Answers1

1

The callback function for $.getJson method has response already parsed automatically.

Also, features object is an array. Use this:

console.log(json.features[0].properties.cat);

For making key-value pairs you can use reduce method, which accepts a callback provided method applied for every item.

var json= {
      "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 ] } }
      ]
}
var obj=json.features.reduce(function(obj,item){
  obj[item.properties.cat]=item.properties.name;
  return obj;
},{});
console.log(obj);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128