This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
i'm using this API called "pollDaddy",
using php to retrieve json responses ..I've come across a little hiccup..
How do you get the total of each answer?
My json data:
{
"pdResponse": {
"partnerGUID": "3F2504E0-4F89-11D3-9A0C-0305E82C3301",
"userCode": "123456-FErKS5yu15scpSGmvip4JA==",
"demands": {
"demand": {
"result": {
"answers": {
"answer": [{
"text": "Yes",
"id": "23123124",
"total": "1074",
"percent": "89.13"
}, {
"text": "No",
"id": "23123125",
"total": "131",
"percent": "10.87"
}]
}, "id": "690432265"
}, "id": "GetPollResults"
}
}
}
}
First we have to decode the json data. so we use json_decode. Then we select the right element and loop through it to get all the anwers
$data = '{ "pdResponse": { "partnerGUID": "3F2504E0-4F89-11D3-9A0C-0305E82C3301", "userCode": "123456-FErKS5yu15scpSGmvip4JA==", "demands": { "demand": { "result": { "answers": { "answer": [{ "text": "Yes", "id": "23123124", "total": "1074", "percent": "89.13" }, { "text": "No", "id": "23123125", "total": "131", "percent": "10.87" }] }, "id": "690432265" }, "id": "GetPollResults" } } } }';
$response = json_decode($data);
$answers = $response->pdResponse->demands->demand->result->answers->answer;
foreach($answers as $a)
{
echo $a->total;
}
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 10 months ago.
I've a GET request in PHP that receives data in JSON as below.
{
"data": {
"id": "1cc58ad2-ccfd-4ede-a6a6-35809c81cb8a",
"type": "payment",
"attributes": {
"status": "Sent",
"created_at": "2022-05-02T08:57:50.171+03:00",
"amount": {
"currency": "KES",
"value": "500.0"
},
"transfer_batches": [{
"status": "Sent",
"amount": "500.0",
"disbursements": [{
"amount": "500.0",
"status": "Transferred",
"origination_time": "2022-05-02T08:57:50.171+03:00",
"transaction_reference": "1651471070",
"destination_type": "till",
"destination_reference": "3eccf0c1-ab6b-41a4-b51a-4e8f588105f1"
}]
}],
"metadata": {
"something": "TST-01",
"something_else": "Something else"
},
"_links": {
"callback_url": "http://portal.zarafu.com/payments",
"self": "https://sandbox.kopokopo.com/api/v1/payments/1cc58ad2-ccfd-4ede-a6a6-35809c81cb8a"
}
}
}
}
How can I filter the transaction_reference and status.
I have tried..
$jsonData = json_decode($response);
echo '<pre>';
echo $jsonData->status;
echo '</pre>';
You can't get the status like that. You have to use the format of your json. Exemple for the status :
echo $jsonData->data->attributes->status;
For transaction reference, it's :
echo $jsonData->data->attributes->transfer_batches->0->disbursements->0->transaction_reference;
I put '0' because it's arrays
I have never used JSON before so apologies if this is a simple request.
I have a webhook setup that sends me a JSON Post (Example Below) - I want to extract the two answers from this "text":"250252" & {"label":"CE"}
{
"event_id": "1",
"event_type": "form_response",
"form_response": {
"form_id": "VpWTMQ",
"token": "1",
"submitted_at": "2018-05-22T14:11:56Z",
"definition": {
"id": "VpWTMQ",
"title": "SS - Skill Change",
"fields": [
{
"id": "kUbaN0JdLDz8",
"title": "Please enter your ID",
"type": "short_text",
"ref": "9ac66945-899b-448d-859f-70562310ee5d",
"allow_multiple_selections": false,
"allow_other_choice": false
},
{
"id": "JQD4ksDpjlln",
"title": "Please select the skill required",
"type": "multiple_choice",
"ref": "a24e6b58-f388-4ea9-9853-75f69e5ca337",
"allow_multiple_selections": false,
"allow_other_choice": false
}
]
},
"answers": [
{
"type": "text",
"text": "250252",
"field": {
"id": "kUbaN0JdLDz8",
"type": "short_text"
}
},
{
"type": "choice",
"choice": {
"label": "CE"
},
"field": {
"id": "JQD4ksDpjlln",
"type": "multiple_choice"
}
}
]
}
}
I have this currently in my PHP file:
$data = json_decode(file_get_contents('php://input'));
$ID = $data->{"text"};
$Skill = $data->{"label"};
This does not work and all I get is null - Any help would really be appreciated, Thank You.
You need to look at the JSON object you're receiving to know the structure of the object you're receiving after using json_decode, what you're trying to get is in $data->form_response->answers, So you can have a variable for easier access:
$answers = $data->form_response->answers;
remember $answers is an array
So to achieve what you're trying to get, you can do:
$data = json_decode(file_get_contents('php://input'));
$answers = $data->form_response->answers;
$ID = $answers[0]->text;
$Skill = $answers[1]->choice->label;
How I can get text from below JSON.
https://jsfiddle.net/7h6a55m6/2/ JSON Data
Need to get "Testing","Testing2", etc. Stuck in multi level JSON style. Whats the easiest way to do
foreach ($response->data as $res) {
echo $res['comments'];
}
Use the json_decode().Something like below
$test_json='
{ "media":
{ "data":
[
{ "comments":
{ "data":
[
{ "text": "Testing", "id": "17935572247064063" },
{ "text": "Testing2", "id": "17909467621160083" },
{ "text": "Testing3", "id": "17879193508206704" },
{ "text": "Testing4", "id": "17936230114007492" },
{ "text": "Testing5", "id": "17861359981236880" },
{ "text": "Testing6", "id": "17932586956016890" },
{ "text": "Testing7", "id": "17920569544116678" },
{ "text": "Testing8", "id": "17933592700059204" }
]
}
}
]
}
}
';
$test_json=json_decode($test_json,true);
foreach($test_json['media']['data'][0]['comments']['data'] as $row){
print_r($row['text']);
}
try
$responseData = json_decode($response->data, true);
$result = $responseData['media']['data'][0]['comments']['data'];
foreach($result as $data) {
// do your stuff here
}
I'm trying to create a game API thing that requires this decoded, but I'm not sure how (this is just for a certain user, so the values wont be the same)
[
{
"Id": 382779,
"Name": "DarkAge Ninjas"
},
{
"Id": 377291,
"Name": "Emerald Knights of the Seventh Sanctum"
},
{
"Id": 271454,
"Name": "Knights of RedCliff"
},
{
"Id": 288278,
"Name": "Knights of the Splintered Skies "
},
{
"Id": 375307,
"Name": "Korblox's Empire"
},
{
"Id": 387867,
"Name": "Ne'Kotikoz"
},
{
"Id": 696519,
"Name": "Orinthians"
},
{
"Id": 27770,
"Name": "Retexture Artists Official Channel"
},
{
"Id": 585932,
"Name": "Retexturing Apprentices "
},
{
"Id": 7,
"Name": "Roblox"
},
{
"Id": 679727,
"Name": "ROBLOX Community Staff and Forum Users"
},
{
"Id": 127081,
"Name": "Roblox Wiki"
}
]
How can I decode this in PHP so It has a list like
DarkAge Ninjas
Emerald Knights of the Seventh Sanctum
Knights of RedCliff
etc, and have the Id decoded separately so I can make a clickable link out of it :/
You will need json_decode to turn json into php array
$api_json = '[
{ "Id": 382779, "Name": "DarkAge Ninjas" },
{ "Id": 377291, "Name": "Emerald Knights of the Seventh anctum" }
...
]';
$api_data = json_decode($api_json, true);
//Now you can loop over the array and print the `Name`
foreach($api_data as $d) {
echo $d['Name'];
}
above code will output
DarkAge Ninjas
Emerald Knights of the Seventh Sanctum
Knights of RedCliff
...
To make link with ids just add this in above loop
echo ''. $d['Name'].'';
as Ed Cottrell suggested, Read the manual: json_decode to know more
Need some help with the sample code provided the facebook. I can't get it to return a series of IDs that I need to run a sql query against.
$friends = '{
"data": [
{
"name": "Paul",
"id": "12000"
},
{
"name": "Bonnie",
"id": "120310"
},
{
"name": "Melissa",
"id": "120944"
},
{
"name": "Simon",
"id": "125930"
},
{
"name": "Anthony",
"id": "120605"
},
{
"name": "David",
"id": "120733"
}
]
}';
$obj = json_decode($friends);
print $obj->{'data'}[0]->{'name'};
I can return the "Paul"
what I want is to return all the id's using implode(array_keys($obj),",")
I am only getting data to return.
What I'd like to do is retrieve all the IDs separated by a comma.
Thanks!
Try implode-ing on the data key with array_map:
function get_id($o) {
return $o->id;
}
implode(array_map('get_id', $obj->data),",")