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 6875651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:22:47+00:00 2026-05-27T04:22:47+00:00

Javascript/jQuery newbie here. My webserver is sending the contents of a directory tree as

  • 0

Javascript/jQuery newbie here.

My webserver is sending the contents of a directory tree as a JSON object. The object is arbitrarily nested depending on the number of subdirectories containing other subdirectories. It looks like this:

{
    "contents": [
        {
            "filename": "myfile",
            "mtime": 123456,
            "size": 2345,
            "content": nil
        },
        {
            "filename": "mydir",
            "mtime": 2345678,
            "size": 3456788,
            "content": [
                {...},
                {...}
            ]
        }
    ]
}

myfile is a normal file and so “content” is empty. mydir is a directory which may be empty, or contain other files or subdirectories.

I want to parse this JSON with javascript and generate an html ul representation of the contents. My question is: Is there an easy/recommended way to do this?

  • 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-05-27T04:22:47+00:00Added an answer on May 27, 2026 at 4:22 am

    If you’re receiving the JSON text via an ajax call using jQuery, jQuery will deserialize it into an object graph for you. If you’ve received it in some other way and have it in a string, you can deserialize it with jQuery.parseJSON.

    However, the JSON you’ve quoted is invalid, specifically this bit:

    "content": nil
    

    There is no nil keyword in JSON. You’ll want to fix the server to either leave the key off entirely, or use null, or something else. Valid JSON is defined on the JSON website, and you can use http://jsonlint.com for handy validation (and formatting). It might also be useful to use content or contents consistently; currently the top level uses contents but the subordinate entries use content.

    Once you have the object graph, it’s a fairly simple matter of a recursive function, looping through the array entries and, for entries that can have nested content, calling itself again to loop through that. Something vaguely like this (live copy):

    jQuery(function($) {
    
      display("Loading the JSON data");
      $.ajax({
        type:     "GET",
        url:      "/path/to/the/data",
        dataType: "JSON",
        success:  function(data) {
          display("Got the data, rendering it.");
          $(document.body).append(renderContents(data.contents));
        },
        error:    function() {
          display("An error occurred.");
        }
      });
    
      function renderContents(contents) {
        var index, ul;
    
        // Create a list for these contents
        ul = $("<ul>");
    
        // Fill it in
        $.each(contents, function(index, entry) {
          var li;
    
          // Create list item
          li = $("<li>");
    
          // Set the text
          li.text(entry.filename);
    
          // Append a sublist of its contents if it has them
          if (entry.content) {
            li.append(renderContents(entry.content));
          }
    
          // Add this item to our list
          ul.append(li);
        });
    
        // Return it
        return ul;
      }
    
      function display(msg) {
        $("<p>").html(msg).appendTo(document.body);
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

JavaScript and jQuery newbie here, and I wanted to ask a question; Basically, I
I am a newbie at Javascript and Jquery. I have a form that I
I am a newbie at Javascript, JQuery, and to a lesser extent, Rails. I
I'm progamming in javascript/jQuery. Say, I have an array with includes a few number
Being a newbie to Javascript and jQuery, I am not sure whether this is
I am web-designer, not a programmer and a newbie in Javascript / jQuery (although
I'm totally newbie in javascript/jQuery and i have a little problem :) I'm using
Hi everyone (first post!). I'm a JQuery and Javascript newbie. I'm working on the
I'm a newbie with jquery and i've used this code for tabs <script type=text/javascript
I am newbie to jQuery and javascript. In my application I have a list

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.