Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7721951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:03:43+00:00 2026-06-01T04:03:43+00:00

I am trying to loop through this external JSON file(locally stored), but I cannot

  • 0

I am trying to loop through this external JSON file(locally stored), but I cannot get it to read the values and keys properly. What am I doing wrong?

This is driving me crazy and I have tried a million combinations of markup and have tried other methods, but I keep ending up in the same place.

Really appreciate any help.

Also the code does not output

JSON FILE cars.js (local file)

window.cars = { 

    "compact": [

        { "title": "honda", 
          "type": "accord", 
          "thumbnail": "accord_t.jpg", 
          "image": "accord_large.jpg" }, 

        { "title": "volkswagon", 
          "type": "rabbit",
          "thumbnail": "rabbit_t.jpg",    
          "image": "volkswagon_large.jpg" } 

    ],

    "trucks": [

        { "title": "Ford", 
          "type": "f-150", 
          "thumbnail": "ford_t.jpg", 
          "image": "chevy_large.jpg" },

        { "title": "GMC", 
          "type": "silverado", 
          "thumbnail": "gmc_t.jpg", 
          "image": "gmc_large.jpg" }

    ]

};

HTML

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
<script type="text/javascript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script> 
$(document).ready(function(){ 

$.ajax ({      

url: 'cars.js',      
type: 'get', 
dataType: 'json',  
error: function(result){ alert('No Dice')},
success: function(result){

    $('body').append('<div class="main_wrapper"></div><ul>');

        $.each(cars.trucks, function( k, v){

        $('ul').append('<li class="trucks"><img src="' + v.k[1].thumbnail + '" />
                <h1>Title:' +  v.k[1].title + ' </h1>
                <h2>Type: ' + v.k[1].type + '</h2></li>');
        });

        $.each(cars.compact, function( k, v){

       $('ul').append('<li class="compact"><img src="' + v.k[0].thumbnail + '" />
                <h1>Title:' +  v.k[0].title + ' </h1>
                 <h2>Type: ' + v.k[0].type + '</h2></li>');
       });

}

});

});
</script>
</head>
<body>
</body>
</html>

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:03:44+00:00Added an answer on June 1, 2026 at 4:03 am

    First, make sure your web server is configured properly, so it actually returns the content of static cars.js file. Second, remove “windows.cars = ” and semicolon from the file. You are loading data, not a script, so it’s got to be pure json. Then, in success function result will be your json object, so should be able to access result.trucks and result.compact properties (not cars.trucks / cars.compact)

    UPDATE:
    After further reading on dataType setting at http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings, I think you should be able to get you code working if you change dataType from “json” to “script”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to loop through this to get the 'name' values. This is what
I'm trying to loop through this XML to get specifically to the count fields.
I am trying to loop through a list and get an output like this:
I was trying to loop through all possible values of a float like this:
I'm trying to loop through the results from the Last.fm API but it's not
So I have an XML file that I am trying to loop through in
i have an array i'm trying to loop through by array.length but it stops
I am trying to loop through all the checkboxes and get their Value and
Anyone know how to do this? I'm trying to loop through an array where
Im trying to loop through a json files' object array to access its variables'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.