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

Harry

I have a big object I want to convert to JSON and send. However it has circular structure. I want to toss whatever circular references exist and send whatever can be stringified. How do I do that?

Thanks.

var obj = {
  a: "foo",
  b: obj
}

I want to stringify obj into:

{"a":"foo"}
Rob W

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))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I optimize this code to print in this format?

From Dev

How can I format a list to print?

From Dev

How can i improve this data like structure in python?

From Dev

How Can I print like below using thread synchronization method?

From Dev

How can i print multidimensional array like this in php?

From Dev

How can I format my JSON in this way?

From Dev

How can I format json in angularjs?

From Dev

How can I format JSON output in javascript

From Dev

How can I print out a matrix in the following format in prolog

From Dev

How can I format a fraction to look like a formula

From Dev

How to format the JSON Structure in ReactJs

From Dev

Circular Structure to JSON in Chrome

From Dev

Print whole structure with single call (like JSON.stringify) in Java?

From Dev

How can I print this?

From Dev

Can I process a string which is like a json format using d3.json?

From Dev

How can I parse the following JSON structure in Go

From Dev

How can I deserialize a JSON array into a native .net data structure?

From Dev

How can I create json output from deep li structure

From Dev

How can I access the data in this JSON structure from jQuery?

From Dev

How do I tell python what my data structure (that is in binary) looks like so I can plot it?

From Dev

How to change json structure to look like another json structure

From Dev

how can I format string like 9:34, 18:00 to format of 00:00

From Dev

How can I print a ctemplate::TemplateDictionary in JSON form?

From Java

How can I pretty-print JSON using Go?

From Dev

How can I print the Jsonarray in json-Glib?

From Dev

How can I print values of nested objects of JSON string?

From Dev

How can I print a ctemplate::TemplateDictionary in JSON form?

From Dev

How can i print json objects and array values?

From Dev

How can I print json array in Angular 4?

Related Related

  1. 1

    How can I optimize this code to print in this format?

  2. 2

    How can I format a list to print?

  3. 3

    How can i improve this data like structure in python?

  4. 4

    How Can I print like below using thread synchronization method?

  5. 5

    How can i print multidimensional array like this in php?

  6. 6

    How can I format my JSON in this way?

  7. 7

    How can I format json in angularjs?

  8. 8

    How can I format JSON output in javascript

  9. 9

    How can I print out a matrix in the following format in prolog

  10. 10

    How can I format a fraction to look like a formula

  11. 11

    How to format the JSON Structure in ReactJs

  12. 12

    Circular Structure to JSON in Chrome

  13. 13

    Print whole structure with single call (like JSON.stringify) in Java?

  14. 14

    How can I print this?

  15. 15

    Can I process a string which is like a json format using d3.json?

  16. 16

    How can I parse the following JSON structure in Go

  17. 17

    How can I deserialize a JSON array into a native .net data structure?

  18. 18

    How can I create json output from deep li structure

  19. 19

    How can I access the data in this JSON structure from jQuery?

  20. 20

    How do I tell python what my data structure (that is in binary) looks like so I can plot it?

  21. 21

    How to change json structure to look like another json structure

  22. 22

    how can I format string like 9:34, 18:00 to format of 00:00

  23. 23

    How can I print a ctemplate::TemplateDictionary in JSON form?

  24. 24

    How can I pretty-print JSON using Go?

  25. 25

    How can I print the Jsonarray in json-Glib?

  26. 26

    How can I print values of nested objects of JSON string?

  27. 27

    How can I print a ctemplate::TemplateDictionary in JSON form?

  28. 28

    How can i print json objects and array values?

  29. 29

    How can I print json array in Angular 4?

HotTag

Archive