<p>Easy enough:</p>

<pre><code>$query = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$query-&gt;section = 'mySection';
$results = $query-&gt;find();
</code></pre>

<p>You can obviously refine the query before calling <code>find()</code>:</p>

<pre><code>$query-&gt;limit   = null;
$query-&gt;myCustomField = 'value';
</code></pre>

CC BY-SA 3.0
2016-11-11T10:09:15.003Z
Thanks for your answer, but i want to "json encode" the result so that i can use it in my javascript , its hard for me to convert the result using elements criteria is there a way that i can only get the results in json format
uncle drew
CC BY-SA 3.0
2016-11-11T10:23:22.920Z
Well, you can return json_encode($results). Maybe this can help: https://github.com/pixelandtonic/ElementAPI
gioppe
CC BY-SA 3.0
2016-11-11T08:00:51.270Z
2016-11-11T08:00:51.270Z
gioppe
<p>Can you give an example of returning entries from a section in JSON format using a custom query? </p>

<plugin-development>
How to create a custom query inside a plugin in Craft?
# How to create a custom query inside a plugin in Craft?

(Asked by: username_0 on 2016-11-11)

Can you give an example of returning entries from a section in JSON format using a custom query?

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2016-11-11. Score: 2

Easy enough:
$query = craft()->elements->getCriteria(ElementType::Entry);
$query->section = 'mySection';
$results = $query->find();

You can obviously refine the query before calling find():
$query->limit   = null;
$query->myCustomField = 'value';

> **Comments:**
> **username_0**: Thanks for your answer, but i want to "json encode" the result so that i can use it in my javascript , its hard for me to convert the result using elements criteria is there a way that i can only get the results in json format  
> **username_1**: Well, you can return json_encode($results). Maybe this can help: https://github.com/pixelandtonic/ElementAPI  

---

