Skip to content

Instantly share code, notes, and snippets.

@rolaveric
Created March 28, 2015 02:04
Show Gist options
  • Save rolaveric/a2b04376f96412b1d0e8 to your computer and use it in GitHub Desktop.
Save rolaveric/a2b04376f96412b1d0e8 to your computer and use it in GitHub Desktop.
Example of a relationship definition for JSON Schema design
/*
For this schema, each object's relationships are defined as an object under
a "links" property.
Each property of that object is the name of the relationship, and the value
is a single object with the "type" and "id" of the related object.
For "to-many" relationships, the value can be an array of the same objects.
*/
var data = {
"articles": [{
"id": "article1",
"links": {
"author": {"id": "person1", "type": "people"}
}
}],
"people": [{
"id": "person1",
"phoneNumber": "1234"
}]
};
function getLink(data, object, linkName) {
var link = object.links[linkName];
if (link && data[link.type]) {
return data[link.type].find(function(e) {return e.id === link.type;});
}
}
getLink(data, data.articles[0], 'author').phoneNumber; // "1234"
@sheriffderek
Copy link

I want a link to an awesome JSON blog post...

@janpaul123
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment