Translating JSON to XML

Lots of web services return JSON data, and we might run into a situation where we want to feed some JSON data into existing code that accepts only XML. Let’s use our JSON grammar from ​Parsing JSON​ as a foundation to build a JSON to XML translator. Our goal is to read in JSON text like this:

​ {
​  ​"description"​ ​:​ ​"An imaginary server config file"​,
​  ​"logs"​ ​:​ {​"level"​​:​​"verbose"​, ​"dir"​​:​​"/var/log"​},
​  ​"host"​ ​:​ ​"antlr.org"​,
​  ​"admin"​​:​ [​"parrt"​, ​"tombu"​],
​  ​"aliases"​​:​ []
​ }

and emit XML in an equivalent form, like this:

​ <description>An imaginary server config ...

Get The Definitive ANTLR 4 Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.