0

I want to fetch all the data from external json file and print in the console, but it is not fetching data to console. Can anyone help me to solve this.

{
"data": 
    {
        "slug": "allsopp-allsopp",
        "id": 401,
        "imageToken": "d045e18526f988cceb63b08e71180fb6595d9f27",
        "name": "Allsopp & Allsopp",
        "location": "Dubai",
        "description": "Allsopp & Allsopp is a family founded property services company operating a traditional UK estate agency model in the United Arab Emirates (UAE). Our mandate is to deliver levels of customer care well above prevailing industry benchmarks, in a rapid and result oriented fashion which adheres strictly to the regulatory framework now governing the local property market. Consequently the staff we employ are expected to follow a business methodology which demands exceptional honesty, a stringent code of business conduct and total transparency to the client. The key objective at Allsopp & Allsopp is to be an all embracing property service centre that caters to all types of property related transactions in the UAE.",
        "residentialForRentCount": 521,
        "residentialForSaleCount": 1114,
        "commercialForRentCount": 1,
        "commercialForSaleCount": 0,
        "commercialTotalCount": 1,
        "totalProperties": 1636,
        "agentCount": 57,
        "licenseLabel": "RERA",
        "licenseNumber": "1815",
        "phone": "+971 4 429 4444",
        "links": {
            "self": "/en/broker/allsopp-allsopp-401",
            "logo": "https://www.propertyfinder.ae/images/pf_broker/logo/d045e18526f988cceb63b08e71180fb6595d9f27/desktop",
            "logo2x": "https://www.propertyfinder.ae/images/pf_broker/logo/d045e18526f988cceb63b08e71180fb6595d9f27/desktop2x"
        }
   }

jquery code

 $(document).ready(function () {
        $.getJSON('data.json', function(data) {
            console.log(data);
        });
    });
wali
  • 615
  • 10
  • 24

2 Answers2

1

You can try with below.

  1. Check your json file is placed with application root folder or not. If any other path, Change the JSON URL path in script.

  2. Add JSON.Stringify() in your function.

console.log(JSON.stringify(data));

-1

I know this is more complicated than the accepted answer, but its for people who want to read files using PHP and Javascript (& jQuery).

Javascript Code

$.post('readjson.php', {json_file: 'json.txt'}, function(json){
if (String(json).length > 0){
    console.log(json);
}else{
    console.log("Error Reading File");
}
});

PHP CODE [readjson.php]

if (isset($_POST['json_file'])){
    $data = file_get_contents($_POST['json_file']);
    echo $data;
}
Nabil Ali
  • 147
  • 1
  • 13