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}]
The decoding process is right. I think your problem is you could have a malformed JSON string.
Replace the single quotes around property names with double quotes:
[{"name": "XCB808tvXNpqXKqekA2HlkJ8H.jpg","size":5112},{"name": "s6kA6B0e5m1sdSAjPXqNwtiy4.jpg", "size": 13135}]
I am not pretty sure about your program flow but as you are injecting Request dependency to the store function, I assume the JSON object is a part of your request. In that case, you can try,
$input = $request->json()->all();
Just print_r($input) and see what you are getting.
If JSON object is not a part of your request, you missed passing $json to your function. This is a wild guess, though!
Just dropping by for having the same issue of trying to get the json formatted response (Laravel 8.8.0). The way I was able to get it working was using:
$jsonFormattedResult = json_decode ($response->content(), true);
Hope it helps someone out. ( '-')/
you can use Response::json($value);
Related
i'm trying to get the fields "name, price, image and rarity" to show in a php file, anyone can help me? Thanks ;D
{
"status": 300,
"data": {
"date": "2019-09-16T00:00:00.000Z",
"featured": [
{
"name": "Flying Saucer",
"price": "1,200",
"images": {
"icon": icon.png",
},
"rarity": "epic",
},
I'm using this that a friend told me, but i cant put that to work :c
<?php
$response = json_decode(file_get_contents('lista.json'), true);
foreach ($response as $val) {
$item = $val['name'];
echo "<b>$item</b>";
}
?>
I'm not quite sure what you are trying to achieve. You can just access the contents via the $response array like this:
echo $response['status']; // would output 300
You can use foreach to iterate through the array. For example: If you want to output the name of every element of the array you can use:
foreach ($response['data'] as $val) { // loop through every element of the data-array (if this makes sense depends on the structure of the json file, cant tell because it's not complete)
echo $val['featured']['name'];
}
You gotta get the index in $val['data']['featured']['name'] to retrieve the name index.
When you defined the second parameter of json_decode, you said that you want your json to be parsed to an array. The brackets in the original json identify when a new index of your parsed array will begin.
I suggest you to read about json_decode and json in general:
JSON: https://www.json.org/
json_decode function: https://www.php.net/manual/en/function.json-decode.php
i know this question ask many time before but still i could't get this working.
i have a json and when i dump $TenentsAccessible output is this
string(71) "[{`TenantID`:`test.com`,`Name`:`12thdoor`}]"
i need to get the value inside TenantID property. so i use json decode to convert this to php array but is returns null
$jnTenant = json_decode($TenentsAccessible,TRUE);
$tenantID = $jnTenant["TenantID"];
var_dump($jnTenant); // this return null
i try to remove the " and unwanted characters using this
$TenentsAccessible = str_replace('"', '"', $TenentsAccessible);
$TenentsAccessible=preg_replace('/\s+/', '',$TenentsAccessible);
i know this type of question ask before but i still could't get this to work. appropriate the hlep. thanks
you can check your json code on JsonLint.
I tried your code and it's not correct because of backticks (`).
So you should replace with (") to have
[{
"TenantID": "test.com",
"Name": "12thdoor"
}]
As hasan described in his answer, json_decode returns a multi-dimensional array, so to get TenantID:
$jnTenant = json_decode('[{"TenantID":"test.com","Name":"12thdoor"}]',true);
$tenantID = $jnTenant[0]['TenantID'];
var_dump($tenantID) ;
If you want to get the "TenantID" in the way you described, you have to modify (if you can) the json like this
{
"TenantID": "test.com",
"Name": "12thdoor"
}
Hope it helps.
try it :
$jnTenant = json_decode('[{"TenantID":"test.com","Name":"12thdoor"}]',true);
$tenantID = $jnTenant[0]['TenantID'];
var_dump($tenantID) ;
correct json and corect get json !
for understand this plz print_r( $jnTenant );
this varibale is Two-dimensional array .
I have an issue with a json string.
I send this json string in Postman,
{
"places": [
{
"longitude": "79.9304633",
"latitude": "6.720229199999999",
"city": "Panadura"
},
{
"longitude": "79.86296829999999",
"latitude": "6.855948499999999",
"city": "Dehiwala"
}
]
}
But in the server side, when I get this value using this,
$jsonPlaces = $_POST['jsonplaces'];
and the value of $jsonPlaces looks like this,
{\\\"places\\\":[{\\\"longitude\\\":\\\"79.9304633\\\",\\\"latitude\\\":\\\"6.720229199999999\\\",\\\"city\\\":\\\"Panadura\\\"},{\\\"longitude\\\":\\\"79.86296829999999\\\",\\\"latitude\\\":\\\"6.855948499999999\\\",\\\"city\\\":\\\"Dehiwala\\\"}]}
therefore json_encode() function doesn't work for the above string since json syntax is changed.
How to fix this issue?
Thanks
Use stripslashes($_POST['jsonplaces']) to remove additional slashes.
Probably Postman uses the addslashes method to escape the (JSON) string before inputing it in the DB, as mentioned in comment, you should use the inverse function stripslashes to un-escape the string before doing the json_decode function
I wonder how to parse a json array without values
Json: {"status":"FAILED","errors":{"email":["NOT_UNIQUE"],"name":["TOO_SHORT"]}}
How can i get the value of email in a foreach loop?
What i mean with "without value" is: there is an array called email and name... How can i get the value for "email" that currently says NOT_UNIQUE?
In your current example, your JSON string is malformed. I dont know if thats a typo on your part while creating your question. Assuming the JSON string is okay in your code, a simple json_decode() will do just fine. Consider this example:
$json_string = '{ "Json": { "status": "FAILED", "errors": { "email": [ "NOT_UNIQUE" ], "name": [ "TOO_SHORT" ] } }}';
$data = json_decode($json_string, true);
echo $data['Json']['errors']['email'][0]; // NOT UNIQUE
use json_decode, json_decode($str, true) will return it as an assosiative array whereas json_decode($str, false) will return objects.
json_decode("{"status":"FAILED","errors":{"email":["NOT_UNIQUE"],"name":["TOO_SHORT"]}}", true)['errors']['email']
should get the email for you.
I am using a API to get some data from curl. Response i get is like:
{
"abc": 123,
"zxc": 122339900,
"cui": "usd",
"cumer": "wXl3tAPXCM",
"fee": 0,
"live": false,
"object": "test",
"paid": true,
"sss": {
"qwe": "4242",
"wer": "sss",
"mkm": "isa"
}
}
In which form it is ? how can i parse it to get same values in an array for the further process?
Any idea will be appreciated.
$new = json_decode($returnData);
This is a json, which can be converted into PHP array by the function json_decode
if the response is JSON you can use:
json_decode($response, true);
the second parameter forces the result to be an associative array.
php.net documentation
The response is in JSON
It's becoming the standard for transferring data from one machine to other because it is bandwidth friendly and language independent.
To translate the JSON into PHP array you can use PHP's native json_decode
$new = json_decode($returnData, TRUE);
Just be sure to give last param TRUE or you will get PHP Object instead of array.
This is a json string. You have to decode like below & you will get an array.
$your_array = json_decode($your_String);
The data is in 'JSON' format and you should have to use json_decode() function to decode the data..
This response is json. Use json_decode e.g.
$myarray = json_decode ($response, True);
Where the second argument True tells the function to produce an associative array.