0

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\"}]"}]

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

I am using Laravel 5.3 as my framework but I don't think that makes a difference.

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

6
  • 5
    the products value looks double JSON encoded. Can you include the code that generates this JSON? Commented Oct 19, 2016 at 18:37
  • it's a laravel query, DB::table('orders')->get(); and in the db the column is like so: [{"count":2,"id":1,"price":275,"name":"Kayak"},{"count":1,"id":2,"price":48.95,"name":"Lifejacket"}] Commented Oct 19, 2016 at 18:48
  • It's saved in the DB with JSON format? Commented Oct 19, 2016 at 18:49
  • 1
    Well, then your "products" is already a string. You should decode it, insert it into your object and encode the whole thing. What you are seeing there is what @Don'tPanic said, a double json encoded object. PD: you shouldn't save json structures in a relational database tho.. Commented Oct 19, 2016 at 18:55
  • 1
    Just because angular sends JSON doesn't mean you have to directly store that JSON. I would recommend decoding it and using it to populate product entities instead. Commented Oct 19, 2016 at 19:05

1 Answer 1

2

In the main string your products is also JSON String encoded data, so all you need to do to decode it is

$s = '[{"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\"}]"}]';
$address = json_decode($s,true);

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

This returns

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => my name
            [street] => Sava Burica
            [city] => Belgrade
            [state] => Zemun
            [zip] => 11080
            [country] => Serbia
            [giftwrap] => 
            [products] => Array
                (
                    [0] => Array
                        (
                            [count] => 2
                            [id] => 1
                            [price] => 275
                            [name] => Kayak
                        )

                    [1] => Array
                        (
                            [count] => 1
                            [id] => 2
                            [price] => 48.95
                            [name] => Lifejacket
                        )

                )

        )

)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.