4

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!

4
  • So you have a valid JSON string, and you want to convert it to something that is not valid ? Commented Sep 30, 2014 at 19:10
  • features = JSON.parse('{"type":"FeatureCollection","features":[{"type":"Feature","property":"blah"}, {"type":"Feature","property":"blah2"}]}').features Commented Sep 30, 2014 at 19:11
  • I need to convert to a standard Javascript array for something in my javascript code; the regular (valid) GeoJSON is being used fine in some other part of the code. Thanks. Commented Sep 30, 2014 at 19:15
  • @Malk: I need to do in a loop--can't hard code the values. Thanks. Commented Sep 30, 2014 at 19:16

1 Answer 1

8

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;
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, thanks. But gettting an error: SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data geoObject = JSON.parse(geoJsonData);
Answer accepted! I think all I had to do was: features = geoJsonData.features; [no JSON.parse needed]. Thanks!
Ah, the geoJSON object wasn't even JSON then, it was already a javascript object :)
Hey so finally getting somewhere with this solution after hours... :-) anyone know how to get the properties in the array, i tried features = geoObject.features.properties, but that didn't work. Work for features array, but I want to go one more deeper and get all the properties???
ok, so I got it in case anyone else needs it: var features = []; features = json.features; var jproperties = json.features.map(function (el) { return el.properties; });
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.