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.stack.imgur.com/eRqKh.jpg

  • possible duplicate of [jquery json to string?](http://stackoverflow.com/questions/3593046/jquery-json-to-string) – Noy Nov 17 '14 at 09:14
  • `JSON.stringify` _is_ the built in method, at least in modern browsers. Can you post the JSON you're trying to stringify? – James Thorpe Nov 17 '14 at 09:14
  • JSON is a string of a particular shape. You are trying to stringify a JavaScript object, not a JSON. – Amadan Nov 17 '14 at 09:15
  • It might be a duplicate of this question : http://stackoverflow.com/questions/13861254/json-stringify-deep-objects – Denys Séguret Nov 17 '14 at 09: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. –  Nov 17 '14 at 09:24
  • my problem is that I had nested JSON objects and I can't stringfy this and one of them had HTML tags – Muhammed Abd El-Wadod Nov 17 '14 at 09:34
  • Image of my Object http://imgur.com/s3HbdtQ – Muhammed Abd El-Wadod Nov 17 '14 at 09:43

4 Answers4

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
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    You can also use one of the YAML libraries; YAML, unlike JSON, allows for circular representations. – Amadan Nov 17 '14 at 09:17
  • This my object before converting : http://imgur.com/s3HbdtQ and this after converting : http://imgur.com/ilWUGcU I had skipped the error which appear but also this lib skipped some data which is important to me – Muhammed Abd El-Wadod Nov 17 '14 at 10:05
  • @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. – Denys Séguret Nov 17 '14 at 10:07
  • 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 – Muhammed Abd El-Wadod Nov 17 '14 at 10:16
  • and YAML is node.js and I can't use it in app – Muhammed Abd El-Wadod Nov 17 '14 at 10:33
  • @MuhammedAbdEl-Wadod The page I linked you to had a link to a version you can use in your browser – Denys Séguret Nov 17 '14 at 10:37
  • @dystroy I did so and the result is : This my object before converting : imgur.com/s3HbdtQ and this after converting : imgur.com/ilWUGcU – Muhammed Abd El-Wadod Nov 17 '14 at 10:50
  • @dystroy I think the problem is that there is call for n.fn.init inside the JSON object – Muhammed Abd El-Wadod Nov 17 '14 at 11:15
  • The problem is the fact that you are trying to serialise DOM objects, such as `option`. You can't do that. Serialise the relevant DOM objects' properties and attributes instead, or `outerHTML` representation, as already noted by @torazaburo. – Amadan Nov 18 '14 at 08:09
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.

zenw0lf
  • 1,232
  • 1
  • 13
  • 22
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

Mig82
  • 4,856
  • 4
  • 40
  • 63
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]"
  ]
}
Gagan
  • 1,267
  • 3
  • 18
  • 28