DefiantJS enables you to perform lightning-fast searches on JSON using XPath expressions, and transform JSON using XSL.View Source on GitHub Download DefiantJS
Do you need to query large JSON structures? Do you end up coding loops to parse the JSON and identify the data that matches your query? DefiantJS offers a better way. DefiantJS extends the global JSON object with a "search" method that enables lightning-fast searches using XPath expressions. Try out the XPath Evaluator below to get the idea.
DefiantJS 1.2.0 introduced a snapshot feature that improves search performance more than 100 times compared to a regular search. DefiantJS prepares the JSON structure before the search; this preparation 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 (see the example).
With really large JSON structures, avoiding to do the time-consuming preparation on each search has proven to be beneficial. Though it turned out that this preparation have a blocking effect on the UI-thread. Consequently in version 1.2.6 a snapshot can be created utilizing web workers. This approach brings a smoother experience and applies especially when dealing with large data - larger than 1 MB of JSON data.
With DefiantJS, you can write logical templates with powerful technologies such as XSLT & XPath, and apply them to 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 DefiantJS joins XML and
related technologies with JSON.
Most likely, Douglas Crockford will hate this unholy matrimony.
Using DefiantJS 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 demonstrates calling templates from
another template. It contains recursive template calling and renders a fictitious filesystem structure. When the "tree-walker"
template calls itself, indentation is passed as an 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
A 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 a similar feature for working with JSON has prompted initiatives such as JSONPath, which has not been standardized or widely accepted by the community. With DefiantJS, you can unleash the power of XPath on JSON objects, with no retooling or reschooling!
The syntax table below offers a glimpse 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 and how-to's can easily be found and 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 extensions 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.
A couple of suggestions:
- Switch views from XML to JSON to get a better overview of the data structure and how the selections are made.
- Also, check out the browser console for hints.
- To get going, click any of the pre-written examples of XPath queries.
- Don't forget to click the "Edit" button. You can alter or paste in your own JSON data and try out different XPath expressions on your custom data. This way, you can validate your expressions visually and get instant feedback.
There are some internal procedures of DefiantJS 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.
- The structure of object that contains attributes keys (starting with "@"), will be altered and attributes precedes regular regular elements in the object structure.
- Namespace selection is not possible, since JSON does not support such detailed data semantics (as opposed to XML).
- Searching '//*' in JSON will result in one less match (comparing same search with XML), since XML has a root node and the handle to that match is "reachable". JSON's root handle is the name of the objects variable name and is out of scope for the search algorithm. Hence, a handle to the variable name can't be passed back to the querist.
- DefiantJS transforms JSON to XML, which is done lossless. This means that everything from the source data structure can be interpreted back. Transforming XML to JSON is not lossless since JSON isn't built to match the same level of data semantics as XML.
Besides smart templating with the ability to perform transformations using XSLT on XML, & JSON structures, DefiantJS is equiped with powerful methods for refined data selections and generic tools to analyze & manipulate these structures programmatically, implemented cross-browser.
Inline script-block(s) delivers XSLT templates to the browser and can contain multiple templates. Pass the name of the template that will be used on the JSON object - passed in as second argument. This function returns an HTML string of the transformation.
This function does exactly what its name indicates; it creates and returns an XML document from the string passed in as argument. This function is primarily used internally by DefiantJS.
This function does exactly what its name indicates; transforms a node and its descending structure into JSON equivalent.
There are different ways of dealing with XPath in Internet Explorer & other browsers. This function is not supported by all browsers by default but the syntax is much clearner & simpler than
xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE, null);and therefore implemented cross browser with DefiantJS.
This function behaves almost the same way as
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 choice performance-wise.
This property is a good helper when writing & debugging XML centric code. It's a shorthand way to get the serialized representation of a node, as opposed to creating a new
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).
With DefiantJS, it is trivial to convert your JSON object into XML structure. Just call this extended function of the global object JSON and pass in your object as argument & the function will return an XML document.
JSON.search(object, xpath, first)
This function is the jewel of DefiantJS. Call this function whenever you want a collection of elements in a JSON object, matching the XPath query, given as second argument. The third argument is optional and is of
booleantype; if set to
true, the function exits the search algorithm, as soon as a matching element is found, based upon given XPath.
JSON.searchreturns an array with aggregate functions, populated with matching elements. Besides the ability to find elements with XPath, this function returns mutable objects (assuming that the matches are mutable) of 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.