VideosSupport for Querying and Transforming JSON Documents

Overview

This video shows how to use XPath expressions to query JSON documents, how to transform JSON documents using XSLT, and how to query and transform JSON documents using XQuery.

Transcript

00:00:06This video demonstration will present the support for querying and transforming JSON
00:00:12documents in Oxygen after various improvements and new features were implemented in version 21.0.
00:00:19Specifically, I will show you how to use XPath expressions to query JSON documents, how to
00:00:26transform JSON documents using XSLT, and how to query and transform JSON documents using XQuery.
00:00:40There are two ways to use XPath to query a JSON document.
00:00:44You can use the XPath toolbar to quickly query JSON documents...
00:00:49and you can also use the dedicated XPath Builder view where you can compose more complex XPath
00:00:56expressions and execute them over JSON documents.
00:00:59To demonstrate these features, I have a sample file that contains information about XML-related books.
00:01:07Suppose that I want to obtain the titles of all the books listed in my document.
00:01:12To achieve this, I'll type an XPath expression in the XPath toolbar.
00:01:17Notice that the Content Completion Assistant presents the property names and XPath functions
00:01:24that can be inserted in the current context, making it easier to compose the XPath expression.
00:01:31After pressing the Enter key, the matches are presented in the results panel at the
00:01:36bottom of the editor and also highlighted directly in the document.
00:01:41Clicking a result will take you to its corresponding location in the document.
00:01:46I can use the "Copy XPath" action from the contextual menu to copy the XPath expression
00:01:52of the current property to the clipboard.
00:01:55And I'll paste it in the XPath toolbar.
00:01:58I want my expression to find a list of author for all books in my document, so I need to
00:02:05delete the book index in the expression.
00:02:10To automatically generate the XPath expression that corresponds to the property at the cursor
00:02:15location, you can select "XPath update on cursor move" from the settings drop-down menu.
00:02:24You can run the expression to find all similar results in the document.
00:02:36To execute more complex XPath expressions, you can use the XPath Builder view.
00:02:44If this view is not already open...
00:02:47...the easiest way to open it is to select "Switch to XPath Builder view" from the
00:02:53settings drop-down menu on the XPath toolbar.
00:02:58As an example, suppose that I want to find all books listed under the XML category.
00:03:04I'll type the expression in the editing pane in the builder view.
00:03:11Then I'll click the Run button to execute the expression.
00:03:16You can execute XPath expressions over multiple JSON files, both from XPath toolbar
00:03:23and XPath Builder view.
00:03:25To show this, I'll run an XPath expression over multiple files in my current project.
00:03:31So, in the Project view, I'll select the files, right-click, and choose "XPath in Files".
00:03:40This will automatically switch the active view to the XPath Builder and set the scope
00:03:46to the selected project resources.
00:03:49Note that there are other types of scopes you can choose or you can even define a custom one.
00:03:58I'll slightly modify the XPath expression to get more results and I'll execute it.
00:04:09If I double-click a result, it will open the particular file that contains the match.
00:04:23Oxygen also includes support for using transformation scenarios to transform JSON documents to various formats.
00:04:30As an example, I'll transform my JSON document that contains the list of books to an HTML file.
00:04:37To do this, I will use a file template that comes bundled with Oxygen called XSLT Stylesheet
00:04:43for JSON.
00:04:54This default XSLT template processes the JSON document and produces the output based on
00:05:00its content.
00:05:02So, I'll create an XSLT transformation scenario ... and I'll specify my JSON file as the value
00:05:11of the input parameter.
00:05:24I'll choose to save the content as an HTML file and I want it to be opened in my default
00:05:30browser after the transformation is finished.
00:05:34I'll apply the transformation.
00:05:38You can see that the output contains a lot of data, and because it is unformatted, it
00:05:42is difficult to read.
00:05:45So, I'll adjust the XSL file to format the output better.
00:05:50I'll add a document title, and I want to list the books in a table format, and to keep things
00:05:57simple, I will choose to only list the title and author for every book.
00:06:02I'll transform it again... ...and it looks much better.
00:06:10Now I'll add even more information such as images, descriptions, and various other things.
00:06:16I'll transform it again...
00:06:19Now I am happy with the current form of the HTML output.
00:06:30The last thing I want to show you is how to query and transform JSON documents using XQuery.
00:06:37I already created an XQuery file that parses the JSON document and extracts the XML-related books.
00:06:46The output is a JSON document that contains the name and author of the selected books.
00:06:51To apply this XQuery, I need to create an XQuery transformation scenario, specify my
00:06:57source JSON file as the value of the document-uri parameter...
00:07:13I'll choose to save the content as a JSON file and this time I want it to be opened
00:07:19in Oxygen after the transformation finishes, so I'll choose "Open in Editor"
00:07:27And this is what the result looks like.
00:07:36A query can also be run from the XPath/XQuery Builder view.
00:07:45To do so, I first need to select a suitable XQuery processor, such as the Saxon-PE XQuery engine.
00:07:52Now I'll paste an XQuery construct that will parse my JSON file, extract the XSL-related
00:08:00book information, then output it as an XML sequence.
00:08:05I'll execute the XQuery and the output is displayed in the results panel.
00:08:16This concludes the demonstration, as always, thanks for watching.