Return JSON array without extra square brackets I'm having difficulties to remove an additional set of square brackets or avoid including them within array.

My JavaScript code currently accesses array's object at 0 index and therefore I'd have to loop in one level deeper array. Which I'd like to avoid if possible.

This is my endpoint:

'pricelist/&lt;entryId:\d+&gt;.json' =&gt; function($entryId) {
    return [
        'elementType' =&gt; ElementType::Entry,
        'criteria' =&gt; [
            'section' =&gt; "products",
            'descendantOf' =&gt; $entryId,
        ],
        'transformer' =&gt; function(EntryModel $entry) {

            $products = [];
            // "orderItem" is entry field
            foreach ( $entry-&gt;orderItem as $product ) {
                $products[] = [
                    'title' =&gt; $product-&gt;titleVariation,
                ];
            }
            return $products;
        },
    ];
},


Returned JSON array is:

{"data":
    [
        [
            {
                "title": "a",
            },
            {
                "title": "b",
            },
            {
                "title": "c",
            }
        ]
    ]
}


Expected JSON array output:

{"data":
    [
        {
            "title": "a",
        },
        {
            "title": "b",
        },
        {
            "title": "c",
        }
    ]
}


I have almost the same scenario from this question but the answer wasn't my issue.
change $products[] = to $products = in your for each loop, that should fix the extra square brackets. And if you want to get all entries and their titleVariation fields you should use 'elementType' =&gt; Entry::Class

'pricelist/&lt;entryId:\d+&gt;.json' =&gt; function($entryId) {
    return [
        'elementType' =&gt; Entry::Class,
        'criteria' =&gt; [
            'section' =&gt; 'products',
            'descendantOf' =&gt; $entryId,
        ],
        'transformer' =&gt; function(Entry $entry) {

            $products = [];
            // "orderItem" is entry field
            foreach ( $entry-&gt;orderItem as $product ) {
                $products = [
                    'title' =&gt; $product-&gt;titleVariation,
                ];
            }
            return $products;
        },
    ];
},