Parsing 2 head json and grouping using php - php

I have a json like this.But not getting an idea how to parse and display inside a select box using php under heading of name.Basically It has 2
name Customers,Lead.So each values will be displaying under these head.
First step is
$data = json_decode($json);
and a foreach will provide results ? but how to show head names ?
{
"status": {
"message": "success",
"code": 200
},
"clients": [
{
"values": [
{
"Code": "rdf",
"name": "Adfgf"
},
{
"Code": "fg",
"name": "fg"
}
],
"name": "Customers"
},
{
"values": [
{
"Code": "fgf",
"name": "fgdf"
},
{
"Code": "api",
"name": "fgfdgd"
},
{
"Code": "fgbb",
"name": "mnn"
},
{
"Code": "acy",
"name": "System"
}
],
"name": "Lead"
}
]
}

I have fixed the code
http://codepad.org/vLhzs6kJ#output
$string = '{
"status": {
"message": "success",
"code": 200
},
"clients": [
{
"values": [
{
"Code": "rdf",
"name": "first"
},
{
"Code": "fg",
"name": "second"
}
],
"name": "Customers"
},
{
"values": [
{
"Code": "fgf",
"name": "third"
},
{
"Code": "api",
"name": "4th"
},
{
"Code": "fgbb",
"name": "5th"
},
{
"Code": "acy",
"name": "last"
}
],
"name": "Lead"
}
]
}';
$data = json_decode($string,true);
echo "<Select>";
foreach ($data['clients'] as $c) {
$gp =$c['name'];
echo "<optgroup label=$gp>";
# calling the function on the values data
recurse($c['values']);
echo '</optgroup>';
}
function recurse($arr, $level = 0){
# we have a numerically-indexed array. go through each item:
foreach ($arr as $n) {
# print out the item ID and the item name
echo '<option value="' . $n['Code'] . '">'
. $n['name']
. '</option>'
;
}
}
echo "</Select>";

Related

Extract element from nested JSON

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.

parse extended json output file from cucumber with php

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

handaling JSON Data in php [duplicate]

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.

Parsing an array with PHP in a foreach loop

I want to parse an array with PHP's foreach loop to get the object names and values inside the 'ques' array.
[
{
"ques": [
{
"name": "comment",
"value": "comment me for the reason",
"sur_id": "1",
"user_id": "admin#gmail.com",
"pagename": "question_response"
},
{
"name": "check-box[]",
"value": "1"
},
{
"name": "radio",
"value": "radio 2"
},
{
"name": "yes",
"value": "no"
}
]
"ques":[
{
"name": "date",
"value": "2015-10-23"
},
{
"name": "select-deopdown",
"value": ""
},
{
"name": "true",
"value": "false"
},
{
"name": "number",
"value": "55"
}
]
}
]
I want to separate the value from the 'ques' array:
while ($fetch = mysql_fetch_array($query1)) {
$content = $fetch['CONTENT_VALUES'];
// print_r($content);
$content_value= mb_convert_encoding($content ,"UTF-8");
$datas = json_decode($content, true);
foreach($datas->ques as $values)
{
echo $values->value . "\n";
print_r($values);
}
$test[] = array('ques' => $datas ,'answer'=>$values);
}

Parse non array JSON with php

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

Categories