Hi Marco,
I am not
quite sure if I understand your question well, but let me
make a couple of remarks about the rationale of treating
JSON as XML.
XML is
accessible to XPath, nested maps and arrays are not. Hence
any non-trivial navigation of XML trees is incomparably
simpler and more elegant than navigation of JSON trees,
representated by nested maps and arrays. Similarly, the
*construction* of complex XML structure is often much
simpler than the construction of nested maps and arrays. As
a consequence, the processing of JSON can often be radically
simplified by dealing with XML, rather than JSON: just frame
your processing between an initial JSON to XML
transformation (json:parse) and a final XML to JSON
transformation (json:serialize).
It is
important to realize that there are alternative XML
representations of JSON. The representation defined in the
XSLT 3.0 spec is very "clean" from a conceptual point of
view, but navigation-unfriendly, as the all important object
member names are represented by attributes, rather than
element names (which are restricted to "map", "array",
"string", "number", "boolean", umph!), resulting in a rather
cluttered representation, requiring clumsy XPath
expressions. The alternative representation offered by BaseX
[1], on the other hand, looks intuitive and enables
navigation which is in typically cases as elegant as if the
data were supplied in XML to begin with. I can highly
recommend dealing with JSON via the BaseX XML
representation, and I use this XML format also in the
construction of JSON.
An
advantage of the map/array representation is higher
performance, which may become important especially when
dealing with very large documents.
Wrapping
up, it is very important to have access to both principal
kinds of JSON representation in XQuery, maps/array and XML.
Kind
regards,
Hans-Jürgen
PS: If
you are interested in a discussion comparing navigation of
JSON represented by maps/arrays versus XML, you might
consider reading here:
Hi all,
I have a bit of difficulty in understanding
the rationale behind the XML to Json (and
vice-versa) conversions.
I must admit I'm a bit confused on when to
use which format and how those map to
serialization/parsing rules.
In particular I'm wondering whether it's
really necessary to use both the formalisms
(and functions) in the following example.
My feelings are that using only the
standard (?) XQuery 3.1 representation would
be much clearer and would enable a cleaner
service chaining mechanism.
Has anyone 2 minutes left to explain to me
the model behind this sort of things?
I really appreciate your support!
Regards,
Marco.
module namespace jt = "urn:json:test";
declare
%rest:path("jsonsrv")
%output:method("text")
%output:media-type("application/json")
function
jt:service()
{
xml-to-json(
<string
key="a">12345678</string>
</map>)
};
declare
%rest:path("testreq")
%output:method("text")
%output:media-type("application/json")
function jt:test()
{
<http:body
media-type="application/x-www-form-urlencoded"
method="text"/>
return
json:serialize($response[2])
};