2

For a project I'm working on, I need to convert an XML document to JSON, yet keep it human-readable by keeping the data nicely aligned. While xml2json-xslt is able to create JSON, it's not nicely formatted.
My main problem is that it needs to be done in XSLT 1.0 without the use of any external libraries, javascript or whatever else. Just pure XSLT. And while there are many samples on the Internet, I just can't find one that assures a proper (well, readable) format.
It doesn't need to do rocket-science by converting booleans, numbers, dates, etc. It just needs to create JSON, which will be used by another application, yet when operators look at the data, they need to indentation and nicely aligned brackets.

2
  • 1
    Alex: You wrote "It doesn't need to do rocket-science", Yes, but JSON doesn't map XML. That's why there are several conventions for this translation. I think you should pick one of those. Also, there is no clear what you mean by "nice format", and how this is a meaningful problem.
    – user357812
    Jan 24, 2011 at 16:04
  • "Nice format" just means that humans can read it because things are aligned in a proper way. XML nodes can go deep, thus resulting in JSON objects that also go deep. Simple, proper indentation should be enough already. Jan 26, 2011 at 14:08

5 Answers 5

2

The simplest solution might be to push the output of xml2json.xslt through a JSON formatter, of which there seem to be a number available - I don't have experience of any of them.

1
  • If such a JSON formatter would exist as XSLT or if I could call it from XSLT, then problem solved. But such formatters don't exist yet. Jan 26, 2011 at 14:12
1

I think you're going to have to bite the bullet and tweak xml2json-xslt until it indents as you want it to.

See Converting XML to plain text - how should I ignore/handle whitespace in the XSLT? for how to handle whitespace.

I don't think this will be an enjoyable process :(

1
  • That was my conclusion too, in the end. Problem is that it's a low-priority task which is why I was looking for a better solution. Jan 27, 2011 at 10:12
1

In my experience, XSLT is particularly bad for generating nicely formatted output.

Can you punt off the problem onto a JSON viewer that your operators use? A lot of my JSON interaction begins with http://jsonformatter.curiousconcept.com/ ...

Other options: Standalone application, Firefox AddOn, etc

1
  • No, the problem is that I have an existing application that uses an XSLT file to transform data. Thet now want the data to be transformed to JSON instead of XML, but I can't adjust the code of the application. Just the XSLT. Jan 26, 2011 at 14:09
1

Just in case someone else got the same special requirement (producing indented JSON from "simple" XML using XSLT 1.0), I created an indenting version of xml2json-xslt as a gist: https://gist.github.com/3977454

For everyone who is fine with XSLT 2.0 or who has "complex" XML, I'd recommend a different XSLT because xml2json-xslt has some drawbacks: it lacks proper attribute-handling, handling of multiple elements with the same name, etc. (current revision of xml2json-xslt at time of writing: 31).

XSLT alternatives can be found on github when searching for "xml2json xslt" (e.g. https://github.com/bramstein/xsltjson). The drawback is that most of them do not support indentation but this can be compensated by piping the output through a JSON formatter (see, for example, https://stackoverflow.com/a/5244011/490560)

0

http://james.newtonking.com/pages/json-net.aspx

I've only used this library for LINQ to JSON and it was super clean and easy. I was reading a bit in the documentation about converting JSON to .NET objects on the fly and I remember seeing a class for JSON to XML in there...check it out

2
  • by super clean/easy I mean getting a key value from a JSON string using LINQ to JSON with one line of code
    – J Benjamin
    Jan 25, 2011 at 18:39
  • Not what I'm looking for. I need a pure XSLT solution, since all I can adjust is an XSLT file. I definitely cannot use .NET code. Jan 26, 2011 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.