XSLT - JSON Processing with XSLT 3.0

JSON (JavaScript Object Notation) has become one of the most widely used formats for exchanging data between web applications, APIs, cloud services, and mobile applications. While XSLT was originally designed to transform XML documents, XSLT 3.0 introduced built-in support for processing JSON. This enhancement allows developers to read JSON data, convert it into XML-compatible structures, manipulate it using XSLT templates and functions, and even generate JSON as output. As a result, XSLT is no longer limited to XML transformations and can participate in modern data integration workflows.

What is JSON?

JSON is a lightweight, text-based format used to store and exchange structured data. It represents information using key-value pairs and arrays, making it easier to read and write than XML in many web applications.

Example of a JSON document:

{
  "student": {
    "id": 101,
    "name": "Rahul",
    "course": "Computer Science",
    "marks": [85, 90, 88]
  }
}

Unlike XML, JSON does not use opening and closing tags. Instead, it relies on braces {} for objects and square brackets [] for arrays.

Why JSON Support Was Added to XSLT

Modern applications increasingly communicate using REST APIs, which usually return JSON instead of XML. Before XSLT 3.0, developers had to convert JSON into XML using external programs before applying XSLT transformations. XSLT 3.0 eliminates this dependency by providing native functions to read and write JSON.

Benefits include:

  • Direct processing of JSON documents.

  • Reduced dependency on external conversion tools.

  • Easier integration with RESTful web services.

  • Improved support for cloud-based applications.

  • Simplified transformation workflows.

JSON Data Model in XSLT

When JSON is processed in XSLT 3.0, it is converted into an internal data model consisting of:

  • Maps

  • Arrays

  • Strings

  • Numbers

  • Booleans

  • Null values

This data model allows XSLT to access JSON elements similarly to how it accesses XML nodes.

For example:

JSON:

{
  "name": "Alice",
  "age": 22
}

Internally becomes a map where:

  • name → "Alice"

  • age → 22

Reading JSON Using parse-json()

The parse-json() function converts JSON text into an XSLT data structure.

Syntax:

parse-json($json-string)

Example:

<xsl:variable name="json">
{
  "city":"Mysore",
  "state":"Karnataka"
}
</xsl:variable>

<xsl:variable name="data"
              select="parse-json($json)"/>

The variable $data now stores a map containing the JSON information.

Accessing JSON Values

Once JSON has been parsed, values can be retrieved using the lookup operator.

Example:

$data?city

Output:

Mysore

Access another value:

$data?state

Output:

Karnataka

This syntax is much simpler than writing complex XPath expressions.

Processing Nested JSON Objects

JSON often contains objects inside other objects.

Example:

{
  "employee": {
    "id": 501,
    "department": "Sales",
    "salary": 50000
  }
}

Accessing nested values:

$data?employee?id

Output:

501

Similarly,

$data?employee?department

returns

Sales

Working with JSON Arrays

JSON arrays contain multiple values.

Example:

{
  "subjects":
  [
    "Math",
    "Science",
    "English"
  ]
}

After parsing:

$data?subjects

returns an array.

Individual elements can be accessed by position.

Example:

$data?subjects?1

returns

Math

Similarly,

$data?subjects?2

returns

Science

Arrays can also be iterated using loops.

Example:

<xsl:for-each select="$data?subjects?*">
    <subject>
        <xsl:value-of select="."/>
    </subject>
</xsl:for-each>

Output:

<subject>Math</subject>
<subject>Science</subject>
<subject>English</subject>

Converting XML into JSON

XSLT 3.0 provides the xml-to-json() function.

Suppose the XML document is:

<person>
    <name>John</name>
    <age>30</age>
</person>

Using:

xml-to-json($xml)

Possible output:

{
  "name":"John",
  "age":30
}

This feature is valuable when XML-based systems need to exchange information with JSON-based applications.

Converting JSON into XML

The json-to-xml() function converts JSON into an XML representation.

Example:

json-to-xml('{"book":"XML Guide"}')

Produces XML similar to:

<map>
    <string key="book">XML Guide</string>
</map>

This XML representation can then be transformed using standard XSLT templates and XPath expressions.

Generating JSON Output

An XSLT stylesheet can produce JSON directly.

Example:

<xsl:output method="json"/>

Example result:

{
  "student":"Amit",
  "grade":"A"
}

This allows XSLT to serve as both a transformation engine and a JSON generator.

Processing JSON from REST APIs

Many REST APIs return JSON responses.

Example API response:

{
  "temperature":29,
  "humidity":65,
  "city":"Bangalore"
}

Using XSLT:

  • Parse the response.

  • Extract required values.

  • Generate reports.

  • Convert the data into XML if necessary.

  • Produce HTML pages displaying the information.

This makes XSLT useful for integrating XML-based systems with modern web services.

Handling Boolean and Null Values

JSON supports Boolean values.

Example:

{
  "active": true,
  "verified": false
}

After parsing:

$data?active

returns

true

JSON also supports null.

Example:

{
  "middleName": null
}

XSLT correctly recognizes null values, allowing developers to test for missing or optional data.

Error Handling

Invalid JSON causes parsing errors.

Example of invalid JSON:

{
"name":"John",
"age":
}

To avoid failures, XSLT can use xsl:try and xsl:catch.

Example:

<xsl:try>
    <xsl:variable name="data"
                  select="parse-json($json)"/>
    <xsl:catch>
        Invalid JSON detected.
    </xsl:catch>
</xsl:try>

This improves the robustness of applications by allowing graceful handling of malformed input.

Performance Considerations

When processing large JSON files:

  • Parse the document only once and reuse the resulting data structure.

  • Avoid repeatedly accessing the same nested values.

  • Store frequently used values in variables.

  • Process arrays efficiently using iteration.

  • Minimize unnecessary conversions between XML and JSON.

These practices help reduce execution time and memory usage.

Practical Applications

JSON processing in XSLT 3.0 is useful in many real-world scenarios, including:

  • Transforming REST API responses into HTML reports.

  • Converting XML databases into JSON for web applications.

  • Integrating legacy XML systems with modern cloud services.

  • Processing configuration files stored in JSON format.

  • Generating JSON responses for web services.

  • Creating dashboards using data from JSON-based APIs.

  • Migrating data between XML and JSON formats.

  • Building enterprise integration solutions that support both XML and JSON.

Advantages of JSON Processing in XSLT 3.0

  • Eliminates the need for external JSON conversion tools.

  • Supports modern web application development.

  • Simplifies integration with RESTful APIs.

  • Provides built-in functions for parsing and serialization.

  • Supports complex nested objects and arrays.

  • Enables direct JSON output from XSLT transformations.

  • Reduces development effort for data conversion tasks.

  • Improves compatibility between XML-based and JSON-based systems.

Summary

JSON processing in XSLT 3.0 significantly expands the capabilities of the language by allowing it to work seamlessly with both XML and JSON data. Through functions such as parse-json(), json-to-xml(), and xml-to-json(), developers can parse, transform, and generate JSON without relying on external tools. Support for maps, arrays, nested objects, Boolean values, and error handling makes XSLT 3.0 a versatile solution for modern data transformation tasks. This capability is especially valuable for integrating legacy XML systems with contemporary web applications, REST APIs, cloud platforms, and enterprise data exchange environments.