This example was great - thanks.  That xdmp:quote function hadn't occurred to 
me, and your FLOWR is better constructed than mine.  Do appreciate that, as 
well as the helpful tips everyone suggested.  I have ML8 on my laptop but our 
servers still using ML7.  One thing I had tried worked great on QConsole but 
when deployed on the server throw errors, so am very happy to see this 
relatively minor bit working correctly now.  Thanks again!

import module namespace json = "http://marklogic.com/xdmp/json"; at 
"/MarkLogic/json/json.xqy";

let $docs := (<root>a</root>, <root>b</root>, <root>c</root>)

let $outputJSON:=("{&#34;events&#34;:[",
  let $config := json:config("full"),
      $cx := map:put( $config, "whitespace", "ignore" )
  return fn:string-join(
    for $doc at $counter in $docs
    return xdmp:quote(json:transform-to-json($doc, $config)),","),
"]}")



From: [email protected] 
[mailto:[email protected]] On Behalf Of Joe Bryan
Sent: Tuesday, November 17, 2015 10:42 AM
To: MarkLogic Developer Discussion <[email protected]>
Subject: Re: [MarkLogic Dev General] $counter < i -- I might be overthinking 
this

I should clarify:

In ML 8, json:transform-to-json() returns a document-node(), wrapping an 
object-node(), so xdmp:quote() is necessary to serialize that into a string: 
http://docs.marklogic.com/json:transform-to-json

In ML 7, json:transform-to-json() returns a string, so you could simply remove 
xdmp:quote() from that example: 
http://docs.marklogic.com/7.0/json:transform-to-json

The first example should work without modification in both ML7 and ML8.

Thanks.

-jb

From: 
<[email protected]<mailto:[email protected]>>
 on behalf of Joe Bryan 
<[email protected]<mailto:[email protected]>>
Reply-To: MarkLogic Developer Discussion 
<[email protected]<mailto:[email protected]>>
Date: Tuesday, November 17, 2015 at 1:39 PM
To: MarkLogic Developer Discussion 
<[email protected]<mailto:[email protected]>>
Subject: Re: [MarkLogic Dev General] $counter < i -- I might be overthinking 
this

Hi Kari,

Instead of building your JSON output as a string, you could could build a 
json:object or map and convert it to JSON. Here's a simple example:

import module namespace json = "http://marklogic.com/xdmp/json"; at 
"/MarkLogic/json/json.xqy";

let $docs := (<root>a</root>, <root>b</root>, <root>c</root>)
return
  xdmp:to-json(
    map:entry("docs",
      json:to-array(
        for $doc in $docs
        return json:transform-to-json($doc, json:config("full"))/node())))

By way of comparison, here's how you could build the JSON as a string. Note 
that the document-node() from json:transform-to-json() needs to be serialized 
with xdmp:quote():

import module namespace json = "http://marklogic.com/xdmp/json"; at 
"/MarkLogic/json/json.xqy";

let $docs := (<root>a</root>, <root>b</root>, <root>c</root>)

let $outputJSON:=("{&#34;events&#34;:[",
  let $config := json:config("full"),
      $cx := map:put( $config, "whitespace", "ignore" )
  return fn:string-join(
    for $doc at $counter in $docs
    return xdmp:quote(json:transform-to-json($doc, $config)),","),
"]}")

return $outputJSON

Thanks.

-jb

From: 
<[email protected]<mailto:[email protected]>>
 on behalf of Kari Cowan <[email protected]<mailto:[email protected]>>
Reply-To: MarkLogic Developer Discussion 
<[email protected]<mailto:[email protected]>>
Date: Tuesday, November 17, 2015 at 1:18 PM
To: MarkLogic Developer Discussion 
<[email protected]<mailto:[email protected]>>
Subject: Re: [MarkLogic Dev General] $counter < i -- I might be overthinking 
this

Can you suggest the best way to do that?  I tried casting it as a string in the 
return but that doesn't do the job (and doesn't show any error).

let $outputJSON:=("{&#34;events&#34;:[",
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
  fn:string(fn:string-join(json:transform-to-json( fn:doc($events/doc-uri), 
$config ),",")),
"]}")


From:[email protected]<mailto:[email protected]>
 [mailto:[email protected]] On Behalf Of Christopher 
Hamlin
Sent: Tuesday, November 17, 2015 9:37 AM
To: MarkLogic Developer Discussion 
<[email protected]<mailto:[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:=("{&#34;events&#34;:[",
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),
"]}")


________________________________
Learn more about ALM, visit http://www.alm.com . - ALM, an Integrated Media 
Company, is a leading provider of news and information for the legal and 
commercial real estate markets. ALM's market-leading brands include The 
American Lawyer, Corporate Counsel, GlobeSt.com, Insight Conferences, Law.com, 
Law Journal Press, LegalTech, The National Law Journal and Real Estate Forum.



_______________________________________________
General mailing list
[email protected]
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to