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} 
3
  • Did you try logging the json variable to the console prior to parsing it? Commented Jun 4, 2017 at 19:22
  • what is var obj is ment for?? Commented Jun 4, 2017 at 19:22
  • I've edited the question. Please reopen Commented Jun 4, 2017 at 19:34

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks, I've edited my question
@the_darkside, have a look to updated answer.
thanks @Alexandru but when I wrap that in getJSON it does not work: $.getJSON("sample.geojson", function(json) { var obj={}; json.features.reduce(function(obj,item){ obj[item.properties.cat]=item.properties.name; return obj; },{}); console.log(obj) });
why does this work within the getJSON function?
It's because console.log is execute before the callback reduce is finished
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.