Altova XMLSpy 2025 Enterprise Edition

JSON Transformations with XSLT/XQuery

Home Prev Top Next

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 below. For information about constructing XQuery expressions for JSON documents, see the section XQuery Expressions for JSON.

 

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\XMLSpy2025\Examples\Examples.spp.

 

Querying a JSON document via the XPath/XQuery 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.

 

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.

 

© 2018-2024 Altova GmbH