Search code examples
jsonassemble

How to use a JSON data source with assemble.io?


The assemble.io documentation gives some examples of using simple JSON as a data source, such as:

{
  "title": "Assemble" ,
  "author": "Brian Woodward" 
 }

But, what if I wanted to use a more complex JSON structure for my data (to display a list of books on a single page)? How can that be done?

{
    "books": [
        {
            "title": "Book A",
            "author": "Fred"
        },
        {
            "title": "Book 47",
            "author": "Joe"
        }
    ]
}

Solution

  • Let say your library.json contain:

    {
        "books": [
            {
                "title": "Book A",
                "author": "Fred"
            },
            {
                "title": "Book 47",
                "author": "Joe"
            }
        ]
    }
    

    Use {{each}} (Assemble use handlebarjs for default templating) to display a list of books on a single page:

    {{#each library.books}}
      {{title}} - {{author}}
    {{/each}}