Finding JSON Links - GistLabs/mechanize GitHub Wiki

Finding hyperlinks in JSON

JSON is a very flexible data format, but a few good heuristics can be used to find the sources of hyperlinks in the content. The aim is to be able to us CSS Selectors for JSON to find the important JSON objects (like the domain objects) and then find the hypermedia defined on and under those objects.

We want to find the hypermedia in a flexible way, so that even if the exact structure of the data changes we still able to find the correct links.

The JsonLinkFinder.java implementation contains the logic to find and build JsonLinks from JSON.

How does it work?

The JsonLinkFinder works by inspecting the attribute names of JSON objects and matching patterns to look for URIs defining names. A very simple example is:

{
	"a": { "b": "1", "c": "2" },
	"link": { "href": "http://example.com", "data": "value" }
}

The JSON object identified by "link" will define a single JSONLink.

A single JSON object can define multiple Links, for example:

{
	"href": "http://example.com/item/42",
	"rel": "self",
	"children-link": "http://example.com/item/42/children",
	"history_href": "http://example.com/item/42/history"
}

This JSON object would define three links, with three different meanings (or relationships).

  • a self link, with URI http://example.com/item/42
  • a children link, with URI http://example.com/item/42/children
  • and a history link, with URI http://example.com/item/42/history

More examples

See the [find-links.json](https://github.com/GistLabs/mechanize/blob/master/src/test/resources/com/gistlabs/mechanize/document/json/hypermedia/find-links.json] file for more examples, along with the JsonLinkFinderTest.java test class.