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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:52:18+00:00 2026-05-18T09:52:18+00:00

So, JSON.stringify provides a great way to turn a JS object like: var baz

  • 0

So, JSON.stringify provides a great way to turn a JS object like:

var baz = {"foo":1, "bar":someFunction};

in to a JSON string like:

{"foo":1}

It does this with an optional second argument that controls which fields should be serialized:

JSON.stringify(baz, ["foo"]);

That’s great, but there’s a problem. Let’s say your “baz” is actually the property of another object, and you want to serialize that other object:

someObject.baz = {"foo":1, "bar":someFunction};
JSON.stringify(someObject, ["baz"]);

Well, normally you would just define a toJSON method on baz, eg.:

someObject.baz = {"foo":1, "bar":someFunction};
someObject.baz.toJSON = function() { /* logic to "toJSON" baz*/ }
JSON.stringify(someObject, ["baz"]);

Now, as I mentioned earlier, we have the perfect logic to “toJSON” baz already:

someObject.baz.toJSON = function() {
    return JSON.stringify(baz, ["foo"]);
}

but if you try putting that in to your toJSON, you’ll get a recursion error, because stringify will trigger the toJSON, which will trigger the stringify, which will … 🙁

You can work around this with a hack:

someObject.baz.toJSON = function() {
    var oldToJON = this.toJSON;
    this.toJSON = null;
    var ret = JSON.stringify(baz, ["foo"]);
    this.toJSON = oldToJON;
    return ret;
}

But … that just seems wrong. So, my question is: is there any way you can utilize the nifty built-in serialization power of JSON.stringify inside a toJSON method of an object (without having to hide the toJSON method itself during the stringify operation)?

  • 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-18T09:52:18+00:00Added an answer on May 18, 2026 at 9:52 am

    Crockford’s json2.js says:

    A toJSON method does not serialize: it returns the value represented by the name/value pair that should be serialized, or undefined if nothing should be serialized.

    So you are simply expected to return the value that you want serialized. In your case, baz.toJSON should simply return the portion of the baz object that you want serialized:

    someObject.baz.toJSON = function() {
      return { foo: this.foo };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I JSON.stringify() the following code: var exampleObject = { name : Žiga Kovač,
i can use this to make a json on web browser: var json_string =
I've got a function: function createOrLoadDB (host) { var db = JSON.parse( window.localStorage.getItem(host) )
as the title specifies, my hosting provider does not have support for json_decode, so
Possible Duplicate: How can I beautify JSON programmatically? I know how to generate JSON
I'm aiming to build up a JSON array of mouse positions and pass them
Recently I ran into a problem: Valid JSON when passed to the eval() function
I send data from javascript to PHP like this: $.ajax({'url': 'my.php', 'type': 'POST', 'data':
I'm having trouble printing an object with a variable name. It works when I
I have a web method in one of my aspx pages: [WebMethod] public static

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.