Skip to content

Magento 2.4.6. Unable to output data to json

I have a problem with the json output when sending a request to the API.

I receive data in this format:

"{n    "name": "John",n    "age": 30,n    "car": null,n   "address": {n        "street": "123 Main St",n        "city": "Anytown",n        "zip": "12345"n    }n}"

I need the data in this format:

{
    "name": "John",
    "age": 30,
    "car": null,
    "address": {
        { "street": "123 Main St",
        "city": "Anytown",
        "zip": "12345"
    }
}

Am I correct in understanding that this is because I am using such an interface on my side?

namespace DevRestApiApi;

interface TestInterface
{
    /**
    *
    * @return string
    */
    public function testMethod();
}

My Model:

namespace DevRestApiModel;

use DevRestApiApiTestInterface;

class TestModel implements TestInterface
{
    public function testMethod()
    {

        $data = [
            'name' => 'John',
            'age' => 30,
            'car' => null,
            'some' => null,
            'address' => [
                'street' => '123 Main St',
                'city' => 'Anytown',
                'zip' => '12345'
            ]
        ];

        $jsonResponse = json_encode($data, JSON_PRETTY_PRINT);

        return $jsonResponse;
    }
}