Click here to Skip to main content
13,450,884 members (54,552 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 12 Mar 2013

Solving the problem of Circular References in JSON

, 12 Mar 2013
Rate this:
Please Sign up or sign in to vote.
The problem of circular reference is a common problem in JSON.

What is the problem of circular references in JSON? 

This problem occurs when you have entities that are interdependent. For example, a Book entity that contains a single or a collection of authors. The Author entity contains also a single or a collection of books. It's as A -> B -> A. Because JSON, like XML, is tree based, it can't deal with that and will throw an exception. 

I faced the problem of circular reference when I started using JSON. I searched the web, there were a lot of solutions for that. But they are not accepted in many cases. Because you will have to change a lot in your Models by adding annotations. Some other solutions will make your application slower, as they suggest to delete the virtual keyword used for lazy loading.

The solution 

What I suggest is to just remove the circular references, but how? You just set a single or collection of the entity Book in the entity Author. But you set the single or collection of the entity Author within the Book to Null. Then you will have (A -> B -> Null), so there are no more circular references!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Microsoft
Tunisia Tunisia
I'm a Software Engineer and MVP (Client Development). I like writing articles and developing open source software in C#, Windows Phone and Windows 8 applications

You may also be interested in...


Comments and Discussions

QuestionI agree with the previous comment Pin
Assil22-Oct-14 0:27
professionalAssil22-Oct-14 0:27 
GeneralMy vote of 1 Pin
ashved12-Mar-13 15:15
memberashved12-Mar-13 15:15 
GeneralRe: My vote of 1 Pin
Houssem_Dellai12-Mar-13 23:03
memberHoussem_Dellai12-Mar-13 23:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180318.3 | Last Updated 13 Mar 2013
Article Copyright 2013 by Houssem_Dellai
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid