<p>You can set the header in Twig with the <code>{% header %}</code> function. Like this:</p>

<pre><code>{% header "Content-Type: application/json; charset=utf-8" %}
{% header "Access-Control-Allow-Origin: *" %}
</code></pre>

<p>As to generating the actual json, my favourite is using the <a href="https://github.com/mattstauffer/craftcms-prune" rel="nofollow">prune plugin</a> and do it like this:</p>

<pre><code>{% set myEntries = craft.entries.section('mySection').find() %}

{
    {%- spaceless -%}
    "myEntries": {{ myEntries | prune(['onefield', 'anotherfield']) | json_encode() | raw }},
    {%- endspaceless -%}
}
</code></pre>

<p>You could also just loop over all the entries and create the json yourself, but I always get into all sorts of issues with encoding when doing that.</p>

<p>If you're going to do a lot of json requests, you could also look into the <a href="https://github.com/pixelandtonic/ElementAPI" rel="nofollow">Element API plugin</a> from Pixel &amp; Tonic. You'd probably have to set the headers on the webserver level (.htaccess or nginx.conf) though.</p>

CC BY-SA 3.0
2015-10-03T12:59:01.173Z
Awesome this worked a treat. Cheers @andre
Lettie
CC BY-SA 3.0
2015-10-02T12:40:20.730Z
2015-10-02T12:40:20.730Z
André Elvan
<p>Hi Not sure on the best way to do this. I have some javascript that requires a json formatted file to populate a calendar. I can do this in php by adding the following to the file that is being requested:</p>

<pre><code>header('Access-Control-Allow-Origin: *');
    $array = array(
    array('date' =&gt; "27/9/2015", 'title' =&gt; 'BBC Event', 'link' =&gt; 'http://www.bbc.co.uk')
    );
echo json_encode($array);
</code></pre>

<p>I need to be able to do the same with craft. I have tried just formatting the entries in twig which returns the json as it should be but because the header is missing the javascript call does not run it. I have also created a small plugin to pull out all the entries which may be a better option. I just need to know how to return those entries in json to a template as per the above php would create with a header included.</p>

<p>Any help much appreciated.</p>

<p>Cheers</p>

<plugin-development><json>
Return json formatted entries and include a header for Access-Control-Allow-Origin
# Return json formatted entries and include a header for Access-Control-Allow-Origin

(Asked by: username_0 on 2015-10-02)

Hi Not sure on the best way to do this. I have some javascript that requires a json formatted file to populate a calendar. I can do this in php by adding the following to the file that is being requested:
header('Access-Control-Allow-Origin: *');
    $array = array(
    array('date' => "27/9/2015", 'title' => 'BBC Event', 'link' => 'http://www.bbc.co.uk')
    );
echo json_encode($array);

I need to be able to do the same with craft. I have tried just formatting the entries in twig which returns the json as it should be but because the header is missing the javascript call does not run it. I have also created a small plugin to pull out all the entries which may be a better option. I just need to know how to return those entries in json to a template as per the above php would create with a header included.
Any help much appreciated.
Cheers

**Tags:** plugin-development, json


---

## 1 answers

### Answer by username_1 on 2015-10-02. Score: 4 | Accepted answer

You can set the header in Twig with the {% header %} function. Like this:
{% header "Content-Type: application/json; charset=utf-8" %}
{% header "Access-Control-Allow-Origin: *" %}

As to generating the actual json, my favourite is using the prune plugin and do it like this:
{% set myEntries = craft.entries.section('mySection').find() %}

{
    {%- spaceless -%}
    "myEntries": {{ myEntries | prune(['onefield', 'anotherfield']) | json_encode() | raw }},
    {%- endspaceless -%}
}

You could also just loop over all the entries and create the json yourself, but I always get into all sorts of issues with encoding when doing that.
If you're going to do a lot of json requests, you could also look into the Element API plugin from Pixel & Tonic. You'd probably have to set the headers on the webserver level (.htaccess or nginx.conf) though.

> **Comments:**
> **username_0**: Awesome this worked a treat. Cheers @andre  

---

