Transform XML elements of the same name into JSON properties of the same name
Your JSON example:
object-node { "value" : "value1", "value" : "value2"}
isn't really valid: or at any rate, it's best avoided. RFC 7159 says:
When the names within an object are not unique, the behavior of software that receives such an object is unpredictable. Many implementations report the last name/value pair only. Other implementations report an error or fail to parse the object, and some implementations report all of the name/value pairs, including duplicates.
I don't believe that it is possible to embed a FLWOR inside of the object-node()
constructor.
You could construct a string and evaluate with xdmp:eval()
or xdmp:value()
:
let $container := <container> <value>value1</value> <value>value2</value> </container>return xdmp:value( "object-node {" || fn:string-join($container/value ! ('"' || local-name() || '": "' || . || '"'), ",") || " }" )
Or build the JSON string and use xdmp:unquote()
:
let $container := <container> <value>value1</value> <value>value2</value> </container>return xdmp:unquote( "{" || fn:string-join($container/value ! ('"' || local-name() ||'": "' || . || '"'), ",") || "}" )