JSON Post PHP (TypeForm) - php

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;

Related

How can I serialize WhatsApp message payload to fetch the message body, phone_number_id, contact in Laravel php

{
"field": "messages",
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "16505551111",
"phone_number_id": "123456123"
},
"contacts": [
{
"profile": {
"name": "test user name"
},
"wa_id": "16315551181"
}
],
"messages": [
{
"from": "16315551181",
"id": "ABGGFlA5Fpa",
"timestamp": "1504902988",
"type": "text",
"text": {
"body": "this is a text message"
}
}
]
}
}
The easiest way is to save the response to a variable and get the data from the array as follows
$message = $responce['value']['message'];
$phone_number = $responce['value']['metadata']['phone_number_id'];
$contact = $responce['value']['contacts'];
You can also decode the response first and then you can easily do the following
$res = json_decode($responce);
$message = $res->value->message;
$phone_number= $res->value->metadata->phone_number_id;
$contact = $res->value->contacts;
But if you want to convert the JSON you have to a Laravel object I recommend checking out the following answer.

How to replace JSON key without losing its value and position using PHP

This is the JSON
{
"table-name": "Kiwi",
"created-on": "November 20, 2021",
"columns": {
"Info": {
"type": "longtext",
"extra": ""
},
"Status": {
"type": "droplist",
"extra": ""
},
"Task": {
"type": "text",
"extra": ""
}
},
"data": [
{
"Name": "Team Reports",
"Info": "Submitting marketing materials reports",
"Status": "Completed"
},
{
"Name": "Fabia HR",
"Info": "Brian asking for a report",
"Status": "Pending"
},
{
"Name": "Fabia",
"Info": "Meeting with CEO #cafe 9:00",
"Status": "Cancelled"
}
]
}
And I was trying to achieve to rename the "Info" into "Description" without losing its value and array position. I'm not very familiar with array_replace , that seems my code is not working. Please share some codes, it will be much appreciated.
Here my PHP code I tried
<?PHP
$jsn = file_get_contents('./test.json');
$arr = json_decode($jsn, true);
$newArray= [];
foreach($arr['data'] as $row){
$row['Description'] = $row['Info'];
array_push($newArray, $row);
}
//print_r($newArray);
array_replace($arr['data'],$newArray);
echo json_encode($arr, JSON_PRETTY_PRINT);
Thank you so much for your attention and advance help. It will really gonna save me some time. Noob here and I'm still learning things about PHP and JSON

php parse a Json file [duplicate]

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;
}

Pushing object to nested JSON using PHP?

In the following JSON object, I have two dummy products and a nested group of reviews that are siblings to one another:
product.json
[
{
"name": "Dodecahedron",
"price": 2.95,
"description": "This gem is awesome and has 10 sides.",
"images": [
{
"full": "dodecahedron-01-full.jpg",
"thumb": "dodecahedron-01-thumb.jpg"
}
],
"reviews": [
{
"stars": 5,
"body": "I love this product!",
"author": "joe#thomas.com"
},
{
"stars": 1,
"body": "This product sucks",
"author": "tim#hater.com"
}
]
},
{
"name": "Hectahedron",
"price": 8.95,
"description": "Wonderful 6-sided gem that will please all.",
"images": [
{
"full": "hectahedron-01-full.jpg",
"thumb": "hectahedron-01-thumb.jpg"
}
],
"reviews": [
{
"stars": 4,
"body": "product is awesome, seriously!",
"author": "james#crazy.com"
},
{
"stars": 2,
"body": "Seriously sucks, would give 0 if i could",
"author": "john#hater.com"
}
]
}
]
I am using AngularJS to send the newly created JS review object from an HTML form to PHP. But how in PHP do you push this review data to become a sibling IN "reviews" AND target the exact product it should be in? I'm very new to PHP and would greatly appreciate your guidance!
If I understand what you are asking, here you need to use the json_decode() function:
$productsRreviews = json_decode($_POST['reviews'], true);
This will give you a PHP associative array that you can process to do whatever you need to do. For instance:
foreach ($productsReviews as $productReviews) {
$name = $productReviews['name'];
$price = $productReviews['price'];
$reviews = $productReviews['reviews'];
foreach ($reviews as $review) {
$stars = $review['stars'];
...
}
}
Hope that helps!

imploding array_keys from Facebook JSON data

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),",")

Categories