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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:19:36+00:00 2026-06-18T06:19:36+00:00

Dear Stackoverflow Members, I have this JSON array, and it consists of the following

  • 0

Dear Stackoverflow Members,

I have this JSON array, and it consists of the following items (basically):

{
    {
        'Name': 'x',
        'Id': 'y',
        'Unsusedstuff' : 'unused',
        'Unsusedstuff2' : 'unused2',
        'Children': []
    },
    {   'Name' : 'xx',
        'Id': 'yy',
        'Unsusedstuff' : 'unused',
        'Unsusedstuff2' : 'unused2',
        'Children': [{
            'Name': 'xyx',
            'Id' : 'yxy',
            'Unsusedstuff' : 'unused',
            'Unsusedstuff2' : 'unused2',
            'Children: []
        }

You get the basic idea. I want to emulate this (and just grab the id and the name and the structure) in a Python-list using the following code:

names = []
def parseNames(col):
    for x in col:
        if(len(x['Children'])> 0):
            names.append({'Name' : x['Name'], 'Id' : x['Id'], 'Children' : parseNames(x['Children'])})
        else:
            return {'Name' : x['Name'], 'Id' : x['Id']}

But, it only seems to return the first ‘root’ and the first nested folder, but doesn’t loop through them all.

How would I be able to fix this?

Greetings,

Mats

  • 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-18T06:19:37+00:00Added an answer on June 18, 2026 at 6:19 am

    The way I read this, you’re trying to convert this tree into a tree of nodes which only have Id, Name and Children. In that case, the way I’d think of it is as cleaning nodes.

    To clean a node:

    1. Create a node with the Name and Id of the original node.
    2. Set the new node’s Children to be the cleaned versions of the original node’s children. (This is the recursive call.)

    In code, that would be:

    def clean_node(node):
        return {
            'Name': node['Name'],
            'Id': node['Id'],
            'Children': map(clean_node, node['Children']),
        }
    
    >>> print map(clean_node, data)
    [{'Name': 'x', 'Children': [], 'Id': 'y'}, {'Name': 'xx', 'Children': [{'Name': 'xyx', 'Children': [], 'Id': 'yxy'}], 'Id': 'yy'}]
    

    I find it’s easier to break recursive problems down like this – trying to use global variables turns simple things very confusing very quickly.

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

Sidebar

Related Questions

Dear Stackoverflow members I have a recursive CTE query like this: DECLARE @Level TABLE
Dear StackOverFlow Members, Please help me with this batch file. I would like to
Dear stackoverflow members, I have a small problem, I want to replace some characters
Dear members of the Stackoverflow community, We are developing a web application using the
Dear all,Now i have this question in my java program,I think it should be
Dear stackoveflow, I have this problem. I'm working with an old version of mssql
Dear Delphi Folks who know Assembler -- I have this great routine that worked
Dear all I have got the following code in my .htaccess in the folder
Dear Stackoverflow Developers i have to load image as an icon to jmenu from
dear all..i have a table it looks like: Name version DDX 01 DTX 05

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.