Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aayala91's avatar

Issues with JSON response from API - Object not array

I'm having issues getting a json reponse from my API as an array.

Here is my code:

Axios request:

axios.get('/api/sheets/all')
                    .then((response) => {
                        console.log(response.data.sheets);
                    })
                    .catch((error) => {
                        this.errors = error.response.data;
                    });

Here is my api method:

    public function all()
    {
        return response()->json([
            'sheets' => [
                'sheet1' => 'test',
                'sheet2' => 'test2'
            ]
        ]);
    }

When I console log the response, I can see its an object - not an array

{sheet1: "test", sheet2: "test2"}
sheet1: (...)
sheet2: (...)
__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}
get sheet1: ƒ reactiveGetter()
set sheet1: ƒ reactiveSetter(newVal)
get sheet2: ƒ reactiveGetter()
set sheet2: ƒ reactiveSetter(newVal)
__proto__: Object

This is causing issues with my vue component, as it expects an array - not an object.

Any ideas?

0 likes
8 replies
tykus's avatar
tykus
Best Answer
Level 104

You are returning nested associative arrays, these become JSON objects. If you want a JSON array, then remove the key(s) at the relevant level, e.g.

return response()->json([
    'sheets' => [
        'test',
        'test2'
    ]
]);
Cronix's avatar

or the key needs to be an int, starting with 0, and not skipping any numbers. Javascript doesn't have a concept of an "associative array"

json_encode([
    0 => [
        'sheet1' => 'test',
        'sheet2' => 'test2'
    ]
]);
// [{"sheet1":"test","sheet2":"test2"}]
tykus's avatar

@cronix the OP expects a sheets key in the response, that is the incorrect key to remove.

Cronix's avatar

They didn't state they needed that key preserved, only that they weren't receiving an array back. They'd just need to remove the sheets key from console.log(response.data.sheets);. Maybe they wanted the sheet1/sheet2 properties that you removed? Hopefully they'll clarify further.

aayala91's avatar

@TYKUS - This makes sense... I terms of JSON

['key' => 'value']

Is actually an object.

I couldn't tell which side I was going wrong - either on handling the response, or handling the request.

Thank you

aayala91's avatar

@CRONIX - Not quite what I want to return - but your comment "Javascript doesn't have a concept of an "associative array"" has helped me understand why I was going wrong here.

Thank you

allgood's avatar

return Response::json(array( 'sheets' => [ 'test', 'test2' ] ));

aayala91's avatar

@TYKUS @allgood - The only issue with this is that in my JavaScript - I wish to use the response to build options in a <select> input - Where by the option value is the "key" and the option text is the "value" ... I.e. <option value="sheet1">test</option>

How can I achieve this?

Please or to participate in this conversation.