-2

I have a problem converting JSON to HTML Table. There are lots similar to this question but there is no correct answer for my problem.

This is the JSON API result from getting it through the URL:

{
"status":"success",
"postcode":"W14 9JH",
"postcode_type":"full",
"url":"https://propertydata.co.uk/draw?input=W14+9JH",
"bedrooms":2,
"data":{
    "points_analysed":20,
    "radius":"0.09",
    "average":657495,
    "70pc_range":[
        575000,
        725000
    ],
    "80pc_range":[
        550000,
        875000
    ],
    "90pc_range":[
        550000,
        925000
    ],
    "100pc_range":[
        525000,
        950000
    ],
    "raw_data":[
        {
            "price":650000,
            "lat":"51.48887000",
            "lng":"-0.20776000",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.00"
        },
        {
            "price":575000,
            "lat":"51.48884800",
            "lng":"-0.20701200",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.03"
        },
        {
            "price":615000,
            "lat":"51.48851000",
            "lng":"-0.20742000",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.03"
        },
        {
            "price":640000,
            "lat":"51.48932000",
            "lng":"-0.20804000",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.03"
        },
        {
            "price":725000,
            "lat":"51.48843100",
            "lng":"-0.20775400",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.03"
        },
        {
            "price":699950,
            "lat":"51.48928000",
            "lng":"-0.20793700",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.03"
        },
        {
            "price":550000,
            "lat":"51.48941000",
            "lng":"-0.20832000",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.04"
        },
        {
            "price":600000,
            "lat":"51.48813700",
            "lng":"-0.20781600",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.05"
        },
        {
            "price":925000,
            "lat":"51.48948300",
            "lng":"-0.20827400",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.05"
        },
        {
            "price":650000,
            "lat":"51.48824900",
            "lng":"-0.20669000",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.06"
        },
        {
            "price":700000,
            "lat":"51.48941200",
            "lng":"-0.20666900",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.06"
        },
        {
            "price":625000,
            "lat":"51.48817000",
            "lng":"-0.20689500",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.06"
        },
        {
            "price":645000,
            "lat":"51.48822500",
            "lng":"-0.20872100",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.06"
        },
        {
            "price":675000,
            "lat":"51.48877800",
            "lng":"-0.20643200",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.06"
        },
        {
            "price":525000,
            "lat":"51.48978800",
            "lng":"-0.20864000",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.07"
        },
        {
            "price":700000,
            "lat":"51.48785100",
            "lng":"-0.20757600",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.07"
        },
        {
            "price":675000,
            "lat":"51.48819900",
            "lng":"-0.20637900",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.08"
        },
        {
            "price":875000,
            "lat":"51.48818700",
            "lng":"-0.20931800",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.08"
        },
        {
            "price":550000,
            "lat":"51.48891000",
            "lng":"-0.20955000",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.08"
        },
        {
            "price":950000,
            "lat":"51.48808300",
            "lng":"-0.20628500",
            "bedrooms":2,
            "type":"flat",
            "distance":"0.09"
        }
    ]
},
"process_time":"2.08"
}

I need this result to be converted into HTML Table using PHP if it's possible.

There might be anyone who has the same issue as mine so I hope the one who could answer this can help the others. Anyone who can help is really appreciated.

  • There's no issue here, it's just parsing and processing. What have you attempted? Can you provide any code? – aymcg31 Aug 17 '22 at 08:50
  • @aymcg31 I am new to PHP so I don't know how to start it or where to start but I attempted to call the URL using `file_get_contents` and decode it using `json_decode`. Then the result is now showing and I want it to be converted to HTML Table or to put the result in HTML Table – janus sumbilon Aug 17 '22 at 08:56

1 Answers1

0

It's not a simple array that can be displayed as an HTML table with a title row and content rows but a complex object. Take a look at this : Pretty-Printing JSON with PHP

Example :

<?php
$json_data ='{"status":"success","data": ["..."]}';

$data = json_encode(json_decode($json_data, true), JSON_PRETTY_PRINT);
?>

<!DOCTYPE html>
<html>
<body>
    <p>Data :</p>
    <pre><code><?php echo $data ?></code></pre>
</body>
</html>

-- EDIT --

If you want to make an array of raw_data :

<?php

// phpinfo();

$json_data ='{"status":"success","postcode":"W14 9JH","postcode_type":"full","url":"https://propertydata.co.uk/draw?input=W14+9JH","bedrooms":2,"data":{"points_analysed":20,"radius":"0.09","average":657495,"70pc_range":[ 575000, 725000 ],"80pc_range":[ 550000, 875000 ],"90pc_range":[ 550000, 925000 ],"100pc_range":[ 525000, 950000 ],"raw_data":[ {"price":650000,"lat":"51.48887000","lng":"-0.20776000","bedrooms":2,"type":"flat","distance":"0.00" }, {"price":575000,"lat":"51.48884800","lng":"-0.20701200","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":615000,"lat":"51.48851000","lng":"-0.20742000","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":640000,"lat":"51.48932000","lng":"-0.20804000","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":725000,"lat":"51.48843100","lng":"-0.20775400","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":699950,"lat":"51.48928000","lng":"-0.20793700","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":550000,"lat":"51.48941000","lng":"-0.20832000","bedrooms":2,"type":"flat","distance":"0.04" }, {"price":600000,"lat":"51.48813700","lng":"-0.20781600","bedrooms":2,"type":"flat","distance":"0.05" }, {"price":925000,"lat":"51.48948300","lng":"-0.20827400","bedrooms":2,"type":"flat","distance":"0.05" }, {"price":650000,"lat":"51.48824900","lng":"-0.20669000","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":700000,"lat":"51.48941200","lng":"-0.20666900","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":625000,"lat":"51.48817000","lng":"-0.20689500","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":645000,"lat":"51.48822500","lng":"-0.20872100","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":675000,"lat":"51.48877800","lng":"-0.20643200","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":525000,"lat":"51.48978800","lng":"-0.20864000","bedrooms":2,"type":"flat","distance":"0.07" }, {"price":700000,"lat":"51.48785100","lng":"-0.20757600","bedrooms":2,"type":"flat","distance":"0.07" }, {"price":675000,"lat":"51.48819900","lng":"-0.20637900","bedrooms":2,"type":"flat","distance":"0.08" }, {"price":875000,"lat":"51.48818700","lng":"-0.20931800","bedrooms":2,"type":"flat","distance":"0.08" }, {"price":550000,"lat":"51.48891000","lng":"-0.20955000","bedrooms":2,"type":"flat","distance":"0.08" }, {"price":950000,"lat":"51.48808300","lng":"-0.20628500","bedrooms":2,"type":"flat","distance":"0.09" } ] },"process_time":"2.08"}';

$data = json_decode($json_data, true);

$raw_data = $data['data']['raw_data'];
?>

<!DOCTYPE html>
<html>
<body>
    <table>
        <thead>
            <tr>
                <td>price</td>
                <td>lat</td>
                <td>lng</td>
                <td>bedrooms</td>
                <td>type</td>
                <td>distance</td>
            </tr>
        </thead>
        <tbody>
            <?php
                foreach($raw_data as $raw){
                    echo "<tr>
                        <td>".$raw['price']."</td>
                        <td>".$raw['lat']."</td>
                        <td>".$raw['lng']."</td>
                        <td>".$raw['bedrooms']."</td>
                        <td>".$raw['type']."</td>
                        <td>".$raw['distance']."</td>
                    </tr>";
                }
            ?>
        </tbody>
    </table>
</body>

enter image description here

Geo Daz
  • 214
  • 1
  • 11
  • As I expected it's a complex array. That is why I can't get the answers I am looking for. Yes, what I want to get is the raw_data. This is a great help @Geo Daz. Really appreciated. – janus sumbilon Aug 17 '22 at 10:08