Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding JSON in a GitHub Pages generated file

I'm attempting to create a JSON feed on a GitHub Pages site, and I'm having issues with JSON because I'm not sure how I can properly encode it using Jekyll. Is there an extension or method I can use?

feed: http://iowacodecamp.github.io/sessions.json

source: https://github.com/IowaCodeCamp/iowacodecamp.github.io/blob/master/sessions.json

Note the double quotes in the data.

like image 836
Mike Cole Avatar asked Jan 11 '23 01:01

Mike Cole


1 Answers

Your json doesn't validate because of the coma after the last session.

If you don't want a coma after the last session, use forloop liquid object around

{
  "sessions": {
    "session": [{% for session_hash in site.data.sessions %}{% assign session = session_hash[1] %}
       {
          "title": {{ session.title | jsonify }},
          "description": {{ session.description | jsonify }},
          "level": {{ session.level | jsonify }},
          "author": {
              "name": {{ session.speaker.name | jsonify }},
              "slug": {{ session.speaker.slug | jsonify }}
          }
       }{% if forloop.last == false %}, {% endif %}{% endfor %}
    ]
  }
}

Question : You have multiples sessions in your datas but they are all in the same session array. Do you really need this key ? Maybe you can just do :

{
  "sessions": [{% for session in site.data.sessions %}
       {{ session[1] | jsonify }}{% if forloop.last == false %}, {% endif %}{% endfor %}
  ]
}

Which also validates.

like image 190
David Jacquel Avatar answered Jan 30 '23 23:01

David Jacquel