JSON decode with PHP: Key started with # sign - php

JSON is like
{
"#http_status_code": 200,
"#records_count": 200,
"warnings": [],
"query": { ... ...
In PHP
$data = json_decode($json_entry);
print $data->#http_status_code; //returns error
print $data->http_status_code; //returns nothing
How can I retrieve status code?

1) As Object way
$data = json_decode($json_entry);
print $data->{'#http_status_code'};
2) OR use as array way by passing second argument as true in json_decode
$data = json_decode($json_entry, true);
print $data['#http_status_code'];

Try json_decode to get the json in form of array ..
$json_array = json_decode($data, true);
$required_data = $data['required_key']
with reference to your particular problem .. you will get array as
Array
(
[#http_status_code] => 200
[#records_count] => 200
[warnings] => Array
(
)
....
)
so you can access you data as $data['#http_status_code']

To access an object property that has funky characters in the name, quote the name and stick it in braces.
print $data->{'#http_status_code'};
Or, say $data = json_decode($json_entry, true); to get the data back as an array.

PHP cwill give syntax error when you do this:
$data->#http_status_code;
it looks for $http_status_code variable which is not present
so in order to make this work you have to do this:
echo $data->{'#http_status_code'};

Try this:
$data = json_decode($json_entry, true);
print $data["#http_status_code"];

Related

Change API Response to an array in PHP

I have an API which is returning me response like this.
{
"logs": [
[
"2018-05-22 00:10:16",
"Billed"
]
],
"package": "superpremium",
"subdate": "2018-05-08 14:18:18",
"submedium": "CALL"
}
I'm trying to change into array so It can be easily accessible for me. For example.
$response['subdate']; // echo 2018-05-08 14:18:18
$response['submedium']; // echo CALL
$response['package']; // echo superpremium
and the logs one should be like
$response['logs'];
logs must be an array so I can use foreach log to display all values of an array like 2018-05-22 00:10:16 or "Billed" etc
I have used below codes but returns empty screen.
json_decode($response, true);
json_decode($response);
I'm trying to change into array
Just cast it into one then:
$response = (array) json_decode($response);
After that, you can easily access $response['subdate'] etc.
Use json_decode($your_response) method. Your json data will get converted to arrays.
Like:
$response = json_decode($api_response);
And then you can access the values as (By seeing your data, seems that you are getting object):
$subdate = $response->subdate; // echo 2018-05-08 14:18:18
$submedium = $response->submedium; // echo CALL
$package = $response->package;
$json_data = json_decode($YourJSONResponse, true);
Then these will be available:
$json_data["package"]
$json_data["subdate"]
$json_data["submedium"]
Please change date from:
"submedium": "CALL",
To:
"submedium": "CALL"
Remove char "," and used function json_decode it, and enjoy

How to take out int value from JSON object?

I'm working with Laravel 5 right now and I have the following problem. I've got response from DB query:
[{"id":1}]
and I want to take out 1 as int or string. Any ideas?
I've tried to solve this like follows:
$json = (DB query);
$data = json_decode($json);
$final = $data[0]->id;
and response is :
json_decode() expects parameter 1 to be string, array given
This is all you need.
$response = json_decode($response); // Decode the JSON
$string = $response[0]->id; // Save the value of id var
As you say, the string you have is in JSON format, so you need to use json_decode to access it in PHP.
The square brackets refer to an array, and the braces refer to an object within that array, so what you're looking for is the id value of the first element (i.e. element 0) in the array.
<?php
$json = '[{"id":1}]';
$data = json_decode($json);
echo $data[0]->id;
// 1
Try this
$json_array = '[{"id":1}]';
$data = json_decode($json_array);
print_r($data); // To display the result array
You just need to decode it, if I'm not misunderstanding your questions.
$json = '[{"id":1}]';
$decodedObject = json_decode($json);
Then you can loop through the aray and do something with your data:
foreach($decodedObject as $key => $value) {
$id = $value->id;
}
If you're using Laravel, though, why not use Eloquent models?

PHP get variable from json results

Using PHP I have a json result of
{"text_block":[{"text":"XYZ","left":0,"top":0,"width":10,"height":12}]}
I can get this printed by the below code:
$json= file_get_contents('https://api.url');
$result = json_decode($json, true); //this returns an array
$result = json_decode($json);
$data = get_object_vars(json_decode($json));
$data = array_slice( $data, 0, 10 ); // now you can array functions
echo json_encode( $data );
but need to put the text "XYZ" into a variable for further use in PHP script. How can I do this, I've check various sources but don't seem to be getting anywhere! Thanks
$res = json_decode($data,true);
$my_text = $res['text_block'][0]['text'];
now you can use my_text as a variable
It looks like the value "XYZ" would be in
$data["text_block"][0]["text"]
Temporarily change the last line to var_dump($data) so you can get familiar with the php array structure.

Referencing PHP arrays returned from MySQL for a JSON API

I have a result, $result, returned from an SQL query which contains the following data: [{"TOTAL":"12345"}]
I want to put that result into a JSON API with a route of, say, /api/total/ and have it return: {total:12345}. I'll be setting the first half of that manually, e.g. 'total' => whatever in the Slim framework.
$app->render(200, array(
'total' => $result["TOTAL"]
)
);
How do I reference 12345 from the returned array? Doing $result["TOTAL"] doesn't work.
The result looks like [{"TOTAL":"12345"}]?
So you have to json_decode it first.
$decodedResult = json_decode($result, true);
echo $decodedResult[0]['TOTAL'];
Use this code , you will get the value.
$result = '[{"TOTAL":"12345"}]';
$res = json_decode($result);
echo $res[0]->TOTAL;
Please try this
$res = json_decode($result);
even though there is only one object in the array, it is still an array so you need to reference the object's index.
$result[0]["TOTAL"]

unable to decode json variable

I have following piece of code which is generating JSON variable. I use php built in json_decode function to decode the json variable but i am getting NULL after decoding JSOn variable.
$a=array("targetAction"=>"getHeadFields","targetHead"=>$table_name);
$obj1 = Post_Uamsdata($a);
echo $obj1;
$file = json_decode($obj1,true);
var_dump($file);
$obj1 is my json variable. whenever i echo it i get the result as follows:
{"success":"yes","error":"","message":"","arguments":"[{\"fieldNo\":\"1\",\"fieldName\":\"ItemType\",\"fieldType\":\"character(16)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"2\",\"fieldName\":\"Long\",\"fieldType\":\"character(20)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"3\",\"fieldName\":\"Lat\",\"fieldType\":\"character(20)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"4\",\"fieldName\":\"MapDate\",\"fieldType\":\"character(16)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"5\",\"fieldName\":\"FieldNote\",\"fieldType\":\"character(64)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]}]"}
i have checked it in online json validator and it is saying that this JSOn is valid. But whenever i decode this $obj1 into $file then i am always getting NULL.
after json_decode we get the following, But I guess this is not what you want. You want the fieldNo, fieldName etc also to be parsed.
$json = '{"success":"yes","error":"","message":"","arguments":"[{\"fieldNo\":\"1\",\"fieldName\":\"ItemType\",\"fieldType\":\"character(16)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"2\",\"fieldName\":\"Long\",\"fieldType\":\"character(20)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"3\",\"fieldName\":\"Lat\",\"fieldType\":\"character(20)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"4\",\"fieldName\":\"MapDate\",\"fieldType\":\"character(16)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"5\",\"fieldName\":\"FieldNote\",\"fieldType\":\"character(64)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]}]"}';
$arr = json_decode($json, true);
echo "<pre>";
print_r($arr);
Output is as follows
Array
(
[success] => yes
[error] =>
[message] =>
[arguments] => [{"fieldNo":"1","fieldName":"ItemType","fieldType":"character(16)","notnull":"f","fieldLabel":null,"primary_key":"f","default":null,"fieldOption":[]},{"fieldNo":"2","fieldName":"Long","fieldType":"character(20)","notnull":"f","fieldLabel":null,"primary_key":"f","default":null,"fieldOption":[]},{"fieldNo":"3","fieldName":"Lat","fieldType":"character(20)","notnull":"f","fieldLabel":null,"primary_key":"f","default":null,"fieldOption":[]},{"fieldNo":"4","fieldName":"MapDate","fieldType":"character(16)","notnull":"f","fieldLabel":null,"primary_key":"f","default":null,"fieldOption":[]},{"fieldNo":"5","fieldName":"FieldNote","fieldType":"character(64)","notnull":"f","fieldLabel":null,"primary_key":"f","default":null,"fieldOption":[]}]
)
As http://php.net/manual/en/function.json-decode.php indicates that the function will only work for UTF-8 encoded string, try the following:
$file = json_decode(mb_convert_encoding($obj1, 'UTF-8'),true);

Categories