The format-xml and format-json cts.search() for cts.search() filter the search results for these formats; they do not perform any conversions (see cts.search() for parameter documentation ).
There are many ways to convert XML to JSON using MarkLogic; probably the simplest is the json XQuery library , specifically json:transform-to-json-object() . You can use this library on the JS server side as follows:
var json = require('/MarkLogic/json/json.xqy'); var doc = cts.doc('/triplestore/97a5ab126bddeea0.xml'); var jsonDoc = json.transformToJsonObject(doc, json.config('custom'));
You can use json.config() to configure and configure the conversion.
cts.search() returns an Iterator , so you need a for-of loop (or some kind of battery function) to get the actual XML documents, which can then be converted.
Update
This error may be a bug in the JSON library, but it is a very deep HTML path; and I donβt think it makes sense to convert the HTML elements into JSON object properties. Instead, we will serialize the HTML and add the string back to our JSON object.
Here is an example of converting search results; showing how to deploy Iterator , configure JSON conversions, serialize XHTML content for use in JSON, etc.
Notes:
- it uses
fn.subsequence to limit the Iterator to the first 10 results. - I am serializing the excluded
<html/> elements into a string (using the xpath() method of the Node object and xdmp.quote() and adding this JSON object as escapedContent .
Here is a combined example; you can run this in MarkLogic QConsole:
var json = require('/MarkLogic/json/json.xqy'); var conf = json.config('custom'); var htmlNs = 'http://www.w3.org/1999/xhtml'; // exclude <html:html/> elements // Note: this is a little awkward because the JSON library is XQuery // and requires an XDM sequence, not an Array conf['ignore-element-names'] = json.arrayValues([ fn.QName(htmlNs, 'html') ]); var results = fn.subsequence( cts.search(cts.andQuery(null), 'format-xml'), 1, 10 ); var transformedResults = []; var transformed = []; for (var result of results) { // transformToJson() returns an object-node() wrapped in a document-node() // convert it to a regular JS object transformed = json.transformToJson(result, conf).toObject() transformed.escapedContent = xdmp.quote( result.xpath('.//html:html', { html: htmlNs}) ); transformedResults.push(transformed); } transformedResults