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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:41:29+00:00 2026-05-30T05:41:29+00:00

I have an object (parse tree) that contains child nodes which are references to

  • 0

I have an object (parse tree) that contains child nodes which are references to other nodes.

I’d like to serialize this object, using JSON.stringify(), but I get

TypeError: cyclic object value

because of the constructs I mentioned.

How could I work around this? It does not matter to me whether these references to other nodes are represented or not in the serialized object.

On the other hand, removing these properties from the object when they are being created seems tedious and I wouldn’t want to make changes to the parser (narcissus).

  • 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-30T05:41:30+00:00Added an answer on May 30, 2026 at 5:41 am

    Use the second parameter of stringify, the replacer function, to exclude already serialized objects:

    var seen = [];
    
    JSON.stringify(obj, function(key, val) {
       if (val != null && typeof val == "object") {
            if (seen.indexOf(val) >= 0) {
                return;
            }
            seen.push(val);
        }
        return val;
    });
    

    http://jsfiddle.net/mH6cJ/38/

    As correctly pointed out in other comments, this code removes every “seen” object, not only “recursive” ones.

    For example, for:

    a = {x:1};
    obj = [a, a];
    

    the result will be incorrect. If your structure is like this, you might want to use Crockford’s decycle or this (simpler) function which just replaces recursive references with nulls:

    function decycle(obj, stack = []) {
        if (!obj || typeof obj !== 'object')
            return obj;
        
        if (stack.includes(obj))
            return null;
    
        let s = stack.concat([obj]);
    
        return Array.isArray(obj)
            ? obj.map(x => decycle(x, s))
            : Object.fromEntries(
                Object.entries(obj)
                    .map(([k, v]) => [k, decycle(v, s)]));
    }
    
    //
    
    let a = {b: [1, 2, 3]}
    a.b.push(a);
    
    console.log(JSON.stringify(decycle(a)))
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have object A which contains multiple instances of object B, which in turn
I have an xml file that looks like this: <!DOCTYPE ROOT SYSTEM zombie.dtd> <ROOT>
I want to have a map that permits a tree-like behaviour. I want to
I have defined a pyparsing rule to parse this text into a syntax-tree... TEXT
I have an object. fp = open(self.currentEmailPath, rb) p = email.Parser.Parser() self._currentEmailParsedInstance= p.parse(fp) fp.close()
I have a model which gets its data from a parser object. I'm thinking
I have a recursion function that parses an object/array with a global variable. If
I have parsed XML file into objects, in which each object has a 1:1
My application receives strings that represent objects. I have to parse the strings to
I have object A which in turn has a property of type Object B

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.