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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:51:37+00:00 2026-05-30T16:51:37+00:00

I want to create a multi-level JSON string with JS. Scenario 3 countries with

  • 0

I want to create a multi-level JSON string with JS.

Scenario

3 countries with 5 grandfathers with 3 kids which whom also have 3 kids that have 5 friends.

I get the data from a external JSON file that looks like this.

 {"countries":[
    {
        "name":"USA",

        "grandfathers":[
            {
                "gFName":"Steve",
                "grandfathersKid":[
                    {
                        "gFKName": "Linda",
                        "kid": [{
                            "name": "Steve JR", 
                            "friends": [{
                                "name": "Kriss|John|Martin|Steven"
                            }]
                        }
                        ]
                    }

                ]
            }
        ]
    }
]}

And now I want to store some of the countries with people and their relatives and friends in a a new JSON list that looks exactly as the list made in the external json file. I aim to use this “homemade” list later on in the script.

My initial response for this was

var tree = new Array();

tree = {};


var countries = new Array();

countries[0] = "canada";
countries[1] = "USA";
countries[2] = "Mexico";
countries[0][0] = "Steve"; //Lives in Canada
countries[0][0][0] = "Linda"; //Daughter of Steve
countries[0][0][0][0] = "Steve JR"; // Kid of Linda
countries[0][0][0][0][0] = "Kriss"; //Steves Friend
...


$.each(countries...function(index, value){
      tree[index].country = value;

  $.each(grandfathers...function(key, value){
      tree[index].country[key].grandfather = value;

}

And so on, but this is not giving me the result I want. What am I doing wrong? And a more effective way than to take each of everything?

Third edit…

  • 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-30T16:51:39+00:00Added an answer on May 30, 2026 at 4:51 pm

    Is this the sort of thing you’re trying to do?

    var countries = $.map(oldCountries || [], function(country) {
        return {
            name: country.name,
            people: $.map(country.grandfathers || [], function(gpa) {
                return {
                    name: gpa.gFName,
                    children: $.map(gpa.grandfathersKid || [], function(parent) {
                        return {
                            name: parent.gFKName,
                            children: $.map(parent.kid || [], function(kid) {
                                return {
                                    name: kid.name,
                                    friends: kid.friends
                                };
                            })
                        };
                    })
                };
            })
        };
    });
    

    I wasn’t sure what to do with the friends node. Should that be normalized into something more useful, or do you want to leave it alone?

    This Fiddle demonstrates the technique.

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

Sidebar

Related Questions

I want to create a makefile variable that is a multi-line string (e.g. the
I want to create a multi-tenant application that uses the hostname to determine the
I'm trying to create a multi-level editor in MVC3. By multi-level I mean that
I'm trying to create a folder structure which has multi level sub folders. For
i want create image animation , i have 50 images with png format now
I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic
I want PHP to randomly create a multi-dimensional array by picking a vast amount
I want to create a class that could hold any of a number of
I want to create a multi platform application whose support all type of window

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.