Search code examples
javascriptarraysgeojson

Converting GeoJSON object to Javascript Array


I need to convert a GeoJSON output from a server to a Javascript array; I have done some searches and they were for the 'regular' JSON outputs and not the GeoJSON. Here is the output from the Server:

{"type":"FeatureCollection","features":[{"type":"Feature","property":"blah"}, {"type":"Feature","property":"blah2"}]}

And here is what I need (note, no quotation on the 'features' variable needed):

features = [{"type":"Feature","property":"blah"}, {"type":"Feature","property":"blah2"}]}

I think I need to find the 'features' property and then loop through its objects? Thanks!


Solution

  • A GeoJSON object is still a JSON object (it's the first thing their documentation says).

    http://geojson.org/geojson-spec.html

    If you want to store the features PROPERTY of a GeoJSON object, access the property value as you would normally after conversion to a javascript object and push that property into a new variable, or use as is.

    var geoObject = JSON.parse(geoJSONObject);
    var features = [];
    
    features = geoObject.features;