Fetching data from Key Value JSON object [duplicate] - php

This question already has answers here:
Why can't Python parse this JSON data? [closed]
(3 answers)
Closed 4 years ago.
I have a JSON object below, but i'm struggling to get name, value from it.
I would want to get the values and save them into a database.
$json = '
{
"Body":
{
"stkCallback":
{
"ResultCode":0,
"ResultDesc":"The service request is processed successfully.",
"CallbackMetadata":
{
"Item":
[
{
"Name":"Amount",
"Value":10
},
{
"Name":"TransactionDate",
"Value":20170727154800
},
{
"Name":"PhoneNumber",
"Value":26721566839
}
]
}
}
}
}';
How can I get the "Name":"Amount" values and "Name":"TransactionDate" value.
At least for the rest
$ResultCode = json_decode($json)->Body->stkCallback->ResultCode;
$ResultDesc = json_decode($json)->Body->stkCallback->ResultDesc;
The question is, how do I get the values in a loop when some have values and others don't?

Try this :
$response=json_decode($json,true);
echo $amount=$response['Body']['stkCallback']['CallbackMetadata']['Item']['0']['Value'];
echo $TransactionDate=$response['Body']['stkCallback']['CallbackMetadata']['Item']['1']['Value'];
echo $PhoneNumber=$response['Body']['stkCallback']['CallbackMetadata']['Item']['2']['Value'];
You have all the values in variables now.

Related

How to see if json array contains a value? [duplicate]

This question already has answers here:
PHP multidimensional array search by value
(23 answers)
json_decode to array
(12 answers)
Closed 2 years ago.
So, I'm new to php and while testing some services, I have a JSON object:
{
"entry": [
{
"authorId": "#me",
"type": "USER"
},
{
"authorId": "514",
"type": "USER"
},
{
"authorId": "516",
"type": "USER"
}
],
"count": 3,
"totalResults": 3
}
and I need to assert that '#me' is part of this object. I tried something like:
$Obj->assertTrue(array_key_exists('#me', $response['entry']));
but it won't just work. Can someone give me a hint regarding this? Thank you
EDIT:
'#me' can be anywhere in the array, not on the first position and I'm asking this for a a test in codecept, so I need to an assert directly
You can loop through your result set to check it for each entry.
function checkEntries($object, $search) {
$object = json_decode($object, true);
foreach($object['entry'] as $entry) {
if ($entry['authorId'] == $search) {
return true;
}
}
return false;
}
Or you can use this:
PHP multidimensional array search by value

return single array from json by passing a value [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I get useful error messages in PHP?
(41 answers)
Closed 3 years ago.
I have a json that I need to filter a specific key and value from
the following json
{
"5": {
"Owner": "94EAC",
"Record":"0121ln"
},
"15": {
"Owner": "009AC",
"Record":"0120Pc"
},
"1": {
"Owner": "00G11A",
"Record":"000lPcn"
},
"199": {
"Owner": "00G1y9",
"Record":"01211cn"
},
"33": {
"Owner": "001AC",
"Record":"0121n"
}
}
I would like to be able to pass the first int and get back array for that number.
For example if I pass 15 I get
{
"Owner": "009AC",
"Record":"0120Pc"
}
I tried foreach loop but cannot set specific value for the first int
If I assign $data = json
then $date[15] didn't work
$data->15 also didn't work
I did also use the json decode and was able to print an array but wasn't able to get a single value
Any help would be great, I did spend all day and still cannot get an answer.
Thank you
Using Array:
$arr = json_decode($json, true);
print_r( $arr['15']);
Using Object:
$obj = json_decode($json);
print_r( $obj['15']);
Reference: json_decode

How to extract a series of variables from string of data in php? [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
The following chunk of data is produced by a webpage along with other html data;
"search":{"searchResults":{"results":[
{"id":"123","name":"ABC","rating":{"average":0,"count":2,"__typename":"Rating"},"category":"AAA/Cars","__typename":"ProductQuery"},
{"id":"456","name":"DEF","rating":{"average":5,"count":8,"__typename":"Rating"},"category":"BBB/Bikes","__typename":"ProductQuery"}
{"id": //and so on//
"}
]}}
How to extract multiple variables from this string like data like "id", "rating" etc., to be able to print it on another php page?
You can use json_decode to convert JSON to the array and then iterate through the array
$json = '{
"search":
{
"searchResults":
{
"results":[
{"id":"123","name":"ABC","rating":{"average":0,"count":2,"__typename":"Rating"},"category":"AAA/Cars","__typename":"ProductQuery"},
{"id":"456","name":"DEF","rating":{"average":5,"count":8,"__typename":"Rating"},"category":"BBB/Bikes","__typename":"ProductQuery"}
]
}
}
}';
$jsonToArray = json_decode($json,true);
foreach($jsonToArray['search']['searchResults']['results'] as $v){
echo $v['id'];
print_r($v['rating']);echo '<br/>';
}

how to access object array as show in code [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
how to access each file name...
{"Appointments":
[
{
"file_name":"eeea4a560492004c5c14f9414e88c32c.jpg",
"file_type":"image\/jpeg",
"file_path":"C:\/inetpub\/wwwroot\/nautSearch\/server\/uploadedfiles\/files\/",
"orig_name":"a46cf6866df6f358bece629ef472ac69.jpg",
"file_ext":".jpg",
"file_size":418.17,
"is_image":true,
"image_width":2560,
"image_height":1440,
"image_type":"jpeg",
"image_size_str":"width=\"2560\" height=\"1440\""
},
{
"file_name":"c08ff85a44e0f86ab48ec709e6ba1b4a.jpg",
"file_type":"image\/jpeg",
"file_path":"C:\/inetpub\/wwwroot\/nautSearch\/server\/uploadedfiles\/files\/",
"file_ext":".jpg",
"file_size":27.38,
"is_image":true,
"image_width":479,
"image_height":403,
"image_type":"jpeg",
"image_size_str":"width=\"479\" height=\"403\""
},
{
"file_name":"4536acd0479c6c5dee3b1f546ed8d328.jpg",
"file_type":"image\/jpeg",
"file_path":"C:\/inetpub\/wwwroot\/nautSearch\/server\/uploadedfiles\/files\/",
"file_ext":".jpg",
"file_size":27.38,
"is_image":true,
"image_width":479,
"image_height":403,
"image_type":"jpeg",
"image_size_str":"width=\"479\" height=\"403\""
}
]
}
What you gave is a json string. First you must decode it and loop over the appointments to use the file names:
$result = json_decode($yourJsonString);
foreach ($result->Appointments as $appointment) {
echo $appointment->file_name;
}

php json decode object with array of multiple objects [duplicate]

This question already has answers here:
Read JSON Data Using PHP [duplicate]
(5 answers)
Closed 7 years ago.
I am unable to get values of array of this json... how can i have all these values seperately!!
{
"id": "jai",
"pwd": "123",
"user": [
{
"fname": "jai",
"lname": "gupta"
},
{
"fname": "sameer",
"lname": "seth"
}
],
"college": "vit"
}
$myArray = json_decode($json, true);
var_dump($myArray['id']);
var_dump($myArray['user'][0]['fname']);
You can decode these values from json to object.
$result = json_decode('{"id":"jai","pwd":"123","user":[{"fname":"jai","lname":"gupta"},{"fname":"sameer","lname":"seth"}],"college":"vit"}');
and acccess like below:
$result->id;
$result->pwd;
You can access each "value" by accessing regular php array key value pairs.
$jsonn = '{"id":"jai","pwd":"123","user":[{"fname":"jai","lname":"gupta"},{"fname":"sameer","lname":"seth"}],"college":"vit"}';
$new = json_decode($jsonn, true);
$id = $new['id'];
$user = $new['user'];
..... and so on.
Hope this helps.
Cheers!

Categories