Skip to main content
3 of 7
added 492 characters in body
Cameron McKay
  • 316
  • 1
  • 2
  • 8

How do I resolve $ref in a JSON object?

I have written a single page application that uses rest services to retrieve JSON objects. The JSON objects being returned are C# objects serialized using the Newtonsoft.JSON library. The returned JSON object contains $ref pointers to specific $id objects within the same JSON object (The question here shows an example). I need to access the actual data that the $ref points to, but I do not know how to access it without locating the original source.

I've found solutions such as writing a new contract serializer and writing my own implementation of resolving the references. I feel like there should be an easier solution than these two. I've also explored the Newtonsoft JSON serialization settings; I've tried setting PreserveReferenceHandling to None, but I haven't had any success there.

I'm looking for solutions other than the ones I've mentioned. I'm hoping there's a simple solution that requires very little code to implement and no additional frameworks.

UPDATE

I've been reading some more about this and discovered that $ref is a JSON Pointer fragment. The reference number is an ID that references an item from a different schema. So, I need to know the schema ahead of time to use this. Which I do know, but if the object being serialized changes, then I wonder if the code will break because it doesn't match the schema.

Info about JSON Pointer Fragments

References:
Resolve circular JSON references
Serialization Settings

Cameron McKay
  • 316
  • 1
  • 2
  • 8