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

  • Home
  • SEARCH
  • 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 8874787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:48:41+00:00 2026-06-14T18:48:41+00:00

I have an array of nested JSON structures where they have varying depth and

  • 0

I have an array of nested JSON structures where they have varying depth and not the same set of keys everywhere:

[
    {
        "name":"bob",
        "salary":10000,
        "friends":[
            {
                "name": "sarah",
                "salary":10000
            },
            {
                "name": "bill",
                "salary":5000
            }
        ]
    },
    {
        "name":"marge",
        "salary":10000,
        "friends":[
            {
                "name": "rhonda",
                "salary":10000
            },
            {
                "name": "mike",
                "salary":5000,
                "hobbies":[
                    {
                        "name":"surfing",
                        "frequency":10
                    },
                    {
                        "name":"surfing",
                        "frequency":15
                    }
                ]
            }
        ]
    },
    {
        "name":"joe",
        "salary":10000,
        "friends":[
            {
                "name": "harry",
                "salary":10000
            },
            {
                "name": "sally",
                "salary":5000
            }
        ]
    }
]

I wanted to use D3 to render this as nested html tables. For example the friends column will have tables showing the name, and salary of the friends of the individual referenced in the row. Sometimes one of these tables will have another level of a sub table.

I imagine the way to do this is by recursively creating tables. I wrote a python program that takes a JSON structure like this, and renders tables within tables, and the easiest way to do that was recursively. I see on the d3.js documentation there is a .each() thing you can call, which I am sure is what I need, I just need a little boost getting there (https://github.com/mbostock/d3/wiki/Selections#wiki-each).

So is there a nice way to do this in D3? I found this great example for rendering a 2d matrix of data as a table Creating a table linked to a csv file. With that tutorial I was able to get the outer most level of this data-structure rendered as a table, but I am stuck on how to go into levels recursively as needed, as of now they just show up as “Object” in the table since I am not treating them differently from normal strings and numbers.

Also I found this other question/answer that is similar to my question, but I really don’t understand javascript well enough to see where/how the recursion is happening and readapt the solution to fit my needs: How do I process data that is nested multiple levels in D3?. Any advice or pointers to tutorials on recursively or iteratively processing nested tree like JSON data-structures in D3 would be much appreciated!

  • 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-14T18:48:42+00:00Added an answer on June 14, 2026 at 6:48 pm

    A recursive function would probably be good approach. See code below for one possible implementation (assuming your data is stored in jdata). See the comments in the code for some explanation and see this Gist for a live version: http://bl.ocks.org/4085017

    d3.select("body").selectAll("table")
        .data([jdata])
      .enter().append("table")
        .call(recurse);
    
    function recurse(sel) {
      // sel is a d3.selection of one or more empty tables
      sel.each(function(d) {
        // d is an array of objects
        var colnames,
            tds,
            table = d3.select(this);
    
        // obtain column names by gathering unique key names in all 1st level objects
        // following method emulates a set by using the keys of a d3.map()
        colnames = d                                                     // array of objects
            .reduce(function(p,c) { return p.concat(d3.keys(c)); }, [])  // array with all keynames
            .reduce(function(p,c) { return (p.set(c,0), p); }, d3.map()) // map with unique keynames as keys
            .keys();                                                     // array with unique keynames (arb. order)
    
        // colnames array is in arbitrary order
        // sort colnames here if required
    
        // create header row using standard 1D data join and enter()
        table.append("thead").append("tr").selectAll("th")
            .data(colnames)
          .enter().append("th")
            .text(function(d) { return d; });
    
        // create the table cells by using nested 2D data join and enter()
        // see also http://bost.ocks.org/mike/nest/
        tds = table.append("tbody").selectAll("tr")
            .data(d)                            // each row gets one object
          .enter().append("tr").selectAll("td")
            .data(function(d) {                 // each cell gets one value
              return colnames.map(function(k) { // for each colname (i.e. key) find the corresponding value
                return d[k] || "";              // use empty string if key doesn't exist for that object
              });
            })
          .enter().append("td");
    
        // cell contents depends on the data bound to the cell
        // fill with text if data is not an Array  
        tds.filter(function(d) { return !(d instanceof Array); })
            .text(function(d) { return d; });
        // fill with a new table if data is an Array
        tds.filter(function(d) { return (d instanceof Array); })
            .append("table")
            .call(recurse);
      });    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have javascript object which consists of complex array of structures with nested structures
Not sure how to extract the array in JSON from controller. I have a
I have a JSON array like this [{Email:someone@some.com,Name:ACO,Groups:[MOD_SW,MOD_PI,MOD_GE],Id:63,Url:aco}, {Email:someone@some.com,Name:Agpo,Groups:[MOD_PI],Id:22,Url:agpo}, {Email:someone@some.com,Name:Akatherm,Groups:[MOD_SW],Id:64,Url:akatherm}, {Email:someone@some.com,Name:Albrand,Groups:[MOD_PI,MOD_GE],Id:23,Url:albrand}] I want to
I have a nested array with valid numbers => data:- $validData = array(array(1 =>
I have a generated nested Array which I store some data. How can i
I have a simple structure in mongodb, with nested array. How can I update
I have a nested associative array like this: $inputTypes= array( natural => array( text,
I have a nested php array. I have the first select populated already. I
I have a hierarchically nested associative array. It looks like this: A = {
I have array of select tag. <select id='uniqueID' name=status> <option value=1>Present</option> <option value=2>Absent</option> </select>

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.