3
votes

I'm trying out the new server-side javascript API in MarkLogic 8. I'm mainly interested in search. The examples I've seen start by saving JSON into the database and then search the native JSON. That is not my use case because my company has tons of native XML already stored in the database. I want to use javascript to search for documents and I want the search results in JSON.

Is that even possible from javascript? When I run the code below, (modified from one of the examples) the result is an array of string, 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;
1

1 Answers

4
votes

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

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