I want to decode gallery array JSON objects in Laravel 5.1. my JSON is:
{ "title": "aaaaaaaaaaaaaaaa", "category_id": "1", "user_id": "1", "gallery": "[{name: \"XCB808tvXNpqXKqekA2HlkJ8H.jpg\",size:5112},{name: \"s6kA6B0e5m1sdSAjPXqNwtiy4.jpg\", size: 13135}]" } When I use this code, return me null:
public function store(Request $request) { $json = json_decode($request['gallery'],true); return $json; } } and this is dd($request['gallery']) result
[{'name': "XCB808tvXNpqXKqekA2HlkJ8H.jpg",'size':5112},{'name': "s6kA6B0e5m1sdSAjPXqNwtiy4.jpg", 'size': 13135}]
galleryin not a JSON object, but list of JSON objects, so first divide them - you can useexplode()for that.dd( $request['gallery'] )?[{'name': "XCB808tvXNpqXKqekA2HlkJ8H.jpg",'size':5112},{'name': "s6kA6B0e5m1sdSAjPXqNwtiy4.jpg", 'size': 13135}]