Developers |
Home:Developers:Tutorials & Examples:JSON vs XML JSON Processing using XQueryThere has been a point of controversy over whether JSON or XML is better at dealing with communication between endpoints. For documents, XML is certainly the leader, supporting namespaces and mixed content. But for communications between live programs, JSON with its implicit hash and array support. Where the message content can be handled equally well by either, JSON is more succint, but not as much as one might think. But because the syntax is simpler, it parses more quickly. But it doesn't matter, because with DataDirect XML Converters, you can transform JSON or XML with equal facility. A Simple JSON XQueryHere is a retrieval http call of calendar data from Google's developer calendar.
(In this and several of the following examples, spaces and line breaks are added for clarity.) The portions in this color are the actual XQuery syntax, which includes escaping the "&" characters. The portion in this color is the call to the converter. What the above mini XQuery program (yes, it is actually a complete program) does is fetch the calendar data as JSON and convert it to XML. Because we haven't specified any transformations, the content will just be output. Taking the resulting JSON from the raw http: query would result in something like this (formatted for ease of reading):
which is clearly some sort of nested tree structure. Since XML is also a nested tree structure,* and XQuery actually operates on the XQuery Data Model and not on just raw XML bits, using the JSON XML Converter we can transform this on-the-fly to XML input for another application, including the DataDirect XQuery Engine. (* actually, this looks like XML converted to JSON, which we're going to deconvert.) This means the XQuery engine effectively "sees" something like the following — but remember, this is a virtual translation, which means there is no separate step that saves something as JSON and then reloads it as XML. It all happens inside of the JSON XML Converter library.
Once the data "looks" like XML, XQuery can transform it. So to see all of the names and dates of the next four upcoming entries, the following XQuery program will do just that:
Which of course produces the following schedule of upcoming Google-related events:
JSON versus XML — A Moot PointThe real advantage of the DataDirect XML Converters is that they make file format issues irrelevant. Just as DataDirect XQuery is a highly conformant W3C XQuery processor that works the same against any of the databases it is connected to — whether Oracle, IBM DB2, Sybase, MySQL, SQL Server or whatever — so do the XML Converters mask the difference between different storage formats. So the JSON adherents can have their way, and the XML adherents theirs, but you can bridge the gap and process data from either or both sources without difficulty. |
|||||||||||||||||||





