Making a tree map with JSON data (Highcharts) - php

I am trying to bring data from the database to this Highcharts tree map, I have no idea how to format the JSON part of the code. When there is a clear xAxis and yAxis it is relatively easy. We use a php assembler to get all of the data from the query. I took the code straight from a Highcharts column chart with JSON that was working normally, I tried myself to update it and it keeps telling me the $.getJSON bit is an unexpected identifier.
$(function () {
// Drop-in fix:
Highcharts.seriesTypes.treemap.prototype.getExtremesFromAll = true;
$('#container').highcharts({
colorAxis: {
minColor: ' #000000',
maxColor: '#ffffff'
},
tooltip: {
backgroundColor: '#fffff',
formatter: function () {
return this.point.name + ":" + this.point.value;
}
}
$.getJSON("/charts/data/homepage-charts-data.php?<?php echo $_SERVER['QUERY_STRING']; ?>", function(json) {
options.categories = json[0]['data'];
options.series[0] = json[1];
chart = new Highcharts.Chart(options);
});
});
});
The code that formats the data (from: /charts/data/homepage-charts-data.php)
function displayJsonHomepageBallotMeasuresDataChart ($aaQuery) {
global $NIMSPv4;
$result = $NIMSPv4->query("$aaQuery");
$categories = array();
$categories['name'] = 'Election_Jurisdiction';
$series = array();
$series['name'] = 'Total_$';
while($row = $result->fetch_assoc()) {
$categories['data'][] = $row['Election_Jurisdiction'];
$series['data'][] = $row['Total_$'];
}
$result = array();
array_push($result,$categories);
array_push($result,$series);
print json_encode($result, JSON_NUMERIC_CHECK);
} // END CHART FUNCTION
Update: Here is the JSON I am trying to pull data from. Election_Jurisdiction and Total_$ is what I am trying to access for now, Candidate and Office_Sought could also be brought down as well considering it is a tree map and I will need to show the race.
{
"metaInfo": {
"format": "json",
"completeness": {
"allReports": "297",
"availableReports": "297",
"completeReports": "300",
"incompleteAvailable": "-3",
"lastUpdated": "2015-11-04 09:23:28",
"mostRecentReportDate": "2013-01-31"
},
"paging": {
"pageLink": "p=0",
"minPage": 0,
"currentPage": 0,
"maxPage": 0,
"totalPages": 1,
"totalRecords": "3",
"recordsPerPage": 100,
"recordsThisPage": 3
},
"grouping": {
"groupLink": "gro=c-t-id",
"currentGrouping": {
"c-t-id": "Candidate (General)"
},
"availableGrouping": {
"f-s": "Filing Jurisdiction (General)",
"f-eid": "Filer (General)",
"f-y": "Filing Year (Advanced)",
"c-t-i": "Incumbency Data (Advanced)",
"c-t-icod": "Incumbency Advantage (Advanced)",
"d-nme": "Original Name (Advanced)",
"d-amt": "Amount (Advanced)",
"d-dte": "Date (Advanced)",
"d-ludte": "Last Updated (Advanced)",
"d-typ": "Type of Transaction (Advanced)",
"d-llink": "Lobbying Entity? (Advanced)",
"d-ad-str": "Street (Advanced)",
"c-t-sts": "Status of Candidate (Candidates)",
"d-id": "Record (Contributors)",
"d-eid": "Contributor (Contributors)",
"d-et": "Type of Contributor (Contributors)",
"d-ccg": "Broad Sector (Contributors)",
"d-cci": "General Industry (Contributors)",
"d-ccb": "Specific Business (Contributors)",
"d-ad-cty": "City (Contributors)",
"d-ad-st": "State (Contributors)",
"d-ad-zip": "Zip (Contributors)",
"d-ins": "In-Jurisdiction (Contributors)",
"d-empl": "Employer (Contributors)",
"d-occupation": "Occupation (Contributors)"
}
},
"sorting": {
"sortLink": "so=u-tot&sod=0",
"currentSorting": {
"u-tot": "Total_$"
},
"sortingDirection": "descending",
"availableSorting": {
"c-t-id": "Candidate",
"c-t-pt": "Specific_Party",
"c-t-p": "General_Party",
"s": "Election_Jurisdiction",
"y": "Election_Year",
"c-r-t": "Election_Type",
"c-r-osid": "Office_Sought",
"c-t-ftsts": "Election_Status",
"c-t-ico": "Incumbency_Status",
"u-rec": "#_of_Records"
}
},
"recordFormat": {
"request": "c-r-id=16739",
"Candidate": {
"token": "c-t-id",
"id": "token value",
"Candidate": "display value"
},
"Candidate_Entity": {
"token": "c-t-eid",
"id": "token value",
"Candidate_Entity": "display value"
},
"Specific_Party": {
"token": "c-t-pt",
"id": "token value",
"Specific_Party": "display value"
},
"General_Party": {
"token": "c-t-p",
"id": "token value",
"General_Party": "display value"
},
"Election_Jurisdiction": {
"token": "s",
"id": "token value",
"Election_Jurisdiction": "display value"
},
"Election_Year": {
"token": "y",
"id": "token value",
"Election_Year": "display value"
},
"Election_Type": {
"token": "c-r-t",
"id": "token value",
"Election_Type": "display value"
},
"Office_Sought": {
"token": "c-r-osid",
"id": "token value",
"Office_Sought": "display value"
},
"Election_Status": {
"token": "c-t-ftsts",
"id": "token value",
"Election_Status": "display value"
},
"Incumbency_Status": {
"token": "c-t-ico",
"id": "token value",
"Incumbency_Status": "display value"
},
"#_of_Records": {
"#_of_Records": "display value"
},
"Total_$": {
"Total_$": "display value"
}
}
},
"records": [
{
"record_id": 1,
"request": "c-r-id=16739&c-t-id=76674",
"Candidate": {
"token": "c-t-id",
"id": "76674",
"Candidate": "BUTLER, MACK N"
},
"Candidate_Entity": {
"token": "c-t-eid",
"id": "15881718",
"Candidate_Entity": "BUTLER, MACK N"
},
"Specific_Party": {
"token": "c-t-pt",
"id": "139",
"Specific_Party": "REPUBLICAN"
},
"General_Party": {
"token": "c-t-p",
"id": "2",
"General_Party": "Republican"
},
"Election_Jurisdiction": {
"token": "s",
"id": "AL",
"Election_Jurisdiction": "AL"
},
"Election_Year": {
"token": "y",
"id": "2012",
"Election_Year": "2012"
},
"Election_Type": {
"token": "c-r-t",
"id": "3",
"Election_Type": "Special"
},
"Office_Sought": {
"token": "c-r-osid",
"id": "143",
"Office_Sought": "HOUSE DISTRICT 030"
},
"Election_Status": {
"token": "c-t-ftsts",
"id": "Won-General",
"Election_Status": "Won-General"
},
"Incumbency_Status": {
"token": "c-t-ico",
"id": "O",
"Incumbency_Status": "Open"
},
"#_of_Records": {
"#_of_Records": "197"
},
"Total_$": {
"Total_$": "94700.08"
}
},
{
"record_id": 2,
"request": "c-r-id=16739&c-t-id=76438",
"Candidate": {
"token": "c-t-id",
"id": "76438",
"Candidate": "MCGLAUGHN, BETH"
},
"Candidate_Entity": {
"token": "c-t-eid",
"id": "15881720",
"Candidate_Entity": "MCGLAUGHN, BETH"
},
"Specific_Party": {
"token": "c-t-pt",
"id": "33",
"Specific_Party": "DEMOCRATIC"
},
"General_Party": {
"token": "c-t-p",
"id": "1",
"General_Party": "Democratic"
},
"Election_Jurisdiction": {
"token": "s",
"id": "AL",
"Election_Jurisdiction": "AL"
},
"Election_Year": {
"token": "y",
"id": "2012",
"Election_Year": "2012"
},
"Election_Type": {
"token": "c-r-t",
"id": "3",
"Election_Type": "Special"
},
"Office_Sought": {
"token": "c-r-osid",
"id": "143",
"Office_Sought": "HOUSE DISTRICT 030"
},
"Election_Status": {
"token": "c-t-ftsts",
"id": "Lost-General",
"Election_Status": "Lost-General"
},
"Incumbency_Status": {
"token": "c-t-ico",
"id": "O",
"Incumbency_Status": "Open"
},
"#_of_Records": {
"#_of_Records": "39"
},
"Total_$": {
"Total_$": "22537.65"
}
},
{
"record_id": 3,
"request": "c-r-id=16739&c-t-id=28927",
"Candidate": {
"token": "c-t-id",
"id": "28927",
"Candidate": "MCHUGH, ROB"
},
"Candidate_Entity": {
"token": "c-t-eid",
"id": "15881719",
"Candidate_Entity": "MCHUGH, ROB"
},
"Specific_Party": {
"token": "c-t-pt",
"id": "139",
"Specific_Party": "REPUBLICAN"
},
"General_Party": {
"token": "c-t-p",
"id": "2",
"General_Party": "Republican"
},
"Election_Jurisdiction": {
"token": "s",
"id": "AL",
"Election_Jurisdiction": "AL"
},
"Election_Year": {
"token": "y",
"id": "2012",
"Election_Year": "2012"
},
"Election_Type": {
"token": "c-r-t",
"id": "3",
"Election_Type": "Special"
},
"Office_Sought": {
"token": "c-r-osid",
"id": "143",
"Office_Sought": "HOUSE DISTRICT 030"
},
"Election_Status": {
"token": "c-t-ftsts",
"id": "Lost-Primary",
"Election_Status": "Lost-Primary"
},
"Incumbency_Status": {
"token": "c-t-ico",
"id": "O",
"Incumbency_Status": "Open"
},
"#_of_Records": {
"#_of_Records": "0"
},
"Total_$": {
"Total_$": "0.00"
}
}
]
}

Related

Not iterating through JSON result rows

I'm trying to simply print out the data for each row of a JSON result
The path structure is correct but its only doing the data for row 1
Here is the code
$row = $obj->response->result->Accounts->row;
foreach($row as $r)
{
print($r->FL[0]->content).',';
print($r->FL[1]->content).',';
print($r->FL[2]->content).',';
print($r->FL[3]->content).',';
print($r->FL[4]->content).',';
print($r->FL[5]->content).',';
print($r->FL[6]->content).',';
}
The JSON result
** Because people havent been reading the question properly here is the ENTIRE JSON result
{
"response": {
"result": {
"Accounts": {
"row": [
{
"no": "1",
"FL": [
{
"val": "ACCOUNTID",
"content": "3508588000000259001"
},
{
"val": "SMOWNERID",
"content": "3508588000000176021"
},
{
"val": "Account Owner",
"content": "Chris Yates"
},
{
"val": "Rating",
"content": "null"
},
{
"val": "Account Name",
"content": "Test Co"
},
{
"val": "Phone",
"content": "null"
},
{
"val": "Account Site",
"content": "null"
},
{
"val": "Fax",
"content": "null"
},
{
"val": "PARENTACCOUNTID",
"content": "null"
},
{
"val": "Parent Account",
"content": "null"
},
{
"val": "Website",
"content": "null"
},
{
"val": "Account Number",
"content": "0"
},
{
"val": "Ticker Symbol",
"content": "null"
},
{
"val": "Account Type",
"content": "null"
},
{
"val": "Ownership",
"content": "null"
},
{
"val": "Industry",
"content": "null"
},
{
"val": "Employees",
"content": "0"
},
{
"val": "Annual Revenue",
"content": "129000"
},
{
"val": "SIC Code",
"content": "0"
},
{
"val": "SMCREATORID",
"content": "3508588000000176021"
},
{
"val": "Created By",
"content": "Chris Yates"
},
{
"val": "MODIFIEDBY",
"content": "3508588000000176021"
},
{
"val": "Modified By",
"content": "Chris Yates"
},
{
"val": "Created Time",
"content": "2018-09-20 09:56:30"
},
{
"val": "Modified Time",
"content": "2018-09-20 12:39:56"
},
{
"val": "Billing Street",
"content": "null"
},
{
"val": "Shipping Street",
"content": "null"
},
{
"val": "Billing City",
"content": "null"
},
{
"val": "Shipping City",
"content": "null"
},
{
"val": "Billing State",
"content": "null"
},
{
"val": "Shipping State",
"content": "null"
},
{
"val": "Billing Code",
"content": "null"
},
{
"val": "Shipping Code",
"content": "null"
},
{
"val": "Billing Country",
"content": "null"
},
{
"val": "Shipping Country",
"content": "null"
},
{
"val": "Description",
"content": "null"
},
{
"val": "Last Activity Time",
"content": "2018-09-20 12:39:56"
},
{
"val": "Tag",
"content": "null"
}
]
},
There is the entire JSON result - there are over 400 rows
You should loop again in FL;
foreach($row as $r)
{
foreach($r->FL as $data)
print($data->content).',';
}
To stop outputting commas upon reaching a certain number;
foreach($row as $r)
{
$data_count = count($r->FL);
$counter = 0;
foreach($r->FL as $data)
{
if($data_count < $counter)
{
print($data->content).',';
}
else
{
print($data->content);
}
$counter++;
}
}
Display in new line (html);
foreach($row as $r)
{
foreach($r->FL as $data)
print($data->content).'<br>';
}
Display in new line (excel);
What I've noticed was you have to literally output your echo in a new line.
An excel file table has this data
|1|2|3|4|5|
|1|2|3|4|5|
and its csv file equivalent is
1,2,3,4,5
1,2,3,4,5

JSON sending fine as key:answer, not key:array

I'm having an issue where my android application is getting a positive result from our key:answer POST, but not from key:array for the same structure. My web developer is saying that I'm not serialising it properly, but every JSON tester I can find on the web is quite happy with the send, just not our web server. I'm sending my JSON in groups of 10 as a string (We had a previous issue where when sending about 200 keys it was giving issues, so I've split it down to 10 per send) and the JSON sent is as follows:
[{
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "date_inspDate",
"answervalues": "18-07-2017"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_clientRep",
"answervalues": "testrep"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_designCode",
"answervalues": "destdesign"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_qcPlan",
"answervalues": "testqc"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_equipDesc",
"answervalues": "testdesc"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_companyName",
"answervalues": "testco"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_drawingNo",
"answervalues": "testrev"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "Detail",
"answervalues": [{
"register": 1,
"values": "act2"
}, {
"register": 2,
"values": "act3"
}]
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "Regulated",
"answervalues": [{
"register": 1,
"values": "YES"
}, {
"register": 2,
"values": "NO"
}]
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "Comments",
"answervalues": [{
"register": 1,
"values": "DIMENSIONS CHECK"
}, {
"register": 2,
"values": "HOLD POINT"
}]
}]
All is fine until it hits the Detail key, which is an array. This structure was sending fine last week, they claim they've changed nothing. Is it really a serialisation issue on my side or is the server just not accepting that key as either value or array?
From your error message:
"JSON parse error: Can not deserialize instance of java.lang.String out of START_ARRAY token;"
I think you problem is in 'answervalues'.
/* in some entries you have it as an array of objects*/
"answervalues": [{
"register": 1,
"values": "YES"
}, {
"register": 2,
"values": "NO"
}]
/* while in other entries as a string */
"answervalues": "testrev"
Talk with your developer to see how this need to be formatted. I believe the string values also need to be passed as an object in a single array. Something like..
"answervalues": [{
"register": 1,
"values":"testrev"
}]

How to create a combo chart (bar and line) with fusioncharts using data from MySQL database?

I found this code online to do basically everything I was looking for except they are not using data from a MySQL database where as I am. Here is their code:
{
"chart": {
"caption": "Inventory by Product Categories",
"bgcolor": "FFFFFF",
"plotgradientcolor": "",
"showalternatehgridcolor": "0",
"showplotborder": "0",
"divlinecolor": "CCCCCC",
"showvalues": "0",
"showcanvasborder": "0",
"pyaxisname": "Cost of Inventory",
"syaxisname": "Units in Inventory",
"numberprefix": "$",
"labeldisplay": "STAGGER",
"slantlabels": "1",
"canvasborderalpha": "0",
"legendshadow": "0",
"legendborderalpha": "0",
"showborder": "0"
},
"categories": [
{
"category": [
{
"label": "Seafood",
"labelPadding": 0
},
{
"label": "Beverages",
"labelPadding": 12
},
{
"label": "Condiments",
"labelPadding": 0
},
{
"label": "Dairy Products",
"labelPadding": 12
},
{
"label": "Confections",
"labelPadding": 0
},
{
"label": "Meat/Poultry",
"labelPadding": 12
},
{
"label": "Grains/Cereals",
"labelPadding": 0
},
{
"label": "Produce",
"labelPadding": 12
}
]
}
],
"dataset": [
{
"seriesname": "Cost of Inventory",
"color": "008ee4",
"data": [
{
"value": "13510"
},
{
"value": "12480"
},
{
"value": "12024"
},
{
"value": "11271"
},
{
"value": "10392"
},
{
"value": "5729"
},
{
"value": "5594"
},
{
"value": "3549"
}
]
},
{
"seriesname": "Quantity",
"parentyaxis": "S",
"renderas": "Line",
"color": "f8bd19",
"data": [
{
"value": "701"
},
{
"value": "559"
},
{
"value": "507"
},
{
"value": "393"
},
{
"value": "386"
},
{
"value": "165"
},
{
"value": "258"
},
{
"value": "100"
}
]
}
]
}
This makes sense, but it's changing this JSON code to accept data from a database instead of hardcoding this that is difficult for me.
Here is the relevent part of my code:
<?php
$strQuery2 = "SELECT ScrapDate, SUM(Quantity) AS Quantity FROM Scrap WHERE Department = 'WE' GROUP BY ScrapDate ORDER BY ScrapDate";
// Execute the query, or else return the error message.
$result2 = $dbhandle->query($strQuery2) or exit("Error code ({$dbhandle->errno}): {$dbhandle->error}");
// If the query returns a valid response, prepare the JSON string
if ($result2) {
// The `$arrData` array holds the chart attributes and data
$arrData2 = array(
"chart" => array(
"caption" => "WE Last Week Scrap Quantity",
"paletteColors" => "#0075c2",
"bgColor" => "#ffffff",
"borderAlpha"=> "20",
"canvasBorderAlpha"=> "0",
"usePlotGradientColor"=> "0",
"plotBorderAlpha"=> "10",
"showXAxisLine"=> "1",
"xAxisLineColor" => "#999999",
"showValues"=> "0",
"divlineColor" => "#999999",
"divLineIsDashed" => "1",
"showAlternateHGridColor" => "0",
"xAxisName"=> "Day",
"yAxisName"=> "Quantity"
)
);
$arrData2["data"] = array();
// Push the data into the array
while($row2 = mysqli_fetch_array($result2)) {
array_push($arrData2["data"], array(
"label" => $row2["ScrapDate"],
"value" => $row2["Quantity"],
// "link" => "deptDrillDown.php?Department=".$row["Department"]
)
);
}
$jsonEncodedData2 = json_encode($arrData2);
$columnChart2 = new FusionCharts("column2D", "chart2" , 600, 300, "chart-2", "json", $jsonEncodedData2);
// Render the chart
$columnChart->render();
$columnChart2->render();
// Close the database connection
$dbhandle->close();
}
?>
As of right now, I can get data from the MySQL database and put it into a graph very easily. Now I want to add 1 more dataset, how do I do this?
To dynamically update the chart, following methods turns very useful:
getJSONData: Fetches chart data in the JSON format.
setJSONData: Sets chart data in the JSON data format
A sample implementation is shown in the below snippet. The chart is initially rendered as the default column2d chart. Once the Add Area button is clicked, an area dataset gets visually updated. Similarly on clicking the Add Line button to add a line dataset. The Reset button restores to original column only visual.
var visitChart,
areaBtn = document.getElementById('area'),
lineBtn = document.getElementById('line'),
resetBtn = document.getElementById('reset');
FusionCharts.ready(function() {
visitChart = new FusionCharts({
type: 'mscombi2d',
renderAt: 'chart-container',
width: '500',
height: '300',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "Actual Revenues, Targeted Revenues & Profits",
"subcaption": "Last year",
"xaxisname": "Month",
"yaxisname": "Amount (In USD)",
"numberprefix": "$",
"theme": "fint"
},
"categories": [{
"category": [{
"label": "Jan"
}, {
"label": "Feb"
}, {
"label": "Mar"
}, {
"label": "Apr"
}, {
"label": "May"
}, {
"label": "Jun"
}, {
"label": "Jul"
}, {
"label": "Aug"
}, {
"label": "Sep"
}, {
"label": "Oct"
}, {
"label": "Nov"
}, {
"label": "Dec"
}]
}],
"dataset": [{
"seriesname": "Actual Revenue",
"data": [{
"value": "16000"
}, {
"value": "20000"
}, {
"value": "18000"
}, {
"value": "19000"
}, {
"value": "15000"
}, {
"value": "21000"
}, {
"value": "16000"
}, {
"value": "20000"
}, {
"value": "17000"
}, {
"value": "25000"
}, {
"value": "19000"
}, {
"value": "23000"
}]
}]
}
}).render();
});
areaBtn.addEventListener('click', function() {
var dataset,
json = visitChart.getJSONData();
if (!(dataset = json.dataset)) {
dataset = json.dataset = [];
}
dataset.push({
"seriesname": "Profit",
"renderas": "area",
"showvalues": "0",
"data": [{
"value": "4000"
}, {
"value": "5000"
}, {
"value": "3000"
}, {
"value": "4000"
}, {
"value": "1000"
}, {
"value": "7000"
}, {
"value": "1000"
}, {
"value": "4000"
}, {
"value": "1000"
}, {
"value": "8000"
}, {
"value": "2000"
}, {
"value": "7000"
}]
});
visitChart.setJSONData(json);
areaBtn.disabled = true;
});
lineBtn.addEventListener('click', function() {
var dataset,
json = visitChart.getJSONData();
if (!(dataset = json.dataset)) {
dataset = json.dataset = [];
}
dataset.push({
"seriesname": "Projected Revenue",
"renderas": "line",
"showvalues": "0",
"data": [{
"value": "15000"
}, {
"value": "16000"
}, {
"value": "17000"
}, {
"value": "18000"
}, {
"value": "19000"
}, {
"value": "19000"
}, {
"value": "19000"
}, {
"value": "19000"
}, {
"value": "20000"
}, {
"value": "21000"
}, {
"value": "22000"
}, {
"value": "23000"
}]
});
visitChart.setJSONData(json);
lineBtn.disabled = true;
});
resetBtn.addEventListener('click', function() {
var dataset,
len,
json = visitChart.getJSONData();
if (!(dataset = json.dataset)) {
dataset = json.dataset = [];
}
if ((len = dataset.length) > 1) {
dataset.splice(1, len - 1);
}
visitChart.setJSONData(json);
areaBtn.disabled = false;
lineBtn.disabled = false;
});
<script src="http://static.fusioncharts.com/code/latest/fusioncharts.js"></script>
<script src="http://static.fusioncharts.com/code/latest/themes/fusioncharts.theme.fint.js"></script>
<button id='area'>Add Area</button>
<button id='line'>Add Line</button>
<button id='reset'>Reset</button>
<span id="chart-container">FusionCharts XT will load here!</span>
The data getting appended might be fetched from a database in a similar manner stated above.

unable to get json_decode field

I want to get title for each song from the json below in php.I am unable to get all the names .I tried with the code below but its not displaying anything.I really appreciate any help.Thanks in Advance.
code:
$json = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=5/json');
$data=(json_decode($json, true));
foreach ($data as $item)
{
foreach ($item->entry as $asset)
{
echo $asset->title;
}
}
json:
{
"feed": {
"author": {
"name": {
"label": "iTunes Store"
},
"uri": {
"label": "http://www.apple.com/itunes/"
}
},
"entry": [
{
"im:name": {
"label": "Happy (From \"Despicable Me 2\")"
},
"im:image": [
{
"label": "http://a923.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a1645.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a1228.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "G I R L"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/g-i-r-l/id823593445?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2014 Columbia Records, a Division of Sony Music Entertainment, 2013 Back Lot Music, under exclusive license to Columbia Records, a Division of Sony Music Entertainment"
},
"title": {
"label": "Happy (From \"Despicable Me 2\") - Pharrell Williams"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/happy-from-despicable-me-2/id823593445?i=823593456&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a1083.phobos.apple.com/us/r1000/014/Music/v4/4e/44/b7/4e44b7dc-aaa2-c63b-fb38-88e1635b5b29/mzaf_1844128138535731917.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/happy-from-despicable-me-2/id823593445?i=823593456&uo=2",
"attributes": {
"im:id": "823593456"
}
},
"im:artist": {
"label": "Pharrell Williams",
"attributes": {
"href": "https://itunes.apple.com/us/artist/pharrell-williams/id14934728?uo=2"
}
},
"category": {
"attributes": {
"im:id": "14",
"term": "Pop",
"scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2",
"label": "Pop"
}
},
"im:releaseDate": {
"label": "2014-03-03T00:00:00-07:00",
"attributes": {
"label": "March 3, 2014"
}
}
},
{
"im:name": {
"label": "All of Me"
},
"im:image": [
{
"label": "http://a1221.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a1943.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a982.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "Love in the Future (Deluxe Edition)"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/love-in-future-deluxe-edition/id679297685?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2013 Getting Out Our Dreams and Columbia Records, a Division of Sony Music Entertainment"
},
"title": {
"label": "All of Me - John Legend"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/all-of-me/id679297685?i=679297849&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a355.phobos.apple.com/us/r1000/041/Music4/v4/90/cf/04/90cf0482-07c0-fca0-549f-c1ea62c4bdef/mzaf_6715619947923767616.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/all-of-me/id679297685?i=679297849&uo=2",
"attributes": {
"im:id": "679297849"
}
},
"im:artist": {
"label": "John Legend",
"attributes": {
"href": "https://itunes.apple.com/us/artist/john-legend/id16586443?uo=2"
}
},
"category": {
"attributes": {
"im:id": "15",
"term": "R&B/Soul",
"scheme": "https://itunes.apple.com/us/genre/music-r-b-soul/id15?uo=2",
"label": "R&B/Soul"
}
},
"im:releaseDate": {
"label": "2013-08-30T00:00:00-07:00",
"attributes": {
"label": "August 30, 2013"
}
}
},
{
"im:name": {
"label": "Let It Go"
},
"im:image": [
{
"label": "http://a261.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a983.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a262.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "Frozen (Deluxe Edition) [Original Motion Picture Soundtrack]"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/frozen-deluxe-edition-original/id728903889?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2013 Walt Disney Records"
},
"title": {
"label": "Let It Go - Idina Menzel"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/let-it-go/id728903889?i=728904000&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a878.phobos.apple.com/us/r1000/023/Music/v4/0a/eb/c2/0aebc299-44e3-3465-3cdf-5bab336b547a/mzaf_7970981567593232494.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/let-it-go/id728903889?i=728904000&uo=2",
"attributes": {
"im:id": "728904000"
}
},
"im:artist": {
"label": "Idina Menzel",
"attributes": {
"href": "https://itunes.apple.com/us/artist/idina-menzel/id3297504?uo=2"
}
},
"category": {
"attributes": {
"im:id": "16",
"term": "Soundtrack",
"scheme": "https://itunes.apple.com/us/genre/music-soundtrack/id16?uo=2",
"label": "Soundtrack"
}
},
"im:releaseDate": {
"label": "2013-11-25T00:00:00-07:00",
"attributes": {
"label": "November 25, 2013"
}
}
},
{
"im:name": {
"label": "Dark Horse (feat. Juicy J)"
},
"im:image": [
{
"label": "http://a1657.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a731.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a1498.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "PRISM (Deluxe Version)"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/prism-deluxe-version/id690928033?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2013 Capitol Records, LLC"
},
"title": {
"label": "Dark Horse (feat. Juicy J) - Katy Perry"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/dark-horse-feat.-juicy-j/id690928033?i=690928460&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a1930.phobos.apple.com/us/r1000/022/Music/v4/a4/ac/63/a4ac6341-59e3-bcfe-b7f5-370100eea063/mzaf_4835051646631223146.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/dark-horse-feat.-juicy-j/id690928033?i=690928460&uo=2",
"attributes": {
"im:id": "690928460"
}
},
"im:artist": {
"label": "Katy Perry",
"attributes": {
"href": "https://itunes.apple.com/us/artist/katy-perry/id64387566?uo=2"
}
},
"category": {
"attributes": {
"im:id": "14",
"term": "Pop",
"scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2",
"label": "Pop"
}
},
"im:releaseDate": {
"label": "2013-10-22T00:00:00-07:00",
"attributes": {
"label": "October 22, 2013"
}
}
},
{
"im:name": {
"label": "The Man"
},
"im:image": [
{
"label": "http://a1169.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a243.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a1234.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "Lift Your Spirit"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/lift-your-spirit/id798928288?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2014 Aloe Blacc Recording, Inc. under exclusive license to XIX Recordings LLC/Interscope Records"
},
"title": {
"label": "The Man - Aloe Blacc"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/the-man/id798928288?i=798928362&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a85.phobos.apple.com/us/r1000/033/Music6/v4/8a/09/dc/8a09dcf0-4845-81ae-6936-a94972ad94e7/mzaf_5332424013433716082.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/the-man/id798928288?i=798928362&uo=2",
"attributes": {
"im:id": "798928362"
}
},
"im:artist": {
"label": "Aloe Blacc",
"attributes": {
"href": "https://itunes.apple.com/us/artist/aloe-blacc/id4750752?uo=2"
}
},
"category": {
"attributes": {
"im:id": "14",
"term": "Pop",
"scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2",
"label": "Pop"
}
},
"im:releaseDate": {
"label": "2014-03-11T00:00:00-07:00",
"attributes": {
"label": "March 11, 2014"
}
}
}
],
"updated": {
"label": "2014-03-26T14:09:41-07:00"
},
"rights": {
"label": "Copyright 2008 Apple Inc."
},
"title": {
"label": "iTunes Store: Top Songs"
},
"icon": {
"label": "http://itunes.apple.com/favicon.ico"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?cc=us&id=38&popId=1"
}
},
{
"attributes": {
"rel": "self",
"href": "http://itunes.apple.com/us/rss/topsongs/limit=5/json"
}
}
],
"id": {
"label": "http://itunes.apple.com/us/rss/topsongs/limit=5/json"
}
}
}
$json = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=5/json');
$data=json_decode($json);
$titles = array();
foreach ($data as $item)
{
foreach ($item->entry as $asset)
{
$titles[] = $asset->title->label;
}
}

Parse a dynamic JSON object into PHP

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.

Categories