0

I'm trying to load a GeoJSON string into a VectorLayer but facing a JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data problem.

Already read som texts about like JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data but can't solve my problem.

This is my JSON. Tested at http://jsonviewer.stack.hu/.

{'type': 'FeatureCollection','crs': {'type': 'name','properties': {'name': 'EPSG:4326'}},'features': [{'type': 'Feature','geometry':{'type':'MultiLineString','coordinates':[[[-43.1996056,-22.9109588],[-43.1993777,-22.9115575],[-43.1993539,-22.9116778],[-43.1993568,-22.9118156],[-43.199378,-22.9123812]]]}},{'type': 'Feature','geometry':{'type':'MultiLineString','coordinates':[[[-43.199378,-22.9123812],[-43.1994332,-22.9131767]]]}},{'type': 'Feature','geometry':{'type':'MultiLineString','coordinates':[[[-43.1994332,-22.9131767],[-43.1994563,-22.9141351],[-43.1994364,-22.9142456],[-43.199379,-22.9143303]]]}},{'type': 'Feature','geometry':{'type':'MultiLineString','coordinates':[[[-43.199379,-22.9143303],[-43.1985846,-22.9144791]]]}}]}

My OpenLayers stuff (featuresText is the GeoJSON above):

    var styleFunction = function(feature) {
        return styles[feature.getGeometry().getType()];
    };      

    var vectorSource = new ol.source.Vector({
        features: ( new ol.format.GeoJSON() ).readFeatures( featuresText )
    });     

    var vectorLayer = new ol.layer.Vector({
        source: vectorSource,
        style: styleFunction
    });   

Error is at ol-debug.js:45632. The JSON string seems to be ok. I can't figure out.

I've read this http://openlayers.org/en/master/examples/geojson.html too.

Community
  • 1
  • 1
Magno C
  • 1,922
  • 4
  • 28
  • 53

1 Answers1

2

Looking at your geojson and your error message, I'll try an answer. Line one column 2 is a single quotation. JSON requires double quotes to hold strings, see this question.

Also, from the geojson specification:

A feature object must have a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value).

Your features have no properties.

With those two changes:

{"type": "FeatureCollection", "crs": {"type": "name","properties": {"name": "EPSG:4326"}},"features": [{"type": "Feature","properties": {}, "geometry":{"type":"MultiLineString","coordinates":[[[-43.1996056,-22.9109588],[-43.1993777,-22.9115575],[-43.1993539,-22.9116778],[-43.1993568,-22.9118156],[-43.199378,-22.9123812]]]}},{"type": "Feature","properties": {}, "geometry":{"type":"MultiLineString","coordinates":[[[-43.199378,-22.9123812],[-43.1994332,-22.9131767]]]}},{"type": "Feature","properties": {}, "geometry":{"type":"MultiLineString","coordinates":[[[-43.1994332,-22.9131767],[-43.1994563,-22.9141351],[-43.1994364,-22.9142456],[-43.199379,-22.9143303]]]}},{"type": "Feature","properties": {}, "geometry":{"type":"MultiLineString","coordinates":[[[-43.199379,-22.9143303],[-43.1985846,-22.9144791]]]}}]}

I had your geojson display successfully at geojson.io

Community
  • 1
  • 1
Andrew Reid
  • 37,021
  • 7
  • 64
  • 83
  • Great. I've found someone saying just the opposite. JSON must be single quoted. I think the problem may be just the `properties` member. Will try it now and post the results. – Magno C Dec 16 '16 at 10:10
  • You're right. All is working fine now (well, I'm facing some problems with styling but it is a problem to GIS S.E.). Thanks. BTW, +1 for the geojson.io. – Magno C Dec 16 '16 at 11:03
  • @MagnoC To quote the [JSON specification](http://json.org/) on strings _„A string is a sequence of zero or more Unicode characters, wrapped in double quotes, […]“_. – BlackJack Dec 27 '16 at 14:05