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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:00:25+00:00 2026-05-27T19:00:25+00:00

I am trying to create a treeview like structure from JSON data . Structure

  • 0

I am trying to create a treeview like structure from JSON data. Structure is quite simple, but the problem I am getting is while grouping the children under particular parents.

For example,

Input JSON data is :{‘record’:[{‘sn’:’Demo Sheet 1′,’vn’:’Demo View 1′},{‘sn’:’Demo Sheet 11′,’vn’:’Demo View 12′},{‘sn’:’Demo Sheet 2′,’vn’:’Demo View 21′},{‘sn’:’Demo Sheet 1′,’vn’:’Demo View 13′}],’recordcount’:’4′}

Now, I want to display this data in below format :

  • Demo Sheet 1
    • Demo View 11
    • Demo View 12
    • Demo View 13
  • Demo Sheet 2
    • Demo View 21

In HTML, I have created a div like <div id="treeview"></div>.

Now using javascript I have to populate this div’s innerHTML with the treeview list.

UPDATE : Count of Number of child Items should be displayed in the brackets of Parent Item, e.g. Demo Sheet 1 (3)

Any help to achieve this will be highly appreciated.

Thanks,

manishekhawat

  • 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-27T19:00:26+00:00Added an answer on May 27, 2026 at 7:00 pm

    It looks like you want to go from this structure:

    {
        'record': [
            {'sn':'Demo Sheet 1','vn':'Demo View 11'},
            {'sn':'Demo Sheet 1','vn':'Demo View 12'},
            {'sn':'Demo Sheet 2','vn':'Demo View 21'},
            {'sn':'Demo Sheet 1','vn':'Demo View 13'}
        ],
        'recordcount':'4'
    }
    

    to this one:

    {
        'Demo Sheet 1': [
            'Demo View 11',
            'Demo View 12',
            'Demo View 13'
        ],
        'Demo Sheet 2': [
            'Demo View 21'
        ]
    }
    

    I think the first place to start is to de-serialize your JSON string into the first object, then iterate over the ‘record’ array pulling out each element and creating a new array for each unique key, or adding to an existing array if that key already exists (e.g. ‘Demo Sheet 1’). Once you’ve done that and discarded any extra data, you should end up with a data structure similar to the second one above, which should be very easy to generate mark-up for.

    EDIT
    As an example of the above (using Douglas Crockford’s JSON2 library, something like this:

    var jsonObject = JSON.parse(jsonString /* your original json data */);
    var newArrays = {};
    var records = jsonObject.record;
    
    for (var i = 0; i < records.length; i++) {
        if (!newArrays[records[i].sn]) {
            newArrays[records[i].sn] = [];
        }
        newArrays[records[i].sn].push(records[i].vn);
    }
    

    You now have the new data structure – how you mark it up is entirely up to you! See this fiddle for a working example.

    Word of warning: I’d recommend adding some error handling to the above scenario, as you should never assume that the JSON data you’re receiving will parse correctly.

    EDIT 2 I’ve updated the fiddle to give an example of displaying it in the DOM. The function that adds it to the DOM is recursive (will work for any depth of nested arrays) just because I felt like writing it like that, but the function that transforms the initial JSON data isn’t, so it doesn’t really matter. 🙂

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

Sidebar

Related Questions

I'm trying to create a TreeView which has two levels but I'm getting nowhere.
I am trying to create a simple TreeView which will represent my applications data
Trying to create a user account in a test. But getting a Object reference
I am trying to create a TreeView nested structure with the use of self
am trying to pupulate a treeview nodes base on directory structure like this Dim
I am trying to create a TreeView from the Silverlight TreeView control. I have
I am trying create a data.frame from which to create a graph. I have
I am trying to create a treeview that looks something like the following: Parent
I am trying to determine the best way to create a treeview list in
I'm trying to catch a double-click event in a TreeView's empty area to create

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.