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

vk011's avatar
Level 1

prevent json encode from returning arrays as strings

I get the following json response when I query my database:

    [{"id":1,"name":"my name","street":"Sava Burica","city":"Belgrade","state":"Zemun","zip":"11080","country":"Serbia","giftwrap":null,"products":"[{\"count\":2,\"id\":1,\"price\":275,\"name\":\"Kayak\"},{\"count\":1,\"id\":2,\"price\":48.95,\"name\":\"Lifejacket\"}]"}]

e db the column is like so:

[{"count":2,"id":1,"price":275,"name":"Kayak"},{"count":1,"i‌​d":2,"price":48.95,"‌​name":"Lifejacket"}]``` angularjs is being used on frontend.

It is all good except that the products value is a string and it should be an array.

I am using Laravel 5.3.

Can I somehow force it to return arrays as arrays and not as strings?

0 likes
5 replies
willvincent's avatar
Level 54

The products value isn't just a string, it's a json encoded array, inside a json encoded object. You've got double encoding going on.. so you need to json decode products, before you json encode the results.

Is products stored in the DB as json? or json encoded data? If so, you probably want to add an accessor to json decode it.. something like this:

public function getProductsAttribute($value) {
  return json_decode($value);
}
vk011's avatar
Level 1

Yes I think you are right, it is double encoding.

I am using json_encode to store a php array, and then retreive the whole row as json, out of which producs is just one column (but the only one with an array, or json in this case).

Is there a better way of storing the php array in the first place or something like that, because I have to retreive the whole row in one go, I do not see another way?

(I tried to json_decode on the query result but no difference)

willvincent's avatar

I have to retreive the whole row in one go, I do not see another way?

You could use a proper relationship.. ;)

But, use of a mutator and accessor should handle serialization/unserialization automatically for you.

rodrigo.pedra's avatar

You can skip writing a custom accessor or mutator by adding the products column to the $casts property on your model and set its type to array. Laravel will do the json encoding/decoding automatically to you

// ...
class  AModel extends Model {

// ...

    protected $casts = [
        'products' => 'array',
    ];

// ...

}

see the docs: [ https://laravel.com/docs/5.3/eloquent-mutators#attribute-casting ]

One note, if you double encoded you json in your DB you should fix that first.

2 likes
vk011's avatar
Level 1

Hey thank thanks for helping, yes double encoding seems to be the problem that needs to be solved, I managed to do it with some help from SO and it works, basically decode the whole thing and then decode just products and then encode the whole thing.

$s = DB::table('orders')->get();

        $orders = json_decode($s, true);

        $s = $orders[0]['products'];
        $orders[0]['products'] = json_decode($s, true);

        return json_encode($orders);

Please or to participate in this conversation.