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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:47:18+00:00 2026-06-16T21:47:18+00:00

I need a function building a JSON valid string from any argument but :

  • 0

I need a function building a JSON valid string from any argument but :

  • avoiding recursivity problem by not adding objects twice
  • avoiding call stack size problem by truncating past a given depth

Generally it should be able to process big objects, at the cost of truncating them.

As reference, this code fails :

var json = JSON.stringify(window);

Avoiding recursivity problem is simple enough :

var seen = [];
return JSON.stringify(o, function(_, value) {
    if (typeof value === 'object' && value !== null) {
        if (seen.indexOf(value) !== -1) return;
        else seen.push(value);
    }
    return value;
});

But for now, apart copying and changing Douglas Crockford’s code to keep track of the depth, I didn’t find any way to avoid stack overflow on very deep objects like window or any event. Is there a simple solution ?

  • 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-16T21:47:19+00:00Added an answer on June 16, 2026 at 9:47 pm

    I did what I initially feared I’ll have to do : I took Crockford’s code and modified it for my needs. Now it builds JSON but handles

    • cycles
    • too deep objects
    • too long arrays
    • exceptions (accessors that can’t legally be accessed)

    In case anybody needs it, I made a GitHub repository : JSON.prune on GitHub

    Here is the code :

    // JSON.pruned : a function to stringify any object without overflow
    // example : var json = JSON.pruned({a:'e', c:[1,2,{d:{e:42, f:'deep'}}]})
    // two additional optional parameters :
    //   - the maximal depth (default : 6)
    //   - the maximal length of arrays (default : 50)
    // GitHub : https://github.com/Canop/JSON.prune
    // This is based on Douglas Crockford's code ( https://github.com/douglascrockford/JSON-js/blob/master/json2.js )
    (function () {
        'use strict';
    
        var DEFAULT_MAX_DEPTH = 6;
        var DEFAULT_ARRAY_MAX_LENGTH = 50;
        var seen; // Same variable used for all stringifications
    
        Date.prototype.toPrunedJSON = Date.prototype.toJSON;
        String.prototype.toPrunedJSON = String.prototype.toJSON;
    
        var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
            escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
            meta = {    // table of character substitutions
                '\b': '\\b',
                '\t': '\\t',
                '\n': '\\n',
                '\f': '\\f',
                '\r': '\\r',
                '"' : '\\"',
                '\\': '\\\\'
            };
    
        function quote(string) {
            escapable.lastIndex = 0;
            return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
                var c = meta[a];
                return typeof c === 'string'
                    ? c
                    : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
            }) + '"' : '"' + string + '"';
        }
    
        function str(key, holder, depthDecr, arrayMaxLength) {
            var i,          // The loop counter.
                k,          // The member key.
                v,          // The member value.
                length,
                partial,
                value = holder[key];
            if (value && typeof value === 'object' && typeof value.toPrunedJSON === 'function') {
                value = value.toPrunedJSON(key);
            }
    
            switch (typeof value) {
            case 'string':
                return quote(value);
            case 'number':
                return isFinite(value) ? String(value) : 'null';
            case 'boolean':
            case 'null':
                return String(value);
            case 'object':
                if (!value) {
                    return 'null';
                }
                if (depthDecr<=0 || seen.indexOf(value)!==-1) {
                    return '"-pruned-"';
                }
                seen.push(value);
                partial = [];
                if (Object.prototype.toString.apply(value) === '[object Array]') {
                    length = Math.min(value.length, arrayMaxLength);
                    for (i = 0; i < length; i += 1) {
                        partial[i] = str(i, value, depthDecr-1, arrayMaxLength) || 'null';
                    }
                    v = partial.length === 0
                        ? '[]'
                        : '[' + partial.join(',') + ']';
                    return v;
                }
                for (k in value) {
                    if (Object.prototype.hasOwnProperty.call(value, k)) {
                        try {
                            v = str(k, value, depthDecr-1, arrayMaxLength);
                            if (v) partial.push(quote(k) + ':' + v);
                        } catch (e) { 
                            // this try/catch due to some "Accessing selectionEnd on an input element that cannot have a selection." on Chrome
                        }
                    }
                }
                v = partial.length === 0
                    ? '{}'
                    : '{' + partial.join(',') + '}';
                return v;
            }
        }
    
        JSON.pruned = function (value, depthDecr, arrayMaxLength) {
            seen = [];
            depthDecr = depthDecr || DEFAULT_MAX_DEPTH;
            arrayMaxLength = arrayMaxLength || DEFAULT_ARRAY_MAX_LENGTH;
            return str('', {'': value}, depthDecr, arrayMaxLength);
        };
    
    }());
    

    An example of what can be done :

    var json = JSON.pruned(window);
    

    Note: Contrary to the code in this answer, the GitHub repository is updated when needed (documentation, compatibility, use as module in commonjs or node, specific serializations, etc.). It’s a good idea to start from the repository if you need this pruning feature.

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

Sidebar

Related Questions

I am building a common function library but the functions inside need to reference
I have a JSON string which includes a function I need to call. My
I'm building a json object in java. I need to pass a function into
i'm building this little function, what i need is this query to print me
in function need key to encrypt string without mcrypt libraly in php function encrypt($str,
I need following function (from C++ dll) available in C++/CLI extern C _declspec(dllexport) void
I need a function that can print Turkish characters. public String convert(String input) {
i need help building a function that display an associative array and i want
I am building a small app in Lazarus and need a parse function based
I need to call a Javascript function from an a element.. something like this:

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.