Assuming your on ML 8.0 or greater.
you can use json:transform-to-json-string() which is a direct replacment to
json:transform-to-json() from prior versions.
( that is if you really want strings ... generally its much easier to not
convert to a string but rather build
the appropriate structure in the first place. )
Trying to do string operations on JSON is as tedious as it is in XML ...
so if your trying to make JSON like
{ "events" : [ array of other json things ] }
Do that directly.
In 8.0+ you can do this directly in json node construrs
object-node { "events" :
array-node {
for $j in some-query-function()
return
json:transform-to-json( $j , json:config("full") ) )
}
}
This will be a native JSON Object { "events" : [ array of other json things ]
}
In 7.0 (and 8) you can use the lower level object/array native types (
json:object, json:array )
and create them dynamicly as you would create a map:map()
https://docs.marklogic.com/guide/app-dev/json#id_57191
If you need that converted to a string then call xdmp:quote( $obj)
String concatenation should be the very very last resort ... (it shouldn't ever
be needed)
-----------------------------------------------------------------------------
David Lee
Lead Engineer
MarkLogic Corporation
[email protected]
Phone: +1 812-482-5224
Cell: +1 812-630-7622
www.marklogic.com<http://www.marklogic.com/>
From: [email protected]
[mailto:[email protected]] On Behalf Of Christopher Hamlin
Sent: Tuesday, November 17, 2015 12:37 PM
To: MarkLogic Developer Discussion <[email protected]>
Subject: Re: [MarkLogic Dev General] $counter < i -- I might be overthinking
this
json:transform-to-json is returning a document node. fn:concat takes a bunch
of things and casts them to xs:string. fn:string-join takes strings
specifically.
So two possibilities:
1. You need to change the docs to strings before doing string-join.
2. Make sure the strings are in a sequence, not "loose" like for fn:concat.
/ch
On Tue, Nov 17, 2015 at 12:30 PM, Kari Cowan
<[email protected]<mailto:[email protected]>> wrote:
fn:string-join - I thought about that – tried it but it didn’t join each with
the comma – it also didn’t give me any error. That would have been the
simplest had it worked; wouldn’t need the if statement at all. I thought
perhaps it’s not really a string, but documents that are transformed to JSON
from XML? (not sure, that’s still going to return as a string I think). Any
idea why fn:string-join didn’t work?
So I tried the counter idea Gert suggested – this seems to work, as long as the
ordering is ‘descending’ – if I switch it to ascending, it gets quirky – Is it
using the native state not what I set?
let $outputJSON:=("{"events":[",
let $config := json:config("full"),
$cx := map:put( $config, "whitespace", "ignore" )
let $count:= fn:count($search-this-partner/event)
for $events at $counter in $search-this-partner/event
order by $events/EventStartDate/@date descending
return if( $count > $counter ) then
( fn:concat(json:transform-to-json( fn:doc($events/doc-uri), $config ),
","),"count: ", $count, "counter: ", $counter )
else
( json:transform-to-json( fn:doc($events/doc-uri), $config ), "else count: ",
$count, "else counter: ", $counter),
"]}")
_______________________________________________
General mailing list
[email protected]
Manage your subscription at:
http://developer.marklogic.com/mailman/listinfo/general