Defiant.js provides the ability for you to perform lightning fast searches on JSON structures using XPath expressions and enables XSL transformations using JSON data.Download defiant.js
Do you encounter projects with JSON data and you need to query it...? Do you end up writing custom loops to single out selections that match your requirements? You don't need to do so anymore. DefiantJS extends the global object JSON with the method "search" and enables lightning fast searches using XPath expressions. Try out the XPath Evaluator below to get the idea.
As of version 1.2.0 the snapshot feature was added which improves the performance more than 100 times compared to "regular search". Defiant.js prepares the JSON strucutre before the actual search and this preperation is the time consuming part. Using snapshot of the JSON structure, DefiantJS can find and return matches as fast as 4ms on 1.6MB large data (examplified here).
With defiant.js, you can write logical templates with powerful technologies such as XSLT & XPath, applicable on JSON objects. Besides the fact that XSLT is a proven Turing-complete language, it is also standardized and supported by all major browsers.
You could say that defiant.js joins XML and
related technologies with JSON.
Most likely, Douglas Crockford will hate this unholy matrimony.
Using defiant.js is pretty easy. Check out the examples below to get a hint of how it can
be used; the first one is rather simple and the latter is more advanced and exemplifies calling templates from
another template. It contains recursive template calling and renders a fictitious filesystem structure. When "tree-walker"
template is calling itself, indentation is passed as argument, thus indenting child elements of a folder.
To see the code below in action, here are the demo files; simple example and advanced example
One powerful tool when dealing with XML is the advantage of using XPath. XPath is a standardized language for addressing parts of an XML document and enables features such as analysis, transformation & refined data selection out of an XML structure. The lack of this tool when dealing with JSON has conveyed in initiatives with diverse prosperities and introduced suggestion of JSONPath, that has not been standardized (nor wideley absorbed by the community).
By using defiant.js, the power of XPath can be
unleashed on JSON objects, without the need of a new set
of expressions & syntax, as has been with JSONPath.
No re-schooling is required :-)
The syntax table below is a incision of the power that XPath brings. XPath has a lot more to offer. An important advantage of implementing XPath, as contrasted with JSONPath, is that this language has been documented a great deal on the web and information & how-to's can easily be found & digested.
|/||Child operator; selects immediate children of the left-side collection. When this path operator appears at the start of the pattern, it indicates that children should be selected from the root node.|
|//||Recursive descent; searches for the specified element at any depth. When this path operator appears at the start of the pattern, it indicates recursive descent from the root node.|
|.||Indicates the current context.|
|..||The parent of the current context.|
|*||Wildcard; selects all elements regardless of the element name.|
|@||Attribute; prefix for an attribute name.|
|@*||Attribute wildcard; selects all attributes regardless of name.|
|||Subscript operator; applies a filter pattern OR used for indexing within a collection.|
||||Union operator; results in a combination of element sets|
|()||Groups operations to explicitly establish precedence|
In order to access XPath functionality easily, some global objects has been
extended with useful methods, for instance;
Detailed information about these extension can be found in the Function reference section.
Not familiar with XPath? Don't worry, use the XPath evaluator below and you will become an expert in no time.
There are some internal procedures of defiant.js as well as known limitations of JSON (compared to XML) that you should be aware of. Below are some knowledge to keep in mind when working with XSLT, XPath and JSON.
Besides smart templating with the ability to perform transformations using XSLT on XML, & JSON structures, defiant.js is equiped with powerful methods for refined data selections and generic tools to analyze & manipulate these structures programmatically, implemented cross-browser.
xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE, null);and therefore implemented cross browser with defiant.js.
selectNodeswith the difference that it returns the first match of the XPath expression and exits the search algorithm as soon as the first match is found. It is a preferable alternative if a single node is known to be desired, hence better choise performance-wise.
XMLSerializerand call its function
serializeToString. This property outputs a human-readable version of the serialized XML string, i.e. with line breaks and tabbed rows. Lastly, this property is accessible from all element nodes, regardless of depth in the XML structure (the document node included).
JSON.search(object, xpath, first)
booleantype; if set to
true, the function exists the search algorithm, as soon as a matching element is found, based upon given XPath. The function
JSON.searchreturns an array with agregate functions, populated with matching elements. Besides the ability find elements with XPath, this function returns references to the matching elements in the JSON structure. Put differently; the matched elements can be altered with a loop, thus altering the original JSON object. See the code below as example.
powered by defiant.js