Using JSON.stringify() on an object that contains cyclical references

I created a Building object that had a 'marker' property (and some other properties). The 'marker' property contained a Google Map Marker, and I'm not sure of it's internals, but when I tried to use JSON.stringify() on it, I got a 'cyclic object value' error. After digging around, I found that you can pass in a second param to stringify(), which is a function that you can use to override the values of properties in the output string. In this case I used it to exclude the marker property from the string.

 

var json = JSON.stringify(buildingObj, function(key,value){
    if(key == "marker"){
        return undefined;
    }
    return value;
});

Add a Comment...

 

Comments

That's the most lazy and lame solution of all times. Just try to apply your solution to window object in case you want to JSON.stringify(window) from inside an iframe. - 9/04/2016

An impressive share! I have just forwarded this onto a colleague who has been conducting a little research on this. And he in fact ordered me breakfast due to the fact that I stumbled upon it for him... lol. So let me reword this.... Thanks for the meal!! But yeah, thanks for spending the time to discuss this topic here on your website. - 8/30/2016

Have you ever considered publishing an ebook or guest authoring on other websites? I have a blog based upon on the same subjects you discuss and would really like to have you share some stories/information. I know my subscribers would enjoy your work. If you're even remotely interested, feel free to shoot me an e-mail. - 3/25/2017

ER - 2/22/2017

block out this online casinos where you can conquer legal kale with the usurp of this guidebookPlaying free casino games - 2/16/2017

Attractive section of content. I just stumbled upon your website and in accession capital to assert that I get in fact enjoyed account your blog posts. Any way I will be subscribing to your feeds and even I achievement you access consistently rapidly. - 12/01/2016

This info is worth everyone's attention. When can I find out more? - 11/17/2016

I was wondering if you ever looked at changing the page layout of your own blog? Its adequately written; I really like what youve have got to say. But maybe you could a bit more when it comes to content so people could connect with it better. Youve got so much of text for only having 1 or two pictures. You could possibly could space it all out better? - 10/07/2016