[MarkLogic Dev General] Custom JSON objects

Joe Bryan Joe.Bryan at marklogic.com
Tue Jan 19 08:26:38 PST 2016


Hi Kari,

I'm not sure why the map:* API works that way, but you have to be explicit in getting the values from the elements in question. Specifically, you can use fn:string() to get the element contents as a string, you can use fn:data() to defer to any applicable schema types, or you can use the xs:* type constructors (xs:integer(), xs:decimal(), etc.).

You can apply the types in the map:

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

Or earlier, in your variable bindings:

let $theEvent:= $doc/EventTitle/fn:string()
let $theStartDate:=$doc/EventStartDate/fn:string()

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: Tuesday, January 19, 2016 at 10:43 AM
To: MarkLogic Developer Discussion <general at developer.marklogic.com<mailto:general at developer.marklogic.com>>
Subject: Re: [MarkLogic Dev General] Custom JSON objects

It’s ML 7

How would I get back the values rather than the node paths?

In other words, this:

let $outputSimpleJSON:=("{&#34;events&#34;:[",
fn:string-join(
  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:quote(xdmp:to-json($map)),","),
"]}")

Returns a valid JSON object for the 3 records returned, but it doesn’t  have the values of the data.

Example: >>

{"events":[
{"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"}
]}

I need the values, like this – any way to do it?

{"events":[
{"EventStartDate":"2018-03-08", "EventTitle":"Akira"},
{"EventStartDate":"2017-03-08", "EventTitle":"The Day The Earth Stood Still"},
{"EventStartDate":"2017-09-30", "EventTitle":"Solaris"}
]}



From: general-bounces at developer.marklogic.com<mailto:general-bounces at developer.marklogic.com> [mailto:general-bounces at developer.marklogic.com] On Behalf Of Rajamani.Marimuthu at cognizant.com<mailto:Rajamani.Marimuthu at cognizant.com>
Sent: Tuesday, January 19, 2016 5:00 AM
To: general at developer.marklogic.com<mailto:general at developer.marklogic.com>
Subject: Re: [MarkLogic Dev General] Custom JSON objects

Hi,

Your code is working fine and getting expected result . Could you please tell me what version of ML  u r using.

By
Raja >>>

From: general-bounces at developer.marklogic.com<mailto:general-bounces at developer.marklogic.com> [mailto:general-bounces at developer.marklogic.com] On Behalf Of Kari Cowan
Sent: Tuesday, January 19, 2016 5:37 AM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] Custom JSON objects

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> [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<mailto: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.
________________________________
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.
This e-mail and any files transmitted with it are for the sole use of the intended recipient(s) and may contain confidential and privileged information. If you are not the intended recipient(s), please reply to the sender and destroy all copies of the original message. Any unauthorized review, use, disclosure, dissemination, forwarding, printing or copying of this email, and/or any action taken in reliance on the contents of this e-mail is strictly prohibited and may be unlawful. Where permitted by applicable law, this e-mail and other e-mail communications sent to and from Cognizant e-mail addresses may be monitored.
________________________________
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.

________________________________
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/6ef70067/attachment-0001.html 


More information about the General mailing list