Reconstruct and transform JSON object from XQuery Reconstruct and transform JSON object from XQuery json json

Reconstruct and transform JSON object from XQuery


I'm afraid using json:object really is the way to use here. Could be worse though, you only need a few lines to copy all json properties. You also don't need that document{} constructor, nor the extra type cast to json:object. xdmp:from-json already returns a json:object:

let $org := xdmp:from-json($json)let $new := json:object()let $_ :=  for $key in map:keys($org)  return map:put($new, $key, map:get($org, $key))return xdmp:to-json($new)/node()

HTH!


This may be helpful for you: http://docs.marklogic.com/guide/app-dev/json

However, I often take a different approach in xQuery (being comfortable with XML). This may get some push-back from people here, but it is my approach:

Construct what you like in XML and then transform it. If you make your XML in the http: //marklogic.com/xdmp/json/basic namespace, then you can just transform it to whatever complex JSON you desire using json:transform-to-json - since all of the hints to datatypes are in the attributes of the XML. The nice thing about this approach is that it is a nice middle format. I can transform to JSON - or I can apply an XSLT transformation and get other XML if I desire.

It should be noted that json:transform-to-json has other modes of operation and can get datatype hints from your own schema as well. But I perfer the built-in schema.


I stumbled across this blog post by @paxstonhare that uses a non-functional approach, rebuilding new JSON objects during the tree walk by mutating them using map:put():

http://developer.marklogic.com/blog/walking-among-the-json-trees