MarkLogic 8 server side javascript: convert existing xml to json

I am testing a new server side API in MarkLogic 8. I am interested in search. The examples I've seen start by storing the JSON in a database and then searching in native JSON. This is not my use case, because my company has many native XML already stored in the database. I want to use javascript to search documents and I want the search results in JSON.

Is this possible from javascript? When I run the code below (modified from one of the examples), the result is an array of strings, where each string is a complete XML document. Not what I want. Is there an API call to convert each result to JSON?

var count = 0;
var results = [];
for (var result of cts.search(cts.wordQuery("value1"))) {
    count++;
    results.push(result); 
};
results.push(fn.concat("Count = ", count));
results;

      

+3


source to share


1 answer


Yes, you can search and manipulate XML content using JavaScript, although in general JavaScript handles JSON better natively and XQuery handles XML better. I mean, languages ​​are designed in these data formats, not that you can't do anything in one or the other.



You don't need to translate XML to JSON (if you don't want to). The search result will be a document node, and you can use the DOM API or any of the built-in functions that work with nodes to process the results.

+4


source







All Articles