How to Convert JSON to XML using XSLT 2.0

XSLT 2.0 is powerful enough to process even non-XML input. For example, I have created a transformation that converts JSON text to well structured XML output:

JSON text:

     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address": {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     "phoneNumber": [
         { "type": "home", "number": "212 555-1234" },
         { "type": "fax", "number": "646 555-4567" }

XML result:

 <?xml version="1.0" encoding="UTF-8"?>
      <field name="firstName">
      <field name="lastName">
      <field name="age">
      <field name="address">
            <field name="streetAddress">
               <string>21 2nd Street</string>
            <field name="city">
               <string>New York</string>
            <field name="state">
            <field name="postalCode">
      <field name="phoneNumber">
               <field name="type">
               <field name="number">
                  <string>212 555-1234</string>
               <field name="type">
               <field name="number">
                  <string>646 555-4567</string>

It works by employing XML Pipeline technique:

  • first mode parses text using regular expressions and generates sequence of tokens in XML format: <comment>, <string>, <number>, <symbol>{</symbol>, etc.
  • second mode groups all tokens between "{" and "}" symbols into <object> element, between "[" and "]" symbols into <array> element
  • third mode makes a <field> element from <string><symbol>:<symbol>(<string>|<number>|<object>|<array>) sequence
  • fourth mode drops comma between consecutive <field> elements

and finally it performs XSD validation check.

This approach is quite exotic and non-standard, because when people hear word "parsing" they think of BNF, state-machine, AST, YACC, etc., but these technologies were created to parse complex programming languages.

I claim that XSLT coupled w/ regexp as tokenizer is powerful enough to convert simple markup languages such as JSON, CSS, MIF, RTF, wiki, etc., into XML.

See attachment below for source code of the transformation.

json2xml.zip5.13 KB


Error with this xslt

I am trying to use this source code, but the .xsl file is not compiled saying

line 18: Unsupported XSL element 'analyze-string'

Can you please help on htis!


The exception is thrown from

The exception is thrown from

    TransformerHandler handler = handlerFactory.newTransformerHandler(new StreamSource(JsonToXml.class.getResource("./json2xml.xsl").toString()));

and the stacktrace is as following:

Compiler warnings:
  line 59: Illegal attribute 'select'.
  line 65: Illegal attribute 'select'.
ERROR:  'line 18: Unsupported XSL element 'analyze-string'.'
FATAL ERROR:  'Could not compile stylesheet'
Exception in thread "main" javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
    at Source)
    at Source)
    at Source)
    at utility.jsontosoap.JsonToXml.json2xml(
    at utility.jsontosoap.JsonToXml.main(

json2xml is XSLT 2.0

json2xml is XSLT 2.0 transform, the stacktrace indicates default TrAX processor (Xalan) is invoked, but Xalan is not XSLT 2.0 processor.

To get json2xml running, you need XSLT 2.0 processor, for example, such as Saxon – check lib/readme.txt:

grab Saxon-HE from unpack saxon9he.jar here

thanks Andriy Gerasika, it

thanks Andriy Gerasika, it worked after getting saxon :)

hello, i wanted to use this

hello, i wanted to use this app.
in the xsl, you are inserting 'array' or 'object' based on conditions (w.r.t. the json.xsd). but i want to use my own run time generated strings instead of such predefined values. is there any way to do this?

also this xsl is generating various empty text nodes after each xml node. how can i get rid of this?


It is all possible

If you want to transform JSON to your own XML model, you should write 2nd XSLT transformation, transforming from my JSON.XSD to required format.

To get rid of empty text nodes, use xsl:output element with indent="no" attribute

JSon element value is null

Hi, If the input JSON has null value then the following error is thrown while transforming JSON to XML. (For Example -= "middleName": null )

Error Message : 
unknown token:  null
error at line: 74 col:27 message: cvc-complex-type.2.4.a: Invalid content was found starting with element 'symbol'. One of '{field}' is expected.

Thanks Siva

fixed & refreshed attachment

fixed & refreshed attachment w/ newer version (it may take 24 hours to update at your location)

new version

if you plan to use json2xml XSLT in real world applications, I recommend using code from my new article How to Convert JSON to XML using ANTLR

Thanks Andriy this is a great

Thanks Andriy this is a great script, just what I needed.

However I've run into a couple of minor issues. JSON numbers in scientific notation (containing an 'E') are not handled correctly, and nor are String values that contain escaped double quotes.

I'd recommend you use

I'd recommend you use json2xml2 from my newer article "How to Convert JSON to XML using ANTLR"

That's a shame

The idea of of doing it in pure XSLT was pretty neat.

but not practical. XSLT was

but not practical. XSLT was created for transforming XML documents, ANTLR for parsing, while it is possible to use XSLT for parsing and ANTLR for transforming, it is better not to do that.

If json2xml isn't working for

If json2xml isn't working for you and you need to do it in XSLT, then you can trying using f:json-document() and f:json-file-document(), which are provided by FXSL instead.

FXSL is a scary monster, I do

FXSL is a scary monster, I do not recommend using it.

Anyways, vox populi est vox dei I have fixed bugs about scientific notation and escape sequences. Use this direct link to download new version (Note: attachment will be auto-refreshed in 24 hours).

Command line example


unfortunately I am not familiar with programming. I can only somewhat XSL and HTML but would still like to use the program. Unfortunately I can not convert with the lastest Saxon the "sample.json" to xml using the included "json2xml.xsl".

Can someone give me a command line example, how can I convert the "sample.json"?

Also, I would appreciate an example of the use of the ANTLR version.

Thanks in advance

How is the xml2json.xsl applied to json-file?

I have downloaded the zip and extracted folders.
I can see there is a java file involved

I normally execute transformation with saxon like this:
java -jar saxon8.jar xml-file.xml xsl-file.xsl

I use result-document function to export results.

I guess that I type "java" in terminal window, but how do I specify input the json-file to be transformed and the xml output?

Thanx for your help!

java -cp

java -cp lib/saxon9he.jar:build/json2xml.jar com.gerixsoft.json2xml.JsonToXml tests/sample.json tests/sample.json.xml


Hi Andriy!

What licence do you offer this software under? I would like to include this in a piece of XML-based software I am developing, which would have an open source licence, but I would need a compatible licence from you, otherwise I would probably just write my own version.

The most permissive one:

The most permissive one: 2-clause BSD.

Concern regarding JSON to XML without changing the order

When I convert JSON data to XML using Java class, it won't maintain the order. I searched a lot but won't get any solution. This is the JSON input. Is there any way to do that.
"status" : "success",
"response" : {
"p_status_msg" : {
"contact" : {
"contactlist" : [{
"address" : "90 Grove St., Newport ME 04953; United States of America",
"grp" : "ADDRESS",
"pidm" : "",
"type" : "Home (preferred)"
}, {
"address" : "",
"grp" : "EMAIL",
"pidm" : "",
"type" : "Academy Email"
}, {
"address" : "",
"grp" : "EMAIL",
"pidm" : "",
"type" : "Email"
}, {
"address" : "(207) 341-3018",
"grp" : "PHONE",
"pidm" : "",
"type" : "Cell Phone"
"contact" : {
"studentid" : "P000020581",
"dateofbirth" : "07/07/1995",
"firstname" : "John",
"socialsecuritynumber" : "005961046",
"gender" : "M",
"middlename" : "Q.",
"lastname" : "Adams"

Can somebody write a java class and take it as a Input JSON and convert it into XML but order should be same.
Thanks in advance

Post new comment

The content of this field is kept private and will not be shown publicly.