Sometimes you want to get your Craft entries data into a different format so you can allow another application to use the data or migrate to another system.
Sometimes you want to get your Craft entries data into a different format so you can allow another application to use the data or migrate to another system.
It’s always nice to have options so here are 4 ways to output entries to JSON in Craft.
json_encode
filterjson_encode
filterIf you’re really in a pinch and want to quickly get your entry content formatted as JSON, you can use a Twig template to make it happen.
For this article we’ll export our CraftQuest lessons data. We’ll create a new template called export.twig
and create an element query to get the lessons.
{% set lessons = craft.entries.section('lessons').asArray().all() %}
We are formatting the results of the element query as an array so we can more easily format it into JSON in our output tags (via the json_encode
filter). If we don’t do that we get an object back instead of an array. This could work but the returned data is a bit cleaner for creating JSON if we output it as an array.
After setting the output to the lessons variable we can now output it.
{{ lessons | json_encode }}
We use the json_encode
Twig filter, which is the Twig implementation of the json_encode
function in PHP. This filter will format the array we return as JSON.
Let’s curl this from the command line to see what we get.
$ curl https://craftquest.test/export
We should get something back like this:
[{"id":"10134","fieldLayoutId":"45","uid":"c8da2101-f5e5-4913-a859-1cc1fe961dc3","enabled":"1","archived":"0","dateCreated":"2019-07-23 01:41:49","dateUpdated":"2019-07-23 01:43:31","slug":"populating-nested-categories-from-a-google-sheet","siteId":"2","enabledForSite":"1","uri":"lessons\/populating-nested-categories-from-a-google-sheet","sectionId":"3","typeId":"3","authorId":"1","postDate":"2019-07-23 01:41:00"
When you do that you’ll notice that all of the double quotes are encoded. We don’t want that in our JSON. So let’s clean it up.
To do that we’ll add another filter. This time we’ll use the raw
filter which will take the JSON-encoded output and not do any encoding or formatting. It will just output the JSON as-is.
{{ lessons | json_encode | raw }}
Now we curl it again:
$ curl https://craftquest.test/export
And the results are much better.
[{"id":"10134","fieldLayoutId":"45","uid":"c8da2101-f5e5-4913-a859-1cc1fe961dc3","enabled":"1","archived":"0","dateCreated":"2019-07-23 01:41:49","dateUpdated":"2019-07-23 01:43:31","slug":"populating-nested-categories-from-a-google-sheet","siteId":"2","enabledForSite":"1","uri":"lessons\/populating-nested-categories-from-a-google-sheet","sectionId":"3","typeId":"3","authorId":"1","postDate":"2019-07-23 01:41:00"
Since we’re in the command line we have options for what to do with the output of curl. If we needed to, we can export this out as a file. This would be handy if we needed that file to import into another system.
$ curl https://craftquest.test/export > ~/Desktop/lessons.json
And that will export the JSON output to a file called lessons.json
on my local desktop.
I wouldn’t typically export to JSON like we did in the last section because it’s not really reusable or as elegant. For that I would go to the Element API. The downside with this method is that it does require installing a first-party plugin, so it isn’t native Craft and Twig.
First thing we want to do is install the Element API via the Craft plugin store or from the command line using:
$ composer require craftcms/element-api
And then we can install it, either through the control panel or via the command line. Let’s do the command line:
$ ./craft install/plugin element-api
Next up we create our config/element-api.php
file and define what we want output for the lessons data.
<?php
use craft\elements\Entry;
use craft\helpers\UrlHelper;
return [
'endpoints' => [
'lessons.json' => function() {
return [
'elementType' => Entry::class,
'criteria' => ['section' => 'lessons'],
'transformer' => function(Entry $entry) {
return [
'title' => $entry->title,
'url' => $entry->url,
'shortDescription' => $entry->lessonShortDescription,
];
},
];
},
]
];
This is a very basic Element API configuration. The endpoint we named lessons.json
so that’s the location we’ll use in the browser to access it.
If we have another application or service that needs to access this data then we can point it at the /lessons.json
URI.
We can also export the data to a file, if needed, by curl
ing the URL and outputting to a file.
$ curl https://craftquest.test/lessons.json > lessons.json
If you’re running a recent version of Craft CMS (and you should be!) you can also use the built-in GraphQL server to access your entry data as JSON.
The nice thing about this setup is that there’s nothing to install since GraphQL is enabled by default in all Craft installations as of Craft 3.3.
If you haven’t already used the GraphQL support in Craft then check out my videos on the topic. The setup is straight-forward.
I’m going to use GraphQL Playground which is an application that makes it easy to test queries against a GraphQL server. We provide GraphQL Playground a server endpoint and it connects and allows us to run queries against it.
Here’s the query we’ll run to get the basic lessons entry data:
query {
entries(section: "lessons") {
title,
slug
... on lessons_lessons_Entry {
lessonShortDescription
}
}
}
There’s not much here but this will get us started. Let’s run it and see what it does.
Now how do we get this exported as JSON? Well, it’s the same way we did before. We query the GraphQL endpoint from the command line and then send it to a file.
To get the command we’ll lean on the GraphQL Playground a bit by clicking “Copy Curl”, which will copy the full curl command, including the query and authorization headers, to our clipboard. This is super handy if we’re just trying to get our data out.
curl 'https://craftquest.test/api' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: file://' -H 'Authorization: bearer cHexrj4AAbMVL13WbWn9w6YepvuQxl_0' --data-binary '{"query":"query {\n\tentries(section: \"lessons\") {\n title,\n slug\n ... on lessons_lessons_Entry {\n lessonShortDescription\n }\n }\n}"}' --compressed > lessons.json
And the result of that is a JSON file with all of our data. Easy peasy!
As of Craft 3.4, we can export element entries right from the listing view the Craft Control Panel.
Here’s how to do it:
It’s the easiest way to quickly get a JSON export of your section entries!
No matter which method you choose, you now have several ways to get the section data out as JSON and usable for importing into another system.s