Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use JSON from a Jekyll _data directory?

According to the Jekyll docs you can access YAML, JSON, and CSV files in the _data directory using {{ site.data.filename }}.

I have a valid geoJson file of point features called chapters.json. I'm able to access the file, but I'm seeing some strange characters when I use the file in my javascript.

chapters.json excerpt:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "title": "MaptimeBER"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          13.391,
          52.521
        ]
      }
    },
    {...}
  ]
}

For example, when Jekyll processes the following: var chapters = {{ site.data.chapters }};

The output Javascript is:

var chapters = {"type"=>"FeatureCollection", "features"=>[{"type"=>"Feature", "properties"=> ...

My question is, why does the colon separating key-value pairs change to =>? It's causing my javascript to error out.

like image 252
Roy Avatar asked Dec 19 '22 11:12

Roy


1 Answers

Use the jsonify filter, it convert Hash or Array to JSON :

var chapters = {{ site.data.chapters | jsonify }};
like image 164
David Jacquel Avatar answered Dec 24 '22 03:12

David Jacquel