How to Print a Circular Structure in a Json-Like Format

How can I print a circular structure in a JSON-like format?

Use JSON.stringify with a custom replacer. For example:

// Demo: Circular reference
var circ = {};
circ.circ = circ;

// Note: cache should not be re-used by repeated calls to JSON.stringify.
var cache = [];
JSON.stringify(circ, (key, value) => {
if (typeof value === 'object' && value !== null) {
// Duplicate reference found, discard key
if (cache.includes(value)) return;

// Store value in our collection
cache.push(value);
}
return value;
});
cache = null; // Enable garbage collection

The replacer in this example is not 100% correct (depending on your definition of "duplicate"). In the following case, a value is discarded:

var a = {b:1}
var o = {};
o.one = a;
o.two = a;
// one and two point to the same object, but two is discarded:
JSON.stringify(o, ...);

But the concept stands: Use a custom replacer, and keep track of the parsed object values.

As a utility function written in es6:

// safely handles circular references
JSON.safeStringify = (obj, indent = 2) => {
let cache = [];
const retVal = JSON.stringify(
obj,
(key, value) =>
typeof value === "object" && value !== null
? cache.includes(value)
? undefined // Duplicate reference found, discard key
: cache.push(value) && value // Store value in our collection
: value,
indent
);
cache = null;
return retVal;
};

// Example:
console.log('options', JSON.safeStringify(options))

How can I print a circular structure in a JSON-like format?

Use JSON.stringify with a custom replacer. For example:

// Demo: Circular reference
var circ = {};
circ.circ = circ;

// Note: cache should not be re-used by repeated calls to JSON.stringify.
var cache = [];
JSON.stringify(circ, (key, value) => {
if (typeof value === 'object' && value !== null) {
// Duplicate reference found, discard key
if (cache.includes(value)) return;

// Store value in our collection
cache.push(value);
}
return value;
});
cache = null; // Enable garbage collection

The replacer in this example is not 100% correct (depending on your definition of "duplicate"). In the following case, a value is discarded:

var a = {b:1}
var o = {};
o.one = a;
o.two = a;
// one and two point to the same object, but two is discarded:
JSON.stringify(o, ...);

But the concept stands: Use a custom replacer, and keep track of the parsed object values.

As a utility function written in es6:

// safely handles circular references
JSON.safeStringify = (obj, indent = 2) => {
let cache = [];
const retVal = JSON.stringify(
obj,
(key, value) =>
typeof value === "object" && value !== null
? cache.includes(value)
? undefined // Duplicate reference found, discard key
: cache.push(value) && value // Store value in our collection
: value,
indent
);
cache = null;
return retVal;
};

// Example:
console.log('options', JSON.safeStringify(options))

Converting circular structure to JSON at JSON.stringify ()

A circular structure is a structure which references itself as a value. JSON.stringify does not support such structures, since it would result in an infinitely-long string.

What you need is a deep cloning function which does not use JSON.stringify. Such implementation can be found here.

What is a circular structure in JSON?

The circular structure you have is not in JSON, but in the object that you are trying to convert to JSON.

Circular structures come from objects which contain something which references the original object. JSON does not have a manner to represent these.

An example would be a collection object where the child objects contain a reference to the parent:

  • Document contains a list of Nodes, and each Node has a reference to its containing Document.
  • A game may have a list of Players, who may be carrying one or more Items. Each Item may know it's current owner Player.

TypeError: Converting circular structure to JSON in nodejs

JSON doesn't accept circular objects - objects which reference themselves. JSON.stringify() will throw an error if it comes across one of these.

The request (req) object is circular by nature - Node does that.

In this case, because you just need to log it to the console, you can use the console's native stringifying and avoid using JSON:

console.log("Request data:");
console.log(req);

Saving Matter.js engine.world as JSON

What you are looking for is resurrect-js

You can stringify the data using:

var necromancer = new Resurrect();
var data = necromancer.stringify(engine.world);

You can then save data as a string in whatever text file you want.

To turn the string back into the object, you can use:

var world = necromancer.resurrect(data);
engine.world = world;

Hope this helps!

TypeError: Converting circular structure to JSON -- Next.js

I guess your problem is here: res.status(200).json(JSON.stringify(rows));. So, from the docs of res.json:

Sends a JSON response. This method sends a response (with the correct
content-type) that is the parameter converted to a JSON string using
JSON.stringify().

Therefore you just need res.json(rows); or maybe res.status(201).json(rows);, where 201 means Created.

Edit:

I think there might be another problem. The query to insert data does not return the inserted rows (if you use mysql). So, please log rows to check its content - and as mentioned by @jfriend00 there could be and properly are some circular references.

However, you can get the id of the inserted row or the number of affected rows for example.



Related Topics



Leave a reply



Submit