Is there build-in method which can stringify a plex 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 .jpg
Is there build-in method which can stringify a plex 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/eRqKh.jpg
Share Improve this question edited Nov 17, 2014 at 9:41 Muhammed Abd El-Wadod asked Nov 17, 2014 at 9:12 Muhammed Abd El-WadodMuhammed Abd El-Wadod 311 silver badge5 bronze badges 7- possible duplicate of jquery json to string? – Noy 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? – James Thorpe 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. – Amadan Commented Nov 17, 2014 at 9:15
- It might be a duplicate of this question : stackoverflow./questions/13861254/… – Denys Séguret 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 takeouterHTML
before trying to stringify, as already described in an answer to another question. – user663031 Commented Nov 17, 2014 at 9:24
4 Answers
Reset to default 4You 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 patible 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./Canop/JSON.prune
Take a look at this little library:
https://github./WebReflection/circular-json
It serializes and deserializes otherwise valid JSON objects containing circular references into and from a specialized JSON format.
I remend Flatted. It's tiny and it works very well. The minified version of it weighs 1KB.
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)
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]"
]
}