0

This code

    <?php
$connect = mysqli_connect( "localhost", "user", "password", "db" );

$sql = "SELECT * FROM current_rates WHERE date = '2017-05-01'";

$result = mysqli_query( $connect, $sql );

$Json_array = array();


while ( $row = mysqli_fetch_assoc( $result ) ) {

    $Json_array[] = $row;

}

/*   echo "<pre>";
     print_r($Json_array);
      echo "<pre>";
*/
echo json_encode( $Json_array );
?>

returns this JSON info

[{"key":"47","financial_institution":"ATB Financial","variable_rate":"2.1","six_months":"0","one_year":"2.99","two_year":"2.39","three_year":"2.54","four_year":"2.57","five_year":"2.74","date":"2017-05-01"},{"key":"48","financial_institution":"Alterna Bank","variable_rate":"2.25","six_months":"0","one_year":"2.79","two_year":"2.74","three_year":"2.64","four_year":"2.55","five_year":"2.6","date":"2017-05-01"},{"key":"49","financial_institution":"Alterna Savings","variable_rate":"2.28","six_months":"0","one_year":"2.79","two_year":"2.74","three_year":"2.64","four_year":"2.74","five_year":"2.64","date":"2017-05-01"},{"key":"50","financial_institution":"Bank of Montreal","variable_rate":"2.85","six_months":"0","one_year":"3.09","two_year":"3.19","three_year":"3.59","four_year":"4.09","five_year":"4.84","date":"2017-05-01"},{"key":"51","financial_institution":"Bank of Nova Scotia","variable_rate":"2.7","six_months":"0","one_year":"3.29","two_year":"3.09","three_year":"3.39","four_year":"3.89","five_year":"4.64","date":"2017-05-01"},{"key":"52","financial_institution":"CIBC Mortgages","variable_rate":"2.7","six_months":"0","one_year":"2.84","two_year":"2.79","three_year":"3.39","four_year":"4.09","five_year":"4.79","date":"2017-05-01"},{"key":"53","financial_institution":"Canadian Western Bank","variable_rate":"2.5","six_months":"0","one_year":"2.44","two_year":"2.29","three_year":"2.44","four_year":"2.54","five_year":"2.64","date":"2017-05-01"},{"key":"54","financial_institution":"Canadian Western Trust","variable_rate":"2.9","six_months":"0","one_year":"3.14","two_year":"3.04","three_year":"3.65","four_year":"4.2","five_year":"4.74","date":"2017-05-01"},{"key":"55","financial_institution":"Comtech Credit Union","variable_rate":"0","six_months":"0","one_year":"2.84","two_year":"2.89","three_year":"2.95","four_year":"2.99","five_year":"2.89","date":"2017-05-01"},{"key":"56","financial_institution":"DUCA Financial Services","variable_rate":"2.5","six_months":"0","one_year":"3.04","two_year":"2.94","three_year":"2.94","four_year":"3.14","five_year":"2.79","date":"2017-05-01"},{"key":"57","financial_institution":"Desjardins - Caisses","variable_rate":"2.7","six_months":"0","one_year":"3.14","two_year":"3.24","three_year":"3.39","four_year":"3.89","five_year":"4.74","date":"2017-05-01"},{"key":"58","financial_institution":"Effort Trust","variable_rate":"0","six_months":"0","one_year":"3.35","two_year":"3.5","three_year":"3.7","four_year":"4.3","five_year":"4.35","date":"2017-05-01"},{"key":"59","financial_institution":"Equitable Bank","variable_rate":"0","six_months":"0","one_year":"3.14","two_year":"3.04","three_year":"3.65","four_year":"4.39","five_year":"4.64","date":"2017-05-01"},{"key":"60","financial_institution":"First Calgary Financial","variable_rate":"0","six_months":"0","one_year":"2.89","two_year":"2.89","three_year":"2.64","four_year":"2.79","five_year":"2.74","date":"2017-05-01"},{"key":"61","financial_institution":"First National Financial","variable_rate":"2.35","six_months":"0","one_year":"2.64","two_year":"2.64","three_year":"2.64","four_year":"2.84","five_year":"2.84","date":"2017-05-01"},{"key":"62","financial_institution":"FirstOntario Credit Un.","variable_rate":"2.7","six_months":"0","one_year":"2.79","two_year":"2.49","three_year":"2.64","four_year":"2.79","five_year":"2.89","date":"2017-05-01"},{"key":"63","financial_institution":"HSBC Bank Canada","variable_rate":"2.25","six_months":"0","one_year":"2.25","two_year":"2.29","three_year":"2.39","four_year":"2.49","five_year":"2.49","date":"2017-05-01"},{"key":"64","financial_institution":"Home Trust Company-Agent","variable_rate":"2.35","six_months":"0","one_year":"2.69","two_year":"2.14","three_year":"2.59","four_year":"2.74","five_year":"2.84","date":"2017-05-01"},{"key":"65","financial_institution":"ICICI Bank Canada","variable_rate":"3.05","six_months":"0","one_year":"2.64","two_year":"2.64","three_year":"2.64","four_year":"3.19","five_year":"3.29","date":"2017-05-01"},{"key":"66","financial_institution":"Investors Group Trust","variable_rate":"2.85","six_months":"0","one_year":"3.14","two_year":"3.09","three_year":"3.39","four_year":"3.89","five_year":"4.64","date":"2017-05-01"},{"key":"67","financial_institution":"Kindred Credit Union","variable_rate":"2.7","six_months":"0","one_year":"3.35","two_year":"3.35","three_year":"3.4","four_year":"3.4","five_year":"2.79","date":"2017-05-01"},{"key":"68","financial_institution":"Laurentian Bank Canada","variable_rate":"2.7","six_months":"0","one_year":"2.89","two_year":"3.04","three_year":"3.39","four_year":"3.89","five_year":"4.74","date":"2017-05-01"},{"key":"69","financial_institution":"League Svgs & Mortgage","variable_rate":"0","six_months":"0","one_year":"2.99","two_year":"2.59","three_year":"2.64","four_year":"2.74","five_year":"2.79","date":"2017-05-01"},{"key":"70","financial_institution":"Libro Credit Union","variable_rate":"0","six_months":"0","one_year":"2.7","two_year":"2.75","three_year":"2.8","four_year":"2.85","five_year":"2.89","date":"2017-05-01"},{"key":"71","financial_institution":"London Life","variable_rate":"2.85","six_months":"0","one_year":"3.14","two_year":"3.09","three_year":"3.39","four_year":"3.89","five_year":"4.64","date":"2017-05-01"},{"key":"72","financial_institution":"Manulife Bank","variable_rate":"2.7","six_months":"0","one_year":"3","two_year":"2.94","three_year":"2.84","four_year":"2.89","five_year":"2.89","date":"2017-05-01"},{"key":"73","financial_institution":"Manulife Trust","variable_rate":"0","six_months":"0","one_year":"3","two_year":"2.94","three_year":"2.84","four_year":"2.89","five_year":"2.89","date":"2017-05-01"},{"key":"74","financial_institution":"Meridian Credit Union","variable_rate":"2.3","six_months":"0","one_year":"2.84","two_year":"2.79","three_year":"3.39","four_year":"3.89","five_year":"2.69","date":"2017-05-01"},{"key":"75","financial_institution":"National Bank","variable_rate":"2.7","six_months":"0","one_year":"3.14","two_year":"3.04","three_year":"3.39","four_year":"3.89","five_year":"4.74","date":"2017-05-01"},{"key":"76","financial_institution":"PACE Savings Credit Un","variable_rate":"0","six_months":"0","one_year":"3.14","two_year":"3.04","three_year":"3.65","four_year":"4.39","five_year":"4.64","date":"2017-05-01"},{"key":"77","financial_institution":"Parama Credit Union","variable_rate":"2.65","six_months":"0","one_year":"2.8","two_year":"2.85","three_year":"2.95","four_year":"3.05","five_year":"3.15","date":"2017-05-01"},{"key":"78","financial_institution":"Peace Hills Trust","variable_rate":"0","six_months":"0","one_year":"3.15","two_year":"3.5","three_year":"3.75","four_year":"3.85","five_year":"3.95","date":"2017-05-01"},{"key":"79","financial_institution":"Rapport Credit Union","variable_rate":"2.85","six_months":"0","one_year":"3.09","two_year":"3.04","three_year":"3.5","four_year":"3.04","five_year":"2.99","date":"2017-05-01"},{"key":"80","financial_institution":"Royal Bank of Canada","variable_rate":"2.7","six_months":"0","one_year":"3.14","two_year":"3.04","three_year":"3.65","four_year":"4.39","five_year":"4.64","date":"2017-05-01"},{"key":"81","financial_institution":"Steinbach Credit Union","variable_rate":"0","six_months":"0","one_year":"2.39","two_year":"2.39","three_year":"2.49","four_year":"2.59","five_year":"2.69","date":"2017-05-01"},{"key":"82","financial_institution":"Street Capital Bank","variable_rate":"2.15","six_months":"0","one_year":"2.69","two_year":"2.69","three_year":"2.69","four_year":"2.69","five_year":"2.69","date":"2017-05-01"},{"key":"83","financial_institution":"T-D Mortgage","variable_rate":"2.85","six_months":"0","one_year":"3.04","two_year":"2.84","three_year":"3.44","four_year":"3.89","five_year":"4.64","date":"2017-05-01"},{"key":"84","financial_institution":"Tangerine","variable_rate":"2.3","six_months":"0","one_year":"3.19","two_year":"3.24","three_year":"3.49","four_year":"3.69","five_year":"2.69","date":"2017-05-01"},{"key":"85","financial_institution":"WFCU Credit Union","variable_rate":"0","six_months":"0","one_year":"3","two_year":"3.04","three_year":"3.65","four_year":"4.39","five_year":"4.64","date":"2017-05-01"},{"key":"86","financial_institution":"Your Neighbourhood C.U.","variable_rate":"0","six_months":"0","one_year":"2.79","two_year":"2.89","three_year":"3.09","four_year":"3.15","five_year":"2.79","date":"2017-05-01"}]

I have been trying to work this data that I think I the correct JSON format... but I am thinking I am wrong as this is no initial object stated.

I do not know how to edit the code so that I names the object (maybe object is the wrong term - I am very new and my learning has been from this great site and youTube. I think I need the long JSON out put needs to have a "lable" like lenders below added... I have idea how to do this.

   {
"lenders": [
    {
    "date": "2017-05-01",
    "financial_institution": "Your Neighbourhood C.U.",
    "five_year": "2.79",
    "four_year": "3.15",
    "key": "86",
    "one_year": "2.79",
    "six_months": "0",
    "three_year": "3.09",
    "two_year": "2.89",
    "variable_rate": "0"
    },

1 Answer 1

1

Your json is already valid even though there is no label in the front. But if you want to structure your json with the label, here is how you do it: (Look at the last 2 lines)

<?php
$connect = mysqli_connect( "localhost", "user", "password", "db" );

$sql = "SELECT * FROM current_rates WHERE date = '2017-05-01'";

$result = mysqli_query( $connect, $sql );

$Json_array = array();

while ( $row = mysqli_fetch_assoc( $result ) ) {
    $Json_array[] = $row;
}
/*   echo "<pre>";
     print_r($Json_array);
      echo "<pre>";
*/

$finalJson['lenders'] = $Json_array;
echo json_encode( $finalJson );
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. Having the label makes it easier for me as many of the tutorials I have found reference labels. Cheers
You can also omit the intermediate variable: json_encode(array('lenders' => $Json_array))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.