I don't understand why Marklogic json:config generates "_children". How can I exclude "_children". I see all the enumerated children come back for parents, but there's a lot of extra junk in the JSON output.
let $config := json:config("custom")
let $_ := map:put( $config, "full-element-names",xs:QName("Nav:keynavlist")
Json output:
"Navigators": {
"keynavlist": {
"_children": [
{
"keynav": {
"_value": "Fuel Cells"
}
},
{
"keynav": {
"_value": "Microorganisms"
}
},
{
"keynav": {
"_value": "Waste Treatment"
}
I am guessing you would like to get something like:
{
"Navigators": {
"keynavlist": [{
"keynav": "Fuel Cells"
}, {
"keynav": "Microorganisms"
}, {
"keynav": "Waste Treatment"
}]
}
}
But closest you can get with the json transform lib is this:
{
"Navigators": {
"keynavlist": {
"keynav": ["Fuel Cells", "Microorganisms", "Waste Treatment"]
}
}
}
Using:
xquery version "1.0-ml";
import module namespace json="http://marklogic.com/xdmp/json" at "/MarkLogic/json/json.xqy";
let $config := json:config("custom")
let $_ := map:put($config, "array-element-names", ("keynav"))
return json:transform-to-json(
<Navigators>
<keynavlist>
<keynav>Fuel Cells</keynav>
<keynav>Microorganisms</keynav>
<keynav>Waste Treatment</keynav>
</keynavlist>
</Navigators>,
$config
)
Consider building json with a recursive function, maybe something like this:
declare function local:xml-to-json($nodes) {
for $node in $nodes
return typeswitch ($node)
case element() return
if ($node/attribute() or ($node/element() and $node/text())) then
object-node {
local-name($node): object-node {
"@": array-node{ local:xml-to-json($node/attribute()) },
"_": array-node { local:xml-to-json($node/node()) }
}
}
else
object-node {
local-name($node): array-node{ local:xml-to-json($node/node()) }
}
case attribute() return
object-node {
local-name($node): data($node)
}
default return $node
};
local:xml-to-json(
<Navigators>
<keynavlist>
<keynav>Fuel Cells</keynav>
<keynav>Microorganisms</keynav>
<keynav>Waste Treatment</keynav>
</keynavlist>
</Navigators>
)
HTH!