JSON

Developing the next generation of open data interchange

« PreviousNext »

Defining JSON Referencing Convention with JSON Schema

22 January 2008

I have been involved in lengthy discussions on the best approach to JSON referencing. I have long wished for a single convention for JSON referencing, but the numerous approaches currently in use have frustrated my attempts at consensus. In some situations path-based referencing can be a very clean approach for intra-message referencing. On the otherhand, id-based referencing can be preferential, especially when objects already have unique identifiers and/or when referencing outside the local JSON message is necessary, but settling on a convention for referencing is difficult. With id-based referencing one of the sticky points is defining the identity property. I have long argued for always using “id” as the identity property, but others have felt that this may result in the id property may not necessarily be globally unique. Others use an identifier property in the root of objects that defines the identity property for the rest of the object. This allows for flexibility of defining alternate identity properties, but unfortunately mixes meta data right into the data payload.

Perhaps rather than requiring a single agreed upon identity property for interoperable JSON referencing, it may be more reasonable to allow JSON providers to define their own identity and referencing property with a JSON Schema. JSON Schema currently includes a “unique” attribute that is used to define a property as being unique. A JSON Schema can easily declare the identity property by defining a single unique property in the schema. Ids can also be declared as URLs with relative resolution that has the broadly powerful capabilities of not only intra-message referencing, but also cross-site referencing.

Agreeing upon a single referencing property (I have proposed “$ref”) is less problematic, the “$ref” is unlikely to clash with existing properties, and there is little chance for property reuse like with ids. Nonetheless, it would be possible to add a special attribute to JSON Schema to denote a referencing property. This attribute could also signal what forms of referencing to expect (path-based, id-based, or both). An example JSON Schema that defines referencing capabilities might look like:

PersonSchema=
{"type":
   {"person_id":{"unique":"true","type":"string","format":"url"},
    "name":{"type":"string"}},
 "referenceProperty":"$ref",
 "referencingForm":"both"}

While I still like the ease and efficiency of a single JSON referencing convention, the flexibility of defining the id and referencing properties with JSON Schema may be reasonable approach to the future of JSON referencing. What do you think?

Posted in JSON Schema, Referencing | Trackback | del.icio.us | Top Of Page

    One Response to “Defining JSON Referencing Convention with JSON Schema”

  1. Jakob Kruse Says:

    I think that referencing is definitely one thing we should build some consensus about. JSON Schema is another. I generally like your JSON Schema proposal a lot, however I find it to be problematic in certain areas. Please have a look at my modified proposal at http://www.kruse-net.dk/json-schema/ and tell me what you think.

Leave a Reply