Parsing JSON data and inserting to MySQL - php

So basically I want to parse a JSON file in PHP and insert the data into specific tables/columns. At the moment I have a working script but requires me to modify the JSON largely until it works. However, it won't end up working because the JSON data I'm collecting can vary in size having more data rows.
The JSON file is structured differently to most I have seen. Maybe because its output data from sensor units. I want to insert the data and the serial number into the data table, and have an error_log table where I can store the serial number and error messages as strings. How can I achieve this?
JSON File:
{
"device": {
"sn": 5165654,
"name": "FDI_AWS_DEMO",
"v": "2.7B3"
},
"channels": [
{
"code": "RH",
"name": "Relative Humidity",
"unit": "%"
},
{
"code": "AT",
"name": "Air Temperature",
"unit": "C"
},
{
"code": "MINVi",
"name": "Min voltage",
"unit": "V"
},
{
"code": "PTi",
"name": "Processor temperature",
"unit": "C"
},
{
"code": "SDB",
"name": "Network signal dB",
"unit": "dB"
},
{
"code": "LWS",
"name": "Leaf Wetness",
"unit": "%"
},
{
"code": "WSAV",
"name": "Wind Speed Avg",
"unit": "km/h"
},
{
"code": "WSMX",
"name": "Wind Speed Max",
"unit": "km/h"
},
{
"code": "WSMN",
"name": "Wind Speed Min",
"unit": "km/h"
},
{
"code": "PR_TOT",
"name": "PR Tot",
"unit": "mm"
},
{
"code": "RAIN",
"name": "Rain",
"unit": "mm"
},
{
"code": "FDI",
"name": "fdi",
"unit": "Unit"
},
{
"code": "DT",
"name": "Delta-T",
"unit": "C"
},
{
"code": "LAT",
"name": "Latitude",
"unit": "deg"
},
{
"code": "LON",
"name": "Longitude",
"unit": "deg"
},
{
"code": "WD",
"name": "Wind Direction",
"unit": "Degrees"
},
{
"code": "P1",
"name": "Par1",
"unit": ""
},
{
"code": "AVGCi",
"name": "Average Current",
"unit": "mA"
},
{}
],
"data": [
{
"$ts": 170801164400,
"$msg": "SD_FAIL;1"
},
{
"$ts": 170801170000,
"$msg": "WDT;WV01"
},
{
"$ts": 170801170000,
"$msg": "WDT;SDI12"
},
{
"$ts": 170801170000,
"$msg": "WDT;LWS"
},
{
"$ts": 170801170000,
"RH": 67.15,
"AT": 12.87,
"MINVi": 3.81,
"PTi": 23.4,
"LWS": "0*T",
"WSAV": 0,
"WSMX": 0,
"WSMN": 0,
"PR_TOT": 156,
"RAIN": 0,
"FDI": 0.239,
"DT": 2.881,
"WD": "0*T",
"P1": "0*T",
"AVGCi": 175
},
{}
]
}
PHP Code:
<?php
//connect to mysql db
$myConnection= mysqli_connect("localhost","root","******", "ii") or die ("could not connect to mysql");
//read the json file contents
$jsondata = file_get_contents('test.json');
//convert json object to php associative array
$data = json_decode($jsondata, true);
$id = $data['device']['sn'];
$ts = $data['data']['$ts'];
$RH = $data['data']['RH'];
$AT = $data['data']['AT'];
$MINVi = $data['data']['MINVi'];
$PTi = $data['data']['PTi'];
$SDB = $data['data']['SDB'];
$LWS = $data['data']['LWS'];
$WSAV = $data['data']['WSAV'];
$WSMX = $data['data']['WSMX'];
$WSMN = $data['data']['WSMN'];
$PR_TOT = $data['data']['PR_TOT'];
$RAIN = $data['data']['RAIN'];
$FDI = $data['data']['FDI'];
$DT = $data['data']['DT'];
$LAT = $data['data']['LAT'];
$LON = $data['data']['LON'];
$WD = $data['data']['WD'];
$P1 = $data['data']['P1'];
$AVGCi = $data['data']['AVGCi'];
//insert into mysql table
$sql = "INSERT INTO test(sn, date, RH, AT, MINVi, PTi, SDB, LWS, WSAV, WSMX, WSMN, PR_TOT, RAIN, FDI, DT, LAT, LON, WD, P1, AVGCi)
VALUES('$id', '$ts', '$RH','$AT', '$MINVi', '$PTi', '$SDB', '$LWS', '$WSAV', '$WSMX', '$WSMN', '$PR_TOT', '$RAIN', '$FDI', '$DT', '$LAT', '$LON', '$WD', '$P1', '$AVGCi')";
$query=mysqli_query($myConnection, $sql) or die(mysqli_error($myConnection));
?>
Tables Test data table and error_log table
JSON array var_dump - JSON var dump
Any help would be great
(After i get the general gist i want to incorporate PDO)

Do not convert json to associative array blindly. It creates more problems.
For accessing properties containing special characters or reserved
words use placeholders like $data->{'$ts'}
Loop through arrays and objects if needed.
Adding an auto increment id column to tables helps to store data
for one device.
It is a good idea to add time to error_log table as well
Tested bellow short version of your original question and it works.
<?php
$_user = 'root';
$_password= 'root';
$_db = 'localtest';
$_host = 'localhost';
$_port = 3306;
$con = new mysqli($_host, $_user, $_password, $_db) or die(mysql_error);
//read the json file contents
$jsondata = file_get_contents('test.json');
//do not convert to array
$json = json_decode($jsondata);
$id = $json->device->sn;
foreach($json->data as $key => $data){
if(empty($data) || !isset($data->{'$ts'})){
continue;
}
if (isset($data->{'$msg'})){
$msg = $data->{'$msg'};
$time = $data->{'$ts'};
$sql="INSERT into error_log (sn, time, MSG) VALUES (?,?,?); ";
$stmt = $con-> prepare($sql);
$stmt -> bind_param("iss", $id,$time, $msg);
$stmt -> execute();
}else{
$time = (isset($data->{'$ts'}))? $data->{'$ts'}:'';
$RH = (isset($data->RH))? $data->RH:'';
$AT = (isset($data->AT))? $data->AT:'';
$MINVi = (isset($data->MINVi))? $data->MINVi:'';
//insert into mysql table
$sql="INSERT into test (sn, date, RH, AT, MINVi) VALUES (?,?,?,?,?); ";
$stmt = $con-> prepare($sql);
$stmt -> bind_param("issss", $id,$time,$RH,$AT,$MINVi);
$stmt -> execute();
}
}
mysqli_close($con);
?>

looks like the json decode is turning the array into one that is object based...run the $data through the function below to turn it into more of a relational array that is setup the way you are trying to read it....
I cannot find the function i had for this at the moment but If you read the array more like this:
$newvar = $data->node;
//to echo this try this...
echo '<pre style="text-align:left;">';
print_r($data);
echo '</pre>';
OR try fb logs for php to see the arrays in console of ya browser - use firephp for the addon.
it should work just fine...
Hope that helps.

Consider building dynamic SQL calls conditionally depending on the error output. The array keys create the columns in INSERT clause and array values are quote wrapped in VALUES clause. Below echoes the sql statements just to demonstrate.
This should work for any change to JSON suffice no other new keys are added. Also, the array_splice is used to remove the $ts value the second time since you pass it in date column.
$jsondata = file_get_contents('Input.json');
$data = json_decode($jsondata, true);
$id = $data['device']['sn'];
foreach ($data['data'] as $k=>$v){
if (array_key_exists("\$msg",$v) & !empty($v)){
$sql = "INSERT INTO error_log (sn, msg)"
." VALUES('$id', '". $v["\$msg"] ."')";
echo $sql."\n";
// INSERT INTO error_log (sn, msg) VALUES('5165654', 'SD_FAIL;1')
// INSERT INTO error_log (sn, msg) VALUES('5165654', 'WDT;WV01')
// INSERT INTO error_log (sn, msg) VALUES('5165654', 'WDT;SDI12')
// INSERT INTO error_log (sn, msg) VALUES('5165654', 'WDT;LWS')
$query = mysqli_query(...);
}
if (!array_key_exists("\$msg",$v) & !empty($v)) {
$keysArray = array_keys($v);
$keysArray = array_splice($keysArray, 1);
$vVals = array_splice($v, 1);
$sql = "INSERT INTO test(sn, date, ". implode(", ", $keysArray) .")\n"
." VALUES('$id', '". $v['$ts'] ."',". implode("', '", $vVals) .")";
echo $sql."\n";
// INSERT INTO test(sn, date, RH, AT, MINVi, PTi, LWS, WSAV, WSMX, WSMN,
// PR_TOT, RAIN, FDI, DT, WD, P1, AVGCi)
// VALUES('5165654', '170801170000',67.15', '12.87', '3.81', '23.4', '0*T',
// '0', '0', '0', '156', '0', '0.239', '2.881', '0*T', '0*T', '175)
$query=mysqli_query(...)
}
}

Maybe something like this can get you on track... One thing I did notice is there doesn't appear to be an SDB entry in the given JSON data (FYI).
<?php
//connect to mysql db
$myConnection= mysqli_connect("localhost","root","******", "ii") or die ("could not connect to mysql");
//read the json file contents
$jsondata = file_get_contents('test.json');
//convert json object to php associative array
$data = json_decode($jsondata, true);
// Make sure $data has values
if (empty($data)) {
// Process error here
}
else {
// Make sure the proper keys have been set
if (!(isset($data['device']) && !empty($data['device']) && isset($data['data']) && !empty($data['data']))) {
// Process error here if not set
}
// If so, make sure the sn key has been set
elseif (!isset($data['device']['sn']) || empty($data['device']['sn'])) {
// Process error here if not set
}
else {
$data_arr = $data['data'];
foreach ($data_arr as $key => $arr) {
// Iterate through for loop
for ($i = 0; $i < count($data_arr); $i++) {
// Make sure every single key is set
if (!(isset($data_arr[$i]['$ts']) && isset($data_arr[$i]['RH']) && isset($data_arr[$i]['AT']) && isset($data_arr[$i]['MINVi']) && isset($data_arr[$i]['PTi']) && isset($data_arr[$i]['SDB']) && isset($data_arr[$i]['LWS']) && isset($data_arr[$i]['WSAV']) && isset($data_arr[$i]['WSMX']) && isset($data_arr[$i]['WSMN']) && isset($data_arr[$i]['PR_TOT']) && isset($data_arr[$i]['RAIN']) && isset($data_arr[$i]['FDI']) && isset($data_arr[$i]['DT']) && isset($data_arr[$i]['LAT']) && isset($data_arr[$i]['LON']) && isset($data_arr[$i]['WD']) && isset($data_arr[$i]['P1']) && isset($data_arr[$i]['AVGCi']) && isset($data_arr[$i]['DT']) && isset($data_arr[$i]['DT']))) {
// Process error here if not set
}
else {
// If all is well, perform the query
$id[$key][$i] = $arr['device']['sn'];
$ts[$key][$i] = $data_arr[$i]['$ts'];
$RH[$key][$i] = $data_arr[$i]['RH'];
$AT[$key][$i] = $data_arr[$i]['AT'];
$MINVi[$key][$i] = $data_arr[$i]['MINVi'];
$PTi[$key][$i] = $data_arr[$i]['PTi'];
$SDB[$key][$i] = $data_arr[$i]['SDB'];
$LWS[$key][$i] = $data_arr[$i]['LWS'];
$WSAV[$key][$i] = $data_arr[$i]['WSAV'];
$WSMX[$key][$i] = $data_arr[$i]['WSMX'];
$WSMN[$key][$i] = $data_arr[$i]['WSMN'];
$PR_TOT[$key][$i] = $data_arr[$i]['PR_TOT'];
$RAIN[$key][$i] = $data_arr[$i]['RAIN'];
$FDI[$key][$i] = $data_arr[$i]['FDI'];
$DT[$key][$i] = $data_arr[$i]['DT'];
$LAT[$key][$i] = $data_arr[$i]['LAT'];
$LON[$key][$i] = $data_arr[$i]['LON'];
$WD[$key][$i] = $data_arr[$i]['WD'];
$P1[$key][$i] = $data_arr[$i]['P1'];
$AVGCi[$key][$i] = $data_arr[$i]['AVGCi'];
$sql[$key][$i] = "INSERT INTO test(sn, date, RH, AT, MINVi, PTi, SDB, LWS, WSAV, WSMX, WSMN, PR_TOT, RAIN, FDI, DT, LAT, LON, WD, P1, AVGCi)
VALUES('{$id[$key][$i]}', '{$ts[$key][$i]}', '{$RH[$key][$i]}','{$AT[$key][$i]}', '{$MINVi[$key][$i]}', '{$PTi[$key][$i]}', '{$SDB[$key][$i]}', '{$LWS[$key][$i]}', '{$WSAV[$key][$i]}', '{$WSMX[$key][$i]}', '{$WSMN[$key][$i]}', '{$PR_TOT[$key][$i]}', '{$RAIN[$key][$i]}', '{$FDI[$key][$i]}', '{$DT[$key][$i]}', '{$LAT[$key][$i]}', '{$LON[$key][$i]}', '{$WD[$key][$i]}', '{$P1[$key][$i]}', '{$AVGCi[$key][$i]}')";
//insert into mysql table
$query[$key][$i] = mysqli_query($myConnection, $sql[$key][$i]) or die(mysqli_error($myConnection));
}
}
}
}
}
?>

Related

PHP update json from received input and write back to same file

I have a json file stored on server & it looks like below:
{
"support_link":"#",
"support_link_2":"#",
"packs":[
{
"identifier":1,
"viewCount":0,
"downloadCount":0
},
{
"identifier":2,
"viewCount":0,
"downloadCount":0
}
]
}
By using PHP, I want to update the viewCount & downloadCount of some of the arrays inside packs.
But the thing is the data is received via a POST method to the server which contains another json with info. of which identifier to update & what param to update, & I am not able to update the existing file & save it back.
Received Json format:
{
"impressions": [
{
"identifier": "1",
"impressionCount": 2
},
{
"identifier": "100",
"impressionCount": 2
},
{
"identifier": "1000",
"impressionCount": 2000
}
],
"downloads": [
{
"identifier": "1",
"downloadCount": 10
}
]
}
What I've tried to do so far:
$json = file_get_contents('php://input');
if ($json != '') {
$properJsonFormatted = json_decode($json, true);
$impressions = $properJsonFormatted['impressions'];
$downloads = $properJsonFormatted['downloads'];
$testConfig =
$json = file_get_contents('php://input');
if ($json != '') {
$properJsonFormatted = json_decode($json, true);
$impressions = $properJsonFormatted['impressions'];
$downloads = $properJsonFormatted['downloads'];
$testConfig = json_decode(file_get_contents("test_config.json"),true);
$packs = $testConfig['packs'];
foreach ($packs as &$pack) {
$packIdentifier = $pack['identifier'];
foreach ($impressions as $impression) {
$impressionIdentifier = $impression['identifier'];
if ($packIdentifier == $impressionIdentifier) {
$pack['viewCount'] += $impression['impressionCount'];
$newCount = $pack['viewCount'];
print("Id: $packIdentifier, ViewCount: $newCount\n");
}
}
}
put_file_contents("test_config.json" , $testConfig);
// print_r($testConfig);
// Save back the updated test_config.json
}
}
UPDATE
Seem to have misinterpreted the question. The actual problem seems to be much simpler.
Change this:
put_file_contents("test_config.json" , $testConfig);
To this:
file_put_contents('test_config.json', json_encode($testConfig));
Also change this:
$packs = $testConfig['packs'];
To this:
$packs = &$testConfig['packs'];
As it seems you forgot to assign that by reference, while you correctly did that in the foreach.

How to parse json array from mysql?

I have this data but I can't parse it in the way I want. this is what I get:
{
"navigations": {
"title": "Facebook",
"link": "https://facebook.com",
"behavior": "EXTERNAL"
}
}
And what I expect to see:
{
"navigations": [
{
"title": "Facebook",
"url": "https://facebook.com",
"behavior": "INAPP"
},
{
"title": "Youtube",
"url": "https//youtube.com",
"behavior": "INAPP"
}
]
}
I have more results in my database but not more than 12 result so, i want to fetch data in the format i expect. I tried to do it using fetch_array but no success.
This is my PHP code:
$query_update_navigations = $db->prepare("SELECT title, link, behavior FROM navigation_tabs WHERE secret_api_key=?");
$query_update_navigations->bind_param("s", $_SESSION['secret_api_key']);
$query_update_navigations->execute();
$rows = array();
$result = $query_update_navigations->get_result();
while($rows1 = $result->fetch_assoc()) {
$rows['navigations'] = $rows1;
}
$store_path = '../apis/navigation_tabs/';
$file_name = $_SESSION['secret_api_key'] . '.json';
$sign_constants = json_encode($rows);
file_put_contents($store_path . $file_name, $sign_constants);
I searched for an answer but nothing solved my issue :(
You need to push the row onto the array, not overwrite it each time.
$rows = array('navigations' => []);
$result = $query_update_navigations->get_result();
while($rows1 = $result->fetch_assoc()) {
$rows['navigations'][] = $rows1;
}

Notice: Undefined offset & Trying to get property of non-object

I am trying to pull data from the json_decode array, I have done it before but this time I am getting extra issues.
This is what the json looks like.
{
"achievementpercentages": {
"achievements": [
{
"name": "TF_SCOUT_LONG_DISTANCE_RUNNER",
"percent": 54.668815612792969
},
{
"name": "TF_HEAVY_DAMAGE_TAKEN",
"percent": 47.104038238525391
},
{
"name": "TF_GET_CONSECUTIVEKILLS_NODEATHS",
"percent": 44.668777465820312
},
{
"name": "TF_PYRO_CAMP_POSITION",
"percent": 36.480117797851563
},
{
"name": "TF_KILL_NEMESIS",
"percent": 34.392494201660156
},
{
"name": "TF_BURN_PLAYERSINMINIMUMTIME",
"percent": 33.580135345458984
},
{
"name": "TF_PYRO_BURN_MEDICPAIR",
"percent": 32.748367309570312
},
And my code is basically this
$achName = $decodedAch->achievementpercentages->achievements[$achCounter]->name;
$achPercent = $decodedAch->achievementpercentages->achievements[$achCounter]->percent;
The $achCounter is present in a while statement, in an effort to get all the achievements seperately. I don't think that part is wrong, just how I am trying to access the data in the array. I don't see what is wrong with it though.
Any help would be appreciated.
FULL WHILE STATEMENT AS REQUESTED
while($achCounter < $responseCount)
{
$appid = $decodedSteam->response->games[$achCounter]->appid;
$achievementURL = 'http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid='.$appid.'&format=json';
$jsonAch = file_get_contents($achievementURL);
$decodedAch = json_decode($jsonAch);
//var_dump($decodedAch);
$achName = $decodedAch->achievementpercentages->achievements[$achCounter]->name;
$achPercent = $decodedAch->achievementpercentages->achievements[$achCounter]->percent;
//echo $achievementURL;
$sql = "INSERT INTO steamid".$steamid."(name, percent) VALUES ('".$achName."','".$achPercent."')";
$achCounter++;
}
You need to iterate again after you fetch data from your url. Your $achCounter is usefull only for $decodedSteam, not for $decodedAch:
while($achCounter < $responseCount)
{
$appid = $decodedSteam->response->games[$achCounter]->appid;
$achievementURL = 'http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid='.$appid.'&format=json';
$jsonAch = file_get_contents($achievementURL);
$decodedAch = json_decode($jsonAch);
//var_dump($decodedAch);
foreach($decodedAch->achievementpercentages->achievements as $achievement) { //HERE
$achName = $achievement->name;
$achPercent = $achievement->percent;
//echo $achievementURL;
$sql = "INSERT INTO steamid".$steamid."(name, percent) VALUES ('".$achName."','".$achPercent."')";
}
$achCounter++;
}

JSON to MySql using PHP

Hi and I need your help
I'm working on a project for class, I'm looking to write from JSON to a MySql database.
The JSON looks like this
{
"offset": 0,
"results": [
{
"aq1": 22,
"aq3": 27,
"aq2": 27,
"ateam/_source": "/nba/teams/page/NO/new-orleans-pelicans",
"aq4": 24,
"hqf/_source": "108",
"hq2": 27,
"hq1": 22,
"hq4": 36,
"hq3": 23,
"aqf/_source": "100",
"ateam/_text": "New Orleans",
"aq3/_source": "27",
"hq4/_source": "36",
"hteam": "http://www.cbssports.com/nba/teams/page/BOS/boston-celtics",
"aq2/_source": "27",
"hteam/_text": "Boston",
"aq1/_source": "22",
"hteam/_source": "/nba/teams/page/BOS/boston-celtics",
"ateam": "http://www.cbssports.com/nba/teams/page/NO/new-orleans-pelicans",
"aqf": 100,
"hq1/_source": "22",
"hq3/_source": "23",
"hq2/_source": "27",
"hqf": 108,
"aq4/_source": "24"
},
],
"cookies": [],
"connectorVersionGuid": "dgsadjgofsjkgkfsmghjfhf",
"connectorGuid": "fbcjsdbgfjdjgkf",
"pageUrl": "http://xsftbfbjjjsfaf.com/"
}
and my php reads like this
<?php
$server = "localhost";
$username = "lies";
$password = "madeup";
$database = "dgjsfdgjv";
mysql_connect($server,$username,$password) or die("Failed");
mysql_select_db($database) or die("Database Failed");
$url = "https://api.import.io/store/data/jjjjj-ec0f-4553-bda5-def61ca1756c/_query?input/webpage/url=http%3A%2F%2Fwww.jjjjj.com%2Fnba%2Fscoreboard%2F20150111&_user=f1508a78-05c5-4487-9219-51a990391329&_apikey=aLZu2wmRCvUlBwopi%2F6Kj%2F4wnscvZRh7DYQf80LE2e3hL22rtAp0nJ3lujy10cyx9JC9Ed73xb3zGp3aArhYDQ%3D%3D";
$string = file_get_contents($url);
$arr = json_decode($string, true);
foreach($arr as $item){
$aq1 = $item['aq1'];
$aq3 = $item['aq3'];
$aq2 = $item['aq2'];
$ateam/_source = $item['aqeam/_source'];
$aq4 =$item['aq4'];
$hqf/_source =$item['aqf/_source'];
$hq2 =$item['aq2'];
$hq1 =$item['aq1'];
$hq4 =$item['aq4'];
$hq3 =$item['aq3'];
$aqf/_source =$item['aqf/_source'];
$ateam/_text =$item['aqeam/_text'];
$aq3/_source =$item['aq3/_source'];
$hq4/_source =$item['aq4/_source'];
$hteam =$item['aqeam'];
$aq2/_source =$item['aq2/_source'];
$hteam/_text =$item['aqeam/_text'];
$aq1/_source =$item['aq1/_source'];
$hteam/_source =$item['aqeam/_source'];
$ateam =$item['aqeam'];
$aqf =$item['aqf'];
$hq1/_source =$item['aq1/_source'];
$hq3/_source =$item['aq3/_source'];
$hq2/_source =$item['aq2/_source'];
$hqf =$item['aqf'];
$aq4/_source =$item['aq4/_source'];
mysql_query("INSERT INTO createdb (aq1,aq3,aq2,ateam/_source,aq4,hqf/_source,hq2,hq1,hq4,hq3,aqf/_source,ateam/_text,aq3/_source,hq4/_source,hteam,aq2/_source, hteam/_text,aq1/_source,hteam/_source,ateam,aqf,hq1/_source,hq3/_source,hq2/_source,hqf,aq4/_source) VALUES('$aq1','$aq3','$aq2','$ateam/_source','$aq4','$hqf/_source','$hq2','$hq1','$hq4','$hq3','$aqf/_source','$ateam/_text', '$aq3/_source','$hq4/_source','$hteam','$aq2/_source','$hteam/_text','$aq1/_source','$hteam/_source','$ateam','$aqf','$hq1/_source','$hq3/_source','$hq2/_source','$hqf','$aq4/_source')") or die ("Failed");
}
I dont trigger any of the error messages to appear but no data ends up in the database. What am I doing wrong? Is it the php code reading the json string correctly???
By carefully, looking at your problem , I have realized that , the problem was inside foreach() Loop. It should be
foreach($arr as $item){
echo $aq1 = $item[0]['aq1']; # note that [0] here
echo $aq3 = $item[0]['aq3'];
#...........Rest of Loop
}
And also rename variables like this $aqf/_source to $aqf_source , and so on .
I got it working.
foreach ($phpArray['results'] as $key => $value) {
// This is to make the data to be saved into the database
foreach ($value as $k => $v) {
$aq1 = $value['aq1'];
$aq3 = $value['aq3'];
$aq2 = $value['aq2'];
$ateam_source = $value['ateam/_source'];
$aq4 =$value['aq4'];
$hqf_source =$value['hqf/_source'];
$hq2 =$value['aq2'];
$hq1 =$value['aq1'];
$hq4 =$value['aq4'];
$hq3 =$value['aq3'];
$aqf_source =$value['aqf/_source'];
$ateam_text =$value['ateam/_text'];
$aq3_source =$value['aq3/_source'];
$hq4_source =$value['hq4/_source'];
$hteam =$value['hteam'];
$aq2_source =$value['aq2/_source'];
$hteam_text =$value['hteam/_text'];
$aq1_source =$value['aq1/_source'];
$hteam_source =$value['hteam/_source'];
$ateam =$value['ateam'];
$aqf =$value['aqf'];
$hq1_source =$value['hq1/_source'];
$hq3_source =$value['hq3/_source'];
$hq2_source =$value['hq2/_source'];
$hqf =$value['hqf'];
$aq4_source =$value['hq4/_source'];
}

Why I can't return a proper json structure?

I'm facing an issue regarding the json structure.
I have this:
function loadActiveTrackers($host_l, $username_l, $password_l, $dbName_l, $companyId_l){
$trackerId = 0;
$trackerName = "";
$trackerCreator = "";
$projectName = "";
$dataArray = [];
$trackerIdsArray = [];
$trackerNamesArray = [];
$trackerCreatorsArray = [];
$projectNamesArray = [];
$mysqli = new mysqli($host_l, $username_l, $password_l, $dbName_l);
$getActiveTrackersQuery = "SELECT tracker_id, tracker_name, tracker_creator, project_name FROM trackers WHERE "
. "company_id = ? AND is_active=1 ORDER BY tracker_creation_date";
if($stmt = $mysqli->prepare($getActiveTrackersQuery)){
$stmt->bind_param('s',$companyId_l);
$stmt->execute();
/* Store the result (to get properties) */
$stmt->store_result();
/* Bind the result to variables */
$stmt->bind_result($trackerId, $trackerName, $trackerCreator, $projectName);
while ($stmt->fetch()) {
$trackerIdsArray[] = $trackerId;
$trackerNamesArray[] = $trackerName;
$trackerCreatorsArray[] = $trackerCreator;
$projectNamesArray[] = $projectName;
}
$dataArray['ids'] = $trackerIdsArray;
$dataArray['names'] = $trackerNamesArray;
$dataArray['creators'] = $trackerCreatorsArray;
$dataArray['projectNames'] = $projectNamesArray;
// print_r($trackerIdsArray);
/* free results */
$stmt->free_result();
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
echo json_encode($dataArray);
}
The above code works ok, but the structure is not valid.
What I mean is that it returns:
{
"ids": [1,2,3,4],
"names": ["1","2","test tracker","test1"],
"creators": ["1","test","test","test"],
"projectNames": ["1","1","test project","test"]
}
But it is supposed to return:
[
{"id": 1, "name": "1", "creator": "1", "projectName": "1"},
{"id": 2, "name": "2", "creator": "test", "projectName": "1"},
{"id": 3, "name": "test tracker", "creator": "test", "projectName": "test project"},
{"id": 4, "name": "test1", "creator": "test", "projectName": "test"}
]
Can you guide me trough it? I know that it's something really small, but I'm not able to spot it as a php newbie. In java the json library is pretty strong and with a single push() it can be achieved, but here I really can't find the way.
The current code creates arrays of the ids, names, creators, and project names.
So, instead of this code:
while ($stmt->fetch()) {
$trackerIdsArray[] = $trackerId;
$trackerNamesArray[] = $trackerName;
$trackerCreatorsArray[] = $trackerCreator;
$projectNamesArray[] = $projectName;
}
$dataArray['ids'] = $trackerIdsArray;
$dataArray['names'] = $trackerNamesArray;
$dataArray['creators'] = $trackerCreatorsArray;
$dataArray['projectNames'] = $projectNamesArray;
Change your structure to be like so:
while ($stmt->fetch()) {
$dataArray[] = array( 'id' => $trackerId,
'name' => $trackerName,
'creator' => $trackerCreator,
'projectName' => $projectName
);
}
echo json_encode($dataArray);
This will return the structure you desire, which is arrays of the ids, names, creators, and project names.

Categories