This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 6 years ago.
I got a json data in return in my php script and this json data is stored in my variable. Now i would like to check the "price" for name "express" in php. Please guide me the way to extract the data in php.
$serviceTypesJSON = json_decode($rawBody, true);
{
"services": {
"service": [
{
"code": "INT_PARCEL_COR_OWN_PACKAGING",
"name": "Courier",
"price": "85.13",
"max_extra_cover": 5000,
"options": {
"option": [
{
"code": "INT_TRACKING",
"name": "Tracking"
},
{
"code": "INT_SMS_TRACK_ADVICE",
"name": "SMS track advice"
},
{
"code": "INT_EXTRA_COVER",
"name": "Extra Cover"
}
]
}
},
{
"code": "INT_PARCEL_EXP_OWN_PACKAGING",
"name": "Express",
"price": "40.13",
"max_extra_cover": 5000,
"options": {
"option": [
{
"code": "INT_TRACKING",
"name": "Tracking"
},
{
"code": "INT_SIGNATURE_ON_DELIVERY",
"name": "Signature on delivery"
},
{
"code": "INT_SMS_TRACK_ADVICE",
"name": "SMS track advice"
},
{
"code": "INT_EXTRA_COVER",
"name": "Extra Cover"
}
]
}
},
{
"code": "INT_PARCEL_STD_OWN_PACKAGING",
"name": "Standard",
"price": "31.40",
"max_extra_cover": 5000,
"options": {
"option": [
{
"code": "INT_TRACKING",
"name": "Tracking"
},
{
"code": "INT_EXTRA_COVER",
"name": "Extra Cover"
},
{
"code": "INT_SIGNATURE_ON_DELIVERY",
"name": "Signature on delivery"
},
{
"code": "INT_SMS_TRACK_ADVICE",
"name": "SMS track advice"
}
]
}
},
{
"code": "INT_PARCEL_AIR_OWN_PACKAGING",
"name": "Economy Air Parcels",
"price": "23.77",
"max_extra_cover": 500,
"options": {
"option": [
{
"code": "INT_EXTRA_COVER",
"name": "Extra Cover"
},
{
"code": "INT_SIGNATURE_ON_DELIVERY",
"name": "Signature on delivery"
}
]
}
}
]
}
}
Now how to extract the "name": "Express", "price": "40.13", from this variable? I would like to take the express, standard, Economy Air Parcels price. Please help me how to extract the exact data from this json mixed data
var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}';
var obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.name + "<br>" +
obj.street + "<br>" +
obj.phone;
You can use this method.
Related
There is json response:
{
"id": "1234567890123456789",
"creation_date": 12345678,
"event": "WAITING_PAYMENT",
"version": "2.0.0",
"data": {
"product": {
"id": 213344,
"name": "Product Name",
"has_co_production": false
},
"affiliates": [
{
"name": "Affiliate name"
}
],
"buyer": {
"email": "buyer#email.com"
},
"producer": {
"name": "Producer Name"
},
"commissions": [
{
"value": 0.65,
"source": "MARKETPLACE"
},
{
"value": 3.10,
"source": "PRODUCER"
}
],
"purchase": {
"approved_date": 1231241434453,
"full_price": {
"value": 134.0
},
"original_offer_price": {
"currency_value": "EUR"
"value": 100.78,
},
"price": {
"value": 150.6
},
"order_date": "123243546",
"status": "STARTED",
"transaction": "HP02316330308193",
"payment": {
"billet_barcode": "03399.33335 33823.303087 198801027 2 876300015000",
"billet_url": "https://billet-link.com/bHP023163303193",
}
},
"subscription": {
"status": "ACTIVE",
"plan": {
"name": "plan name"
},
"subscriber": {
"code": "12133421"
}
}
}
}
My question is how to extract data["buyer"]["email"] in PHP ?
I only need to extract the email information from the buyer table inside the data table.
First, you need to decode the json to a PHP array (or an object), then you can access the requested information from the decoded data.
$data = json_decode('the json string most place here', true);
$email = $data['buyer']['email'];
Place your json string in the first argument of json_decode() function.
i am tring to parse a special json content. I got this as an output file from a cucumber execution. The goal is it to decode some values like the name the status and some other content. How can i encode that.
Another concern would be a transformation of json into a CSV.
It is not important for me to use php. Java or Perl would be an alternative.
This file I am gonna call in php with:
$jsonconntent = file_get_contents("/home/xxx/test1.json");
The json conten looks like this (I posted only the beginning):
[
{
"uri": "features/complete_ski.feature",
"id": "complete_ski_with_time",
"keyword": "Feature",
"name": "Complete_Ski_with_time",
"description": "",
"line": 1,
"elements": [
{
"id": "complete_ski_with_time;time_part_preamble",
"keyword": "Scenario",
"name": "time_part_preamble",
"description": "",
"line": 3,
"type": "scenario",
"before": [
{
"output": [
"Default Timestamp start: 1516024716000"
],
"match": {
"location": "features/support/env.rb:32"
},
"result": {
"status": "passed",
"duration": 191690
}
},
{
"match": {
"location": "capybara-2.17.0/lib/capybara/cucumber.rb:13"
},
"result": {
"status": "passed",
"duration": 52117
}
},
{
"match": {
"location": "capybara-2.17.0/lib/capybara/cucumber.rb:21"
},
"result": {
"status": "passed",
"duration": 25885
}
}
],
"steps": [
{
"keyword": "Given ",
"name": "a Android A-Party",
"line": 4,
"output": [
"Got handset with number unvisable, IMSI: notfor, android-Id: yourfone, VNC: 11111, port: 9981"
],
"match": {
"location": "features/step_definitions/idrp_steps.rb:11"
},
"result": {
"status": "passed",
"duration": 1415024760
},
"after": [
{
"match": {
"location": "features/support/env.rb:24"
},
"result": {
"status": "passed",
"duration": 264339
}
}
]
}
Use:
$data = json_decode($jsonconntent, true);
You will have the data with javascript objects as arrays, not PHP objects.
To save it as CSV, it depends on the structure of the data. In this case, it is complicated:
{
"uri": "features/complete_ski.feature",
"id": "complete_ski_with_time",
"keyword": "Feature",
"name": "Complete_Ski_with_time",
"description": "",
"line": 1,
"elements": [
{
"id": "complete_ski_with_time;time_part_preamble",
"keyword": "Scenario",
"name": "time_part_preamble",
"description": "",
"line": 3,
...
How do you put the data in the column elements? Data is so nested that it cannot be transformed into a tabular format with column headers and one value per column in each row.
As to how to access the data, you can do this:
$first_element_id = $data[0]['elements'][0]['id'];
foreach ( $data as $item ) {
$uri = $item['uri'];
foreach ( $item['elements'] as $element ) {
$name = $element['name'];
}
}
As asked in one of the comments, this is how to access the 'Default Timestamp start':
foreach ($data as $item) {
foreach ($item['elements'] as $element) {
foreach ($element['before'] as $before) {
if (isset($before['output'])) {
foreach ($before['output'] as $output) {
echo sprintf("%s\n", $output);
}
}
}
}
}
I am looking for a way to retrieve the number of likes of a Facebook post, based on its post_id. I got hold of a php code from this forum itself..and it's something like
<?php
function fetchUrl($url){
return file_get_contents($url);
}
$json_object = fetchUrl("https://graph.facebook.com/{post_id}/likes?access_token={token}&limit=5000000"); //
$feedarray = json_decode($json_object, true);
$likesNum = count($feedarray['data']); // return the number of items in `data` array
print $likesNum;
?>
But the problem is this method does not retrieve the total likes of a post, since likes are displayed in blocks of 1000, after which theres another link to a different page containing the next set of 1000 likes and so on.
Is there a way to get the total number of likes of a facebook post by a single query?
Add the summary flag ** summary=true**
"https://graph.facebook.com/{post_id}/likes?access_token={token}&summary=true"
{
"data": [
{
"id": "663342380429664",
"name": "Luis Mendoza"
},
{
"id": "1532100840406448",
"name": "Sakazuki Akainu"
},
{
"id": "780666205412913",
"name": "Joaito KoRn"
},
{
"id": "1060933433919925",
"name": "Adrian Sosa"
},
{
"id": "860704407276452",
"name": "Sarah Rosenstrauch"
},
{
"id": "1947974762009431",
"name": "David Prieto"
},
{
"id": "804864302928112",
"name": "Ronal Ortega"
},
{
"id": "1505075359814934",
"name": "Gonzalo Larzen"
},
{
"id": "1431207613804483",
"name": "Victor Clc"
},
{
"id": "508785009283633",
"name": "Rxdry EzDe Cerrx Mcmxii"
},
{
"id": "435355413265946",
"name": "Ángel Fernando Huillca Alonso"
},
{
"id": "163773913961445",
"name": "Pelado Miguel Pin Macias"
},
{
"id": "1479227465674392",
"name": "Releck Solitario"
},
{
"id": "161610054193539",
"name": "MD Sahin MD Sahin"
},
{
"id": "798431050242097",
"name": "Brian Nahuel"
},
{
"id": "624869574305480",
"name": "Saul Alfredo"
},
{
"id": "1642733362665392",
"name": "Junior Zurita"
},
{
"id": "134907406871404",
"name": "Wil Peña"
},
{
"id": "10153052770952668",
"name": "Miguel Peña Cáceres"
},
{
"id": "1461494580846182",
"name": "Darian Suarez"
},
{
"id": "365762500250317",
"name": "Igarashi Ganta"
},
{
"id": "750032685093387",
"name": "Camila Barbé"
},
{
"id": "781013541941152",
"name": "Gonzalo Nievas"
},
{
"id": "756520927743339",
"name": "Jonathan C. Duran Cuellar"
},
{
"id": "1504488093199860",
"name": "Maxi Russo"
}
],
"paging": {
"cursors": {
"before": "NjYzMzQyMzgwNDI5NjY0",
"after": "MTUwNDQ4ODA5MzE5OTg2MAZDZD"
},
"next": "https://graph.facebook.com/v2.3/1009501939072385/likes?access_token=TOKEN..."
},
"summary": {
"total_count": 4303
}
}
I am developing a backend in php+codeigniter which connects to a data providing service that returns a JSON such as the following:
{
"code": "36397_26320",
"type": "TEAMS",
"4522": {
"id": "4522",
"code": "324",
"name": "IT24354"
},
"4524": {
"id": "4524",
"code": "1234",
"name": "IT24234"
},
"4527": {
"id": "4527",
"code": "2134",
"name": "IT2678"
},
"4529": {
"id": "4529",
"code": "653",
"name": "IT3546",
"info":{
"type1":
{
"type":"1",
"url":"www.someurl.com",
"date":"some date"
},
"type2":
{
"type":"2",
"url":"www.someurl.com",
"date":"some date"
}
}
},
"4530": {
"id": "4530",
"code": "3456",
"name": "IT8769"
},
"4534": {
"id": "4534",
"code": "6453",
"name": "IT3456"
},
"4537": {
"id": "4537",
"code": "76856",
"name": "IT2676"
},
"4540": {
"id": "4540",
"code": "5768",
"name": "IT23454"
},
"16225": {
"id": "16225",
"code": "4675",
"name": "IT90687"
}
}
And I want to get the info inside those number identifiers such that the output JSON encoded would look the following way:
{
"items": [
{
"id": "4522",
"code": "324",
"name": "IT24354"
},
{
"id": "4524",
"code": "1234",
"name": "IT24234"
},
{
"id": "4527",
"code": "2134",
"name": "IT2678"
},
{
"id": "4529",
"code": "653",
"name": "IT3546",
"info":[
{
"type":"1",
"url":"www.someurl.com",
"date":"some date"
},
{
"type":"2",
"url":"www.someurl.com",
"date":"some date"
}
]
},
{
"id": "4530",
"code": "3456",
"name": "IT8769"
},
{
"id": "4534",
"code": "6453",
"name": "IT3456"
},
{
"id": "4537",
"code": "76856",
"name": "IT2676"
},
{
"id": "4540",
"code": "5768",
"name": "IT23454"
},
{
"id": "16225",
"code": "4675",
"name": "IT90687"
}
]
}
My issue is that for example, I do not know if the item has the field "info" such as in item 4529, or also at the item identifier level, there are two fields called code and type that have no further information.
Is there any easy way to do such operation? or is performing a foreach for as many levels as I want to obtain the only way to do it? how do I identify if the key in the json contains more key value pairs?
Thank you all!
Taking the first json object you supplied and putting it in the $json variable for this example:
<?php
$json = '{
"code": "36397_26320",
"type": "TEAMS",
"4522": {
"id": "4522",
"code": "324",
"name": "IT24354"
},
"4524": {
"id": "4524",
"code": "1234",
"name": "IT24234"
},
"4527": {
"id": "4527",
"code": "2134",
"name": "IT2678"
},
"4529": {
"id": "4529",
"code": "653",
"name": "IT3546",
"info":{
"type1":
{
"type":"1",
"url":"www.someurl.com",
"date":"some date"
},
"type2":
{
"type":"2",
"url":"www.someurl.com",
"date":"some date"
}
}
},
"4530": {
"id": "4530",
"code": "3456",
"name": "IT8769"
},
"4534": {
"id": "4534",
"code": "6453",
"name": "IT3456"
},
"4537": {
"id": "4537",
"code": "76856",
"name": "IT2676"
},
"4540": {
"id": "4540",
"code": "5768",
"name": "IT23454"
},
"16225": {
"id": "16225",
"code": "4675",
"name": "IT90687"
}
}';
?>
Now we can create a json object to access the data:
$data = json_decode($json);
Allowing us to itterate through that data object and see if info is set:
foreach($data as $item){
if(isset($item->info)){
// info found...do what you need to...
print $item->id . " <br />";print_r($item->info);
}
}
Warranting a return of this:
4529
stdClass Object
(
[type1] => stdClass Object
(
[type] => 1
[url] => www.someurl.com
[date] => some date
)
[type2] => stdClass Object
(
[type] => 2
[url] => www.someurl.com
[date] => some date
)
)
Example
Thank you all very much, It is a bit tricky but I ended up doing the following, since it allows me to explore the complete json without knowing if key value is another JSON. Right now it returns the same input json, but to change output to array just add brackets to the return value when recursing
$response[$key][] = $this->read_non_array_json(json_encode($value));
Improvements highly welcomed:
private function read_non_array_json($data)
{
$json = json_decode($data);
if(is_object($json))
{
if(isset($json->id))
{
//obtain values, use a global array maybe to save info or something...
$id = $json->id;
$code = $json->code;
$name = $json->name;
}
if(isset($json->info)
{
//more code...
}
foreach($json as $key => $value)
{
if(is_object($value))
{
$response[$key]= $this->read_non_array_json(json_encode($value));
}
else
{
$response[$key] = $value;
}
}
return $response;
}
else
{
return $data;
}
}
Below is the JSON object I am trying to parse. It its basically the answers to a dynamic form. I know I can convert it to an array and then work with it. The issue I am having is the JSON object will be different every time.
{
"sections": [
{
"elements": [
{
"element": "greeting",
"components": {
"paragraph": {
"content": "Thank you for calling company name. This is a test call."
}
}
},
{
"element": "custom-fullname",
"components": {
"label": {
"content": ""
},
"first_name": {
"name": "1[custom-fullname][first_name]",
"label": "First Name",
"type": "text",
"value": "John",
"required": ""
},
"last_name": {
"name": "1[custom-fullname][last_name]",
"label": "Last Name",
"type": "text",
"value": "smith",
"required": ""
},
"help_text": {
"content": ""
}
}
},
{
"element": "custom-phone",
"components": {
"label": {
"content": "Phone Number"
},
"phone_ext": {
"name": "2[custom-phone][phone_ext]",
"type": "text",
"value": ""
},
"input_format": {
"name": "2[custom-phone][phone_format]",
"type": "hidden",
"value": "standard"
},
"help_text": {
"content": ""
},
"phone": {
"value": "7864564444",
"required": "false",
"name": "2[custom-phone][phone]"
}
}
},
{
"element": "custom-message",
"components": {
"label": {
"content": "Message"
},
"message": {
"name": "3[custom-message][message]",
"type": "text",
"value": "test time 22222",
"required": ""
},
"help_text": {
"content": ""
}
}
},
{
"element": "notes",
"components": {
"paragraph": {
"content": "This is a test call. Please mark this as a test."
}
}
}
],
"faqs": [
{
"question": "Should i have a FAQ?",
"answer": "Yes, this will help the agent to have a fluent conversation with the caller."
}
]
},
{
"elements": [
{
"element": "greeting",
"components": {
"paragraph": {
"content": "Thank you for calling company name. This is a test call."
}
}
},
{
"element": "custom-fullname",
"components": {
"label": {
"content": ""
},
"first_name": {
"name": "1[custom-fullname][first_name]",
"label": "First Name",
"type": "text",
"value": "robert",
"required": ""
},
"last_name": {
"name": "1[custom-fullname][last_name]",
"label": "Last Name",
"type": "text",
"value": "gonzalez",
"required": ""
},
"help_text": {
"content": ""
}
}
},
{
"element": "custom-phone",
"components": {
"label": {
"content": "Phone Number"
},
"phone_ext": {
"name": "2[custom-phone][phone_ext]",
"type": "text",
"value": ""
},
"input_format": {
"name": "2[custom-phone][phone_format]",
"type": "hidden",
"value": "standard"
},
"help_text": {
"content": ""
},
"phone": {
"value": "7864564444",
"required": "false",
"name": "2[custom-phone][phone]"
}
}
},
{
"element": "custom-message",
"components": {
"label": {
"content": "Message"
},
"message": {
"name": "3[custom-message][message]",
"type": "text",
"value": "Test",
"required": ""
},
"help_text": {
"content": ""
}
}
},
{
"element": "notes",
"components": {
"paragraph": {
"content": "This is a test call. Please mark this as a test."
}
}
}
],
"faqs": [
{
"question": "Should i have a FAQ?",
"answer": "Yes, this will help the agent to have a fluent conversation with the caller."
}
]
},
{
"elements": [
{
"element": "greeting",
"components": {
"paragraph": {
"content": "Thank you for calling company name. This is a test call."
}
}
},
{
"element": "custom-fullname",
"components": {
"label": {
"content": ""
},
"first_name": {
"name": "1[custom-fullname][first_name]",
"label": "First Name",
"type": "text",
"value": "robert",
"required": ""
},
"last_name": {
"name": "1[custom-fullname][last_name]",
"label": "Last Name",
"type": "text",
"value": "gonzalez",
"required": ""
},
"help_text": {
"content": ""
}
}
},
{
"element": "custom-phone",
"components": {
"label": {
"content": "Phone Number"
},
"phone_ext": {
"name": "2[custom-phone][phone_ext]",
"type": "text",
"value": ""
},
"input_format": {
"name": "2[custom-phone][phone_format]",
"type": "hidden",
"value": "standard"
},
"help_text": {
"content": ""
},
"phone": {
"value": "7861238975",
"required": "false",
"name": "2[custom-phone][phone]"
}
}
},
{
"element": "custom-message",
"components": {
"label": {
"content": "Message"
},
"message": {
"name": "3[custom-message][message]",
"type": "text",
"value": "test time 22222",
"required": ""
},
"help_text": {
"content": ""
}
}
},
{
"element": "radio",
"components": {
"label": {
"content": "Whats type of office are you looking for?"
},
"radiolist": [
{
"input": {
"value": "value_0",
"id": "single_choice_answer_0"
},
"label": {
"content": "Dedicated Space"
}
},
{
"input": {
"value": "value_1",
"id": "single_choice_answer_1"
},
"label": {
"content": "Parttime Space"
}
}
],
"single_choice_answer": {
"value": "value_0",
"name": "4[radio][single_choice_answer]",
"required": ""
},
"help_text": {
"content": ""
}
}
}
],
"faqs": [
{
"question": "Should i have a FAQ?",
"answer": "Yes, this will help the agent to have a fluent conversation with the caller."
}
]
}
]
}
So, for example a part of the JSON object looks like the following.
"components": {
"label": {
"content": "Whats type of office are you looking for?"
},
"radiolist": [
{
"input": {
"value": "value_0",
"id": "single_choice_answer_0"
},
"label": {
"content": "Dedicated Space"
}
},
{
"input": {
"value": "value_1",
"id": "single_choice_answer_1"
},
"label": {
"content": "Parttime Space"
}
}
],
"single_choice_answer": {
"value": "value_0",
"name": "4[radio][single_choice_answer]",
"required": ""
},
I would like to take that and do something like the following:
{
"components": {
"label": {
"content": "Whats type of office are you looking for?"
},
"answer": {
"value": "value_0",
"name": "Dedicated Space",
"required": ""
}
}
Really I just need the form questions and answer formatted a little nicer in a JSON object. However, I'm not sure how to code this in PHP.