We're looking for a person or people to help audit MDN to find places we could speed up. Is this you or someone you know? Check out the RFP: https://mzl.la/2IHcMiE

Önkénteseink még nem fordították le ezt a cikket magyar nyelvre. Csatlakozzon, és segítsen a munka befejezésében!
Elolvashatja a cikket English (US) nyelven is.


TypeError: cyclic object value (Firefox)
TypeError: Converting circular structure to JSON (Chrome)

Error type


What went wrong?

When calling JSON.stringify() cyclic object reference structures can't be converted to a string.


In a circular structure like the following

var a = {};
var b = {}; 
a.child = b;
b.child = a;

JSON.stringify() will fail

// TypeError: cyclic object value

You will need to check for cyclic object references before stringifying. For example by specifying a replacer function as the second argument of JSON.stringify().

seen = []; 

var replacer = function(key, value) {
  if (value != null && typeof value == "object") {
    if (seen.indexOf(value) >= 0) {
  return value;

JSON.stringify(a, replacer); 
// "{"child":{}}"

Or, you can use a library or utility functions that have already been written for this scenario. For example, there is cycle.js by Douglas Crockford.

See also

  • JSON.stringify
  • cycle.js – Introduces two functions, JSON.decycle and JSON.retrocycle, which make it possible to encode cyclical structures and dags in JSON, and to then recover them.

Dokumentum címkék és a közreműködők

 Ezen oldal közreműködői: fscholz
 Utoljára frissítve: fscholz,