[MarkLogic Dev General] Custom JSON objects

Kari Cowan KCowan at alm.com
Mon Jan 18 16:07:00 PST 2016


Just returning to this, I wanted to try constructing the map, but instead of getting back the values, I get back the document URI and path to the node.    Not what I expected.  What am I doing wrong?

let $outputSimpleJSON:=

  for $doc in $search-this-partner/event (: there are 3 records in this set :)

    let $theEvent:= $doc//EventTitle
    let $theStartDate:=$doc/EventStartDate

  let $map := map:new((
    map:entry("EventTitle", $theEvent),
    map:entry("EventStartDate", $theStartDate)
  ))

return xdmp:to-json($map)

>>

{"EventStartDate":"fn:doc(\"\/data-sources\/events\/Akira_7877013.xml\")\/event\/EventStartDate", "EventTitle":"fn:doc(\"\/data-sources\/events\/Akira_7877013.xml\")\/event\/EventTitle"}
{"EventStartDate":"fn:doc(\"\/data-sources\/events\/TheDayTheEarthStoodStillMarch08,2017.xml\")\/event\/EventStartDate", "EventTitle":"fn:doc(\"\/data-sources\/events\/TheDayTheEarthStoodStillMarch08,2017.xml\")\/event\/EventTitle"}
{"EventStartDate":"fn:doc(\"\/data-sources\/events\/Solaris_956351.xml\")\/event\/EventStartDate", "EventTitle":"fn:doc(\"\/data-sources\/events\/Solaris_956351.xml\")\/event\/EventTitle"}



From: general-bounces at developer.marklogic.com [mailto:general-bounces at developer.marklogic.com] On Behalf Of Joe Bryan
Sent: Monday, December 14, 2015 11:08 AM
To: MarkLogic Developer Discussion <general at developer.marklogic.com>
Subject: Re: [MarkLogic Dev General] Custom JSON objects

Hi Kari,

You can use the "custom" strategy with json:config(): http://docs.marklogic.com/json:config. There are lots of options that can be specified to control the transformation(for instance, "ignore-attribute-names").

Alternately, you can manipulate the resulting JSON object, or even code your own transformation. One of the simplest ways to implement your own transformation is to construct nested maps from the data you want, and then convert the map to json:

let $map := map:new((
  map:entry("key", "value"),
  map:entry("anotherKey", "a different value")
))
return xdmp:to-json($map)

Thanks.

-jb

From: <general-bounces at developer.marklogic.com<mailto:general-bounces at developer.marklogic.com>> on behalf of Kari Cowan <KCowan at alm.com<mailto:KCowan at alm.com>>
Reply-To: MarkLogic Developer Discussion <general at developer.marklogic.com<mailto:general at developer.marklogic.com>>
Date: Monday, December 14, 2015 at 1:42 PM
To: "general at developer.marklogic.com<mailto:general at developer.marklogic.com>" <general at developer.marklogic.com<mailto:general at developer.marklogic.com>>
Subject: [MarkLogic Dev General] Custom JSON objects

I had a question from someone on my team if it's possible to return a more simplified version JSON without child and attributes.

For example, compare Events JSON object:

http://data.smartlitigator.com/EventTools/GetEventsTable?display=New+York+Law+Journal|returnType=JSON

To this:

http://data.smartlitigator.com/QuestV2/Search/?query=law&publication=&section=&orgType=&elementType=&articleType=&practiceArea=&virtualPracticeArea=&subject=&industry=&source=lawcom&almStaff=&lawTopic=&fromDateTime=&toDateTime=&fromDate=1985-01-01&toDate=2015-11-07&start=1&end=10&sort=rank&direction=descending&returnType=falcon_json&callback=


Events, simply contructed snippet:

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 $search-this-partner/event
    order by $doc/EventStartDate/@date descending
    return xdmp:quote(json:transform-to-json($doc, $config)),","),
"]}")


Is it possible to specify which nodes and the format I want to convert, still using transform-to-json, or do I need to write a custom output myself?


________________________________
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.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://developer.marklogic.com/pipermail/general/attachments/20160119/f4d559e3/attachment.html 


More information about the General mailing list