JSON Transformations with XSLT/XQuery

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  User Guide and Reference > JSON, JSON Schema >

JSON Transformations with XSLT/XQuery

JSON maps, arrays, and objects can be targeted with XPath/XQuery 3.1 expressions. As a result, JSON documents can be transformed with XSLT 3.0, XQuery 3.1, and XQuery Update 3.0 documents by using the built-in engines of XMLSpy.

 

The following functionality is available:

 

An active JSON document can be queried with XPath/XQuery 3.1 expressions from the XPath/XQuery output window
An active JSON document can be transformed with a user-selected XSLT or XQuery file
An active XSLT or XQuery document can be executed on a user-selected JSON source file

 

These features are described below in more detail.

 

Note:        You can try out JSON transformations by using the JSON, XSLT, and XQuery files in the JSON Examples folder of the Examples project located in your application folder: C:\Documents and Settings\<username>\My Documents\Altova\XMLSpy2019\Examples.

 

Querying a JSON document via the XPath/XQuery output window

JSON documents can be queried by entering an XPath/XQuery 3.1 query expression in the XPath/XQuery output window (see screenshot below). Select either the XPath 3.1 icon or XQuery 3.1 icon, and ensure that the window is in JSON evaluation mode (explained below).

XPathXQueryWindow4JSON01

The information given below pertains to evaluations of JSON documents in JSON evaluation mode. (For an overview of the XPath/XQuery window and detailed information about its usage, see the section Output Window: XPath/XQuery.)

 

JSON evaluation mode

JSON evaluation mode is described through these points:

 

The XPath/XQuery window will be in either XML evaluation mode or JSON evaluation mode. Which mode is currently active is indicated by the active mode's button being highlighted. See the XML/JSON evaluation mode buttons in the screenshot above. In the screenshot, the window is in JSON evaluation mode.
In the screenshot above, notice that the XML and JSON buttons are grayed out, indicating that they are disabled. When the buttons are disabled, their status—whether activated or deactivated—cannot be changed. Conversely, if the buttons are enabled (not grayed out), then the evaluation mode of the window can be changed.
The enabled/disabled state of the XML/JSON evaluation mode buttons depends on the evaluation scope (the value of the Where field; see screenshot above). Evaluation-scope values are divided into two groups for the determination of the enabled/disabled state: (i) Single file (Current file), and (ii) Multiple files (Open files, Project, Folder).
If, for the evaluation scope, a single file (Current file) is selected (as in the screenshot above), then the window's mode (JSON or XML) is determined on the basis of the file's extension. Either the file is JSON conformant, in which case JSON evaluation mode is activated; or the file is not JSON conformant, and XML evaluation mode is switched on. Since the file type of the single file is known, the appropriate evaluation mode is activated, and both buttons are disabled so that the mode cannot be changed.
If a multiple-files option (Open files, Project, Folder) is selected, then both evaluation mode buttons are enabled, and the user can select what mode to activate (JSON or XML). The default evaluation mode for a multiple-file scope is XML.
In XML evaluation mode, XML conformant files will be processed and JSON files will be skipped.
In JSON evaluation mode, JSON conformant files will be processed and XML files will be skipped.
JSON expressions can also be queried in Debug Mode.

 

The XPath/XQuery expression

In XPath/XQuery expressions, use features that are valid for querying JSON documents, such as the lookup operator and array functions. Note the following points:

 

The Set current selection in document as origin feature is disabled. As a result, expressions must be absolute, and cannot be relative.
As you enter the expression, auto-completion provides context-sensitive completion options (see screenshot below).

XPXQWindowJSONAutoComplete

The Copy XPath of current selection to edit field icon creates an XQuery expression that targets the selection in the active JSON document and copies the expression to the window's edit field. To make a selection in the JSON document, place the cursor within an object or select a part of an object.

 

Given below are two XPath/XQuery expressions that have been used to query the JSON document MusicLibrary.json (see screenshot below), which you can find in the Examples project located in your application folder: C:\Documents and Settings\<username>\My Documents\Altova\XMLSpy2019\Examples.

Click to expand/collapse

 

XPath/XQuery expression: ?Artists?1?Albums?2?Name

XPXQWindowJSON01

XPath/XQuery expression: ?*

XPXQWindowJSON02

 

Results

Results show the JSON component that is selected by the XPath/XQuery expression in the left side of the Results pane, and the component's value in the right side of the pane. Maps and arrays are displayed in short or verbose format according to whether the Show complete result icon in the toolbar is toggled off or on. Maps and arrays in the left side of the pane can be expanded by clicking the plus icon. The values of the expanded components will then be displayed in the right side of the pane.

 

Transforming a JSON document with XSLT/XQuery

To transform an active JSON document with an XSLT 3.0, XQuery 3.1, or XQuery Update 3.0 document, do the following:

 

XSLT 3.0 transformation: Click XSL/XQuery | XSL Transformation, browse for the XSLT 3.0 file, and click OK.
XQuery 3.1 or XQuery Update 3.0 transformation: Click XSL/XQuery | XQuery/Update Execution, browse for the XQuery 3.1 or XQuery Update 3.0 file, and click OK.

 

The transformed document/s will be generated, and can be viewed directly in XMLSpy.

 

Note:XSLT/XQuery Debugger can be started from a JSON document, but breakpoints and tracepoints can be set in the XSLT or XQuery document only.

 

Providing a JSON source for an XSLT/XQuery document

To execute an active XSLT or XQuery document on a JSON source file, do the following:

 

Active XSLT 3.0 document: Click XSL/XQuery | XSL Transformation, browse for the JSON file, and click OK.
Active XQuery 3.1 or XQuery Update 3.0 document: Click XSL/XQuery | XQuery/Update Execution, browse for the JSON file, and click OK.

 

The transformed document/s will be generated, and can be viewed directly in XMLSpy.

 

Note:XSLT/XQuery Debugger can be started from an XSLT or XQuery document and a JSON document can be assigned as input for the debugging session. However, breakpoints and tracepoints can be set in the XSLT or XQuery document only.

 


© 2019 Altova GmbH