How to parse json string contains circular reference in javascript?

I have the following json string in javascript. This line contains circular references. I want to parse this string so that the link will be replaced by its actual object. I use Json.Parse , but it creates a json object with links. Is there any way I can achieve this?

 { "$id": "1", "$values": [ { "$id": "2", "Event": { "$id": "3", "Invitaions": { "$id": "4", "$values": [ { "$ref": "2" }, { "$id": "5", "Event": { "$ref": "3" }, "Id": 2, "Name": "test2", "Date": "24", "EventId": 1 } ] }, "Id": 1, "Name": "marriage", "Address": "abcd" }, "Id": 1, "Name": "test1", "Date": "23", "EventId": 1 }, { "$ref": "5" }, { "$id": "6", "Event": { "$id": "7", "Invitaions": { "$id": "8", "$values": [ { "$ref": "6" } ] }, "Id": 2, "Name": "birthday", "Address": "abcd" }, "Id": 3, "Name": "test3", "Date": "25", "EventId": 2 } ] } 
+4
source share
3 answers

This should do it:

 function resolveReferences(json) { if (typeof json === 'string') json = JSON.parse(json); var byid = {}, // all objects by id refs = []; // references to objects that could not be resolved json = (function recurse(obj, prop, parent) { if (typeof obj !== 'object' || !obj) // a primitive value return obj; if ("$ref" in obj) { // a reference var ref = obj.$ref; if (ref in byid) return byid[ref]; // else we have to make it lazy: refs.push([parent, prop, ref]); return; } else if ("$id" in obj) { var id = obj.$id; delete obj.$id; if ("$values" in obj) // an array obj = obj.$values.map(recurse); else // a plain object for (var prop in obj) obj[prop] = recurse(obj[prop], prop, obj) byid[id] = obj; } return obj; })(json); // run it! for (var i=0; i<refs.length; i++) { // resolve previously unknown references var ref = refs[i]; ref[0][ref[1]] = byid[refs[2]]; // Notice that this throws if you put in a reference at top-level } return json; } 
+3
source

You should check out Douglas Crockfords JSON-js repo on github: https://github.com/douglascrockford/JSON-js

There is a cycle.js that helps you do exactly what you are looking for.

+1
source

Look at my post here, I found some errors in the above code and there was no support for arrays, check out my improved version: Allow circular references from a JSON object

0
source

All Articles