2

Is there build-in method which can stringify a complex JSON object ?

Every time i use JSON.stringfy I get this error: Converting circular structure to JSON , and also I need its parser

this an image of my object https://i.sstatic.net/eRqKh.jpg

7
  • possible duplicate of jquery json to string? Commented Nov 17, 2014 at 9:14
  • JSON.stringify is the built in method, at least in modern browsers. Can you post the JSON you're trying to stringify? Commented Nov 17, 2014 at 9:14
  • JSON is a string of a particular shape. You are trying to stringify a JavaScript object, not a JSON. Commented Nov 17, 2014 at 9:15
  • It might be a duplicate of this question : stackoverflow.com/questions/13861254/… Commented Nov 17, 2014 at 9:17
  • You get the error EVERY TIME you use JSON.stringify? Try it on JSON.stringify(99). If this is your old stringifying-the-DOM question, make sure to take outerHTML before trying to stringify, as already described in an answer to another question. Commented Nov 17, 2014 at 9:24

4 Answers 4

4

You can't exactly convert a circular structure to JSON : there's nothing to describe those relations that can't just be dumped in a finite time.

Alternatives :

  • use another format than JSON (for example YAML as suggested by Armadan)
  • extend JSON with an additional JSON compatible syntax to describe references
  • use a library removing the circular references (producing some JSON but without what can't be stringified). I made such a library : https://github.com/Canop/JSON.prune
Sign up to request clarification or add additional context in comments.

9 Comments

You can also use one of the YAML libraries; YAML, unlike JSON, allows for circular representations.
This my object before converting : imgur.com/s3HbdtQ and this after converting : imgur.com/ilWUGcU I had skipped the error which appear but also this lib skipped some data which is important to me
@MuhammedAbdEl-Wadod And what would you expect ? If you want to restore your object as it was, use a serialization format supporting circular references (for example YAML) not JSON.
I want to stringfy JSON to store it at sessionsttorage and after refresh browser i want to get it back from the sessionsttorage and this object creating at the run time and every time i don't know what this object will carry
and YAML is node.js and I can't use it in app
|
1

Take a look at this little library:

https://github.com/WebReflection/circular-json

It serializes and deserializes otherwise valid JSON objects containing circular references into and from a specialized JSON format.

1 Comment

This answer needs additional information
1

I recommend Flatted. It's tiny and it works very well. The minified version of it weighs 1KB.

enter image description here

Here's an example from their docs.

var a = [{one: 1}, {two: '2'}];
a[0].a = a;
// a is the main object, will be at index '0'
// {one: 1} is the second object, index '1'
// {two: '2'} the third, in '2', and it has a string
// which will be found at index '3'

Flatted.stringify(a);
// [["1","2"],{"one":1,"a":"0"},{"two":"3"},"2"]

It can then parse the stringified result back if necessary and rebuild the circular references.

var a = [{one: 1}, {two: '2'}]
a[0].a = a
Flatted.stringify(a)
var a1 = Flatted.stringify(a)
var a2 = Flatted.parse(a1)

enter image description here

Comments

0

The standard and best solution is json-stringify-safe module.

Here is the usage (described in module):

var stringify = require('json-stringify-safe');
var circularObj = {};
circularObj.circularRef = circularObj;
circularObj.list = [ circularObj, circularObj ];
console.log(stringify(circularObj, null, 2));

// Output:

{
  "circularRef": "[Circular]",
  "list": [
    "[Circular]",
    "[Circular]"
  ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.