<p>I am trying to get the element-api to output  a valid geojson collection.</p>
<p>The way the keys are set in the returned data make me think it isnt possible.</p>
<p>To produce a valid string of json i am having currently to do this to prefix the geojson collection in the correct format:</p>
<pre><code>const geoData = JSON.stringify({&quot;type&quot;: &quot;FeatureCollection&quot;, &quot;features&quot;: [data.features[0]]})

</code></pre>
<p>I'd rather do this in the endpoint but unfortunately I need to set the &quot;type&quot;: &quot;FeatureCollection&quot; at the resourcekey or above - this doesn't seem possible as it seems limited to just passing a string and not an array to resourcekey. For example to get the collection output format right Id need a resource key like this:</p>
<pre><code>'resourceKey' =&gt; ['type' =&gt; 'FeatureCollection', ['features']],

</code></pre>
<p>but this produces an error.
My return endpoint looks like below:</p>
<pre><code>return [
                'resourceKey' =&gt; 'features',
                'elementType' =&gt; Entry::class,
                'criteria' =&gt; $criteria,
                'paginate' =&gt; false,
                'pretty' =&gt; true,
                'cache' =&gt; false,
                'transformer' =&gt; function (craft\elements\Entry $entry) {
                    return [
                        'type' =&gt; 'Feature',
                        'properties' =&gt; [
                            'id' =&gt; $entry-&gt;id,
                            'title' =&gt; $entry-&gt;title,
                            'localauthority' =&gt; $entry-&gt;ladnm,
                            'mapitAreaId' =&gt; $entry-&gt;mapitAreaId,
                            'fundableRegion' =&gt; $entry-&gt;fundableRegion,
                            'openForFunding' =&gt; $entry-&gt;openForFunding,
                            'lsoacode' =&gt; $entry-&gt;lsoacode
                        ],
                        'geometry' =&gt; json_decode($entry-&gt;geojson)
                    ];
                },
            ];
</code></pre>
<p>Is there a way around this? Im on Craft 3.x and graphQl wasnt an option as json support wasn't historically good for my data:)</p>

CC BY-SA 4.0
2022-07-19T18:45:03.077Z
What's the error??
Ben Croker
CC BY-SA 4.0
2022-07-19T19:33:50.917Z
resourceKey only accepts a string not an array I think..
joomkit
<plugins><plugin-elementapi>
Outputting geojson collection with element-api
# Outputting geojson collection with element-api

(Asked by: username_0 on 2022-07-08)

I am trying to get the element-api to output  a valid geojson collection.
The way the keys are set in the returned data make me think it isnt possible.
To produce a valid string of json i am having currently to do this to prefix the geojson collection in the correct format:
const geoData = JSON.stringify({"type": "FeatureCollection", "features": [data.features[0]]})


I'd rather do this in the endpoint but unfortunately I need to set the "type": "FeatureCollection" at the resourcekey or above - this doesn't seem possible as it seems limited to just passing a string and not an array to resourcekey. For example to get the collection output format right Id need a resource key like this:
'resourceKey' => ['type' => 'FeatureCollection', ['features']],


but this produces an error.
My return endpoint looks like below:
return [
                'resourceKey' => 'features',
                'elementType' => Entry::class,
                'criteria' => $criteria,
                'paginate' => false,
                'pretty' => true,
                'cache' => false,
                'transformer' => function (craft\elements\Entry $entry) {
                    return [
                        'type' => 'Feature',
                        'properties' => [
                            'id' => $entry->id,
                            'title' => $entry->title,
                            'localauthority' => $entry->ladnm,
                            'mapitAreaId' => $entry->mapitAreaId,
                            'fundableRegion' => $entry->fundableRegion,
                            'openForFunding' => $entry->openForFunding,
                            'lsoacode' => $entry->lsoacode
                        ],
                        'geometry' => json_decode($entry->geojson)
                    ];
                },
            ];

Is there a way around this? Im on Craft 3.x and graphQl wasnt an option as json support wasn't historically good for my data:)

**Tags:** plugins, plugin-elementapi

> **Comments:**
> **username_1**: What's the error??  
> **username_0**: resourceKey only accepts a string not an array I think..  

---

## 0 answers

