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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:51:04+00:00 2026-06-10T10:51:04+00:00

I have an array of nodes. Each node has an array of children and

  • 0

I have an array of nodes.
Each node has an array of children and a pointer to its parent.
I would like to serialize it using JSON.stringify, but with the parent pointers I obviously end up with circular references, and JSON throws an exception. What can I do to work-around the circular references and serialize with JSON?

Related question: Chrome sendrequest error: TypeError: Converting circular structure to JSON

  • 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-10T10:51:06+00:00Added an answer on June 10, 2026 at 10:51 am

    You should create a customized toJson function in the objects that have parents.

    From the documentation

    If an object being stringified has a property named toJSON whose value
    is a function, then the toJSON method customizes JSON stringification
    behavior

    var x = {
      foo: 'foo',
      toJSON: function () {
        return 'bar';
      }
    };
    var json = JSON.stringify({x: x});
    

    So you could create that function in the objects that have parent references, something like this maybe?

    MyObj = function(){
        this.xxx = 'foobar';
        this.zzz = 'foooobar';
        this.name = 'foo';
        this.parent = ...;
        toJSON = function(){
            tmp = '{'
            for(prop in MyObj){
                if(prop == 'parent'){
                    tmp += 'parent: "'+ this['parent'].name +'"'; //maybe?? optional!
                }else{
                    tmp += prop + ':' + this[prop].stringify + ','; //you will still use the browser function
                }
                tmp += '}            
            }
            return tmp;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tree, each node contains an array children nodes (Node *children_nodes), as
I have to loop in a JSON array to get some informations in node
I have a DAG in a JSON format, where each node is an entry:
I have a linked list of Nodes, each Node is defined as: struct Node
I have a javascript array of objects that I would like to use to
One of our sites has around 10,000 nodes. In each node, there is a
Suppose I have an array of nodes (objects). I need to create a duplicate
C++ Notes: Array Initialization has a nice list over initialization of arrays. I have
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have multiple binary trees stored as an array. In each slot is either

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.