Pragya, Regarding xml to json conversion, where the resulting output is headless. I wrote a library that can do this. You will need to upload it to your modules database at /utilities/jsonify.xqy or wherever is convenient.
jsonify.xqy at https://gist.github.com/masyukun/4190b195afb02e940453c3fbb829dcc4 xquery version "1.0-ml"; import module namespace jsontools = " http://matthewroyal.com/marklogic/jsontools" at "/utilities/jsonify.xqy"; import module namespace json = "http://marklogic.com/xdmp/json" at "/MarkLogic/json/json.xqy"; let $xml := <document> <document-id>/docs/abc.xml</document-id> <action>add</action> <metadata> <name>XYZ</name> <value>fsddddddddddddddddddddddddddddddd sdgsdsdsdsdsdsdsdsdsdsd</value> <type>text</type> </metadata> <metadata> <name>Period</name> <value>aertw hegheh eheheh ghhehe</value> <type>text</type> </metadata> </document> return jsontools:jsonify( $xml/child::*, "headless" ) Returns: {"document-id":"/docs/abc.xml", "action":"add", "metadata":[{"name":"XYZ", "value":"fsddddddddddddddddddddddddddddddd sdgsdsdsdsdsdsdsdsdsdsd", "type":"text"}, {"name":"Period", "value":"aertw hegheh eheheh ghhehe", "type":"text"}]} Matthew Royal MarkLogic Senior Consultant On Tue, Jan 10, 2017 at 5:31 AM, <[email protected]> wrote: > Send General mailing list submissions to > [email protected] > > To subscribe or unsubscribe via the World Wide Web, visit > http://developer.marklogic.com/mailman/listinfo/general > or, via email, send a message with subject or body 'help' to > [email protected] > > You can reach the person managing the list at > [email protected] > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of General digest..." > > > Today's Topics: > > 1. Re: Json file (Kapoor, Pragya) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Tue, 10 Jan 2017 10:31:44 +0000 > From: "Kapoor, Pragya" <[email protected]> > Subject: Re: [MarkLogic Dev General] Json file > To: MarkLogic Developer Discussion <[email protected]> > Message-ID: > <HK2PR02MB1364A2C18CE6F1508DDC25A3EA670@HK2PR02MB1364. > apcprd02.prod.outlook.com> > > Content-Type: text/plain; charset="us-ascii" > > Hi Dharmendra, > > > Thanks for the reply, but didnt work > > > the Required output is: > {"document-id":"\/docs\/abc.xml", "action":"add", > "metadata":[{"name":"XYZ", "value":"fsddddddddddddddddddddddddddddddd > sdgsdsdsdsdsdsdsdsdsdsd", "type":"text"},{"name":"Period", "value":"aertw > hegheh eheheh ghhehe", "type":"text"}]} > > > This is a valid json without root. > > > Thanks > > Pragya > > > ________________________________ > From: [email protected] <general-bounces@developer. > marklogic.com> on behalf of DK Singh <[email protected]> > Sent: Monday, January 9, 2017 5:44:44 PM > To: MarkLogic Developer Discussion > Subject: Re: [MarkLogic Dev General] Json file > > Try out the code if it works as your requirement > > Regards > Dharmendra Kumar Singh > > On Jan 9, 2017 5:34 PM, "DK Singh" <[email protected]<mailto: > [email protected]>> wrote: > Hi Pragya > > You can try out the below code: > > json:transform-to-json($xml/child::*,$custom) > > but your json output won't be valid. > > Regards > Dharmendra Kumar Singh > > On Mon, Jan 9, 2017 at 5:31 PM, DK Singh <[email protected]<mailto: > [email protected]>> wrote: > Hi Pragya, > > You can not remove the element "document" because it is root element > either XML or JSON must have a root element, you can remove the child > elements but not the root element "document". You can replace the element > document with other element. > if you remove the element 'document' your document will not be valid > > Regards > Dharmendra Kumar Singh > > On Mon, Jan 9, 2017 at 5:08 PM, Kapoor, Pragya <[email protected] > <mailto:[email protected]>> wrote: > > Hi, > > > Now I want to ignore the document element while constructing the json: > > > import module namespace json = "http://marklogic.com/xdmp/json" > at "/MarkLogic/json/json.xqy"; > > > declare variable $xml := > <document> > <document-id>/docs/abc.xml</document-id> > <action>add</action> > <metadata> > <name>XYZ</name> > <value>fsddddddddddddddddddddddddddddddd sdgsdsdsdsdsdsdsdsdsdsd</value> > <type>text</type> > </metadata> > <metadata> > <name>Period</name> > <value>aertw hegheh eheheh ghhehe</value> > <type>text</type> > </metadata> > </document> ; > > > let $custom := > let $config := json:config("custom") > let $_ := map:put( $config, "whitespace", "ignore" ) > let $_ := map:put( $config, "ignore-element-names", "document" ) > let $_ := map:put( $config, "array-element-names", "metadata" ) > return $config > > let $json := json:transform-to-json($xml,$custom) > return $json > > But with the above code its not ignoring the document element. > > Required output: > {"document-id":"\/docs\/abc.xml", "action":"add", > "metadata":[{"name":"XYZ", "value":"fsddddddddddddddddddddddddddddddd > sdgsdsdsdsdsdsdsdsdsdsd", "type":"text"},{"name":"Period", "value":"aertw > hegheh eheheh ghhehe", "type":"text"}]} > > > Please let me know, if I am missing something. I am using MarkLogic 7 > > > Thanks > > Pragya > > ________________________________ > From: [email protected]<mailto:general- > [email protected]> <[email protected] > <mailto:[email protected]>> on behalf of David Lee < > [email protected]<mailto:[email protected]>> > Sent: Tuesday, January 3, 2017 6:39:28 PM > > To: MarkLogic Developer Discussion > Subject: Re: [MarkLogic Dev General] Json file > > In v7 transform-to-json returns a string. > Try xdmp:save("file", text { $json }) > -DavId Lee > > Sent from my iPad > > On Jan 3, 2017, at 5:24 AM, Kapoor, Pragya <[email protected]<mailto: > [email protected]>> wrote: > > > Hi, > > > I am getting this error: > > XDMP-ARGTYPE: (err:XPTY0004) xdmp:save("C:\123", > "{"document":{"docID":"\/docs\/0000002-0...") -- > arg2 is not of type node() > > > I am using MarkLogic 7. > > > declare variable $xml := > <document > docID="/docs/0000002-0000468-0004456-ISDA-COL-XXXX_2015-04-01_06-33-57-639.xml" > action="add"> > <metadata> > <name>XYZ</name> > <value>fsddddddddddddddddddddddddddddddd sdgsdsdsdsdsdsdsdsdsdsd</value> > <type>text</type> > </metadata> > <metadata> > <name>Period</name> > <value>aertw hegheh eheheh ghhehe</value> > <type>text</type> > </metadata> > </document>; > > > let $uri := "123" > let $custom := > let $config := json:config("custom") > let $_ := map:put( $config, "whitespace", "ignore" ) > let $_ := map:put( $config, "array-element-names", "metadata" ) > return $config > > let $json := json:transform-to-json($xml,$custom) > > return > > ( > > xdmp:save(concat('C:\test\', $uri), $json) > > ) > > Thanks > Pragya > > > ________________________________ > From: [email protected]<mailto:general- > [email protected]> <[email protected] > <mailto:[email protected]>> on behalf of DK Singh < > [email protected]<mailto:[email protected]>> > Sent: Monday, January 2, 2017 1:06:24 PM > To: MarkLogic Developer Discussion > Subject: Re: [MarkLogic Dev General] Json file > > Hi Pragya, > > You can try out the below mentioned code: > > import module namespace json = "http://marklogic.com/xdmp/json" at > "/MarkLogic/json/json.xqy"; > for $xmls in fn:collection('MarcVideo')[1 to 10] > let $xmls1 := xdmp:quote($xmls) > let $xmls2 := xdmp:unquote($xmls1) > let $uri := base-uri($xmls) > let $token := tokenize($uri,'/')[last()] > let $custom := > let $config := json:config("custom") > let $_ := map:put( $config, "whitespace", "ignore" ) > let $_ := map:put( $config, "array-element-names", "Video" ) > return $config > > let $json := json:transform-to-json($xmls,$custom) > > return > > ( > > xdmp:save(concat('D:\test\', $token), $json) > > ) > > > Regards > > Dharmendra Kumar Singh > On Mon, Jan 2, 2017 at 12:28 PM, Kapoor, Pragya <[email protected] > <mailto:[email protected]>> wrote: > > Hi, > > > Requirement: > > 1. Convert xmls stored in marklogic db to json then save them to file > system. > > 2. Store in-memory json in ML DB > > > How can we achieve the same? > > > Thanks > > Pragya > > "This e-mail and any attachments transmitted with it are for the sole use > of the intended recipient(s) and may contain confidential , proprietary or > privileged information. If you are not the intended recipient, please > contact the sender by reply e-mail and destroy all copies of the original > message. Any unauthorized review, use, disclosure, dissemination, > forwarding, printing or copying of this e-mail or any action taken in > reliance on this e-mail is strictly prohibited and may be unlawful." > > _______________________________________________ > General mailing list > [email protected]<mailto:[email protected]> > Manage your subscription at: > http://developer.marklogic.com/mailman/listinfo/general > > > "This e-mail and any attachments transmitted with it are for the sole use > of the intended recipient(s) and may contain confidential , proprietary or > privileged information. If you are not the intended recipient, please > contact the sender by reply e-mail and destroy all copies of the original > message. Any unauthorized review, use, disclosure, dissemination, > forwarding, printing or copying of this e-mail or any action taken in > reliance on this e-mail is strictly prohibited and may be unlawful." > _______________________________________________ > General mailing list > [email protected]<mailto:[email protected]> > Manage your subscription at: > http://developer.marklogic.com/mailman/listinfo/general > "This e-mail and any attachments transmitted with it are for the sole use > of the intended recipient(s) and may contain confidential , proprietary or > privileged information. If you are not the intended recipient, please > contact the sender by reply e-mail and destroy all copies of the original > message. Any unauthorized review, use, disclosure, dissemination, > forwarding, printing or copying of this e-mail or any action taken in > reliance on this e-mail is strictly prohibited and may be unlawful." > > _______________________________________________ > General mailing list > [email protected]<mailto:[email protected]> > Manage your subscription at: > http://developer.marklogic.com/mailman/listinfo/general > > > > "This e-mail and any attachments transmitted with it are for the sole use > of the intended recipient(s) and may contain confidential , proprietary or > privileged information. If you are not the intended recipient, please > contact the sender by reply e-mail and destroy all copies of the original > message. Any unauthorized review, use, disclosure, dissemination, > forwarding, printing or copying of this e-mail or any action taken in > reliance on this e-mail is strictly prohibited and may be unlawful." > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: http://developer.marklogic.com/pipermail/general/ > attachments/20170110/f61da355/attachment.html > > ------------------------------ > > _______________________________________________ > General mailing list > [email protected] > Manage your subscription at: > http://developer.marklogic.com/mailman/listinfo/general > > > End of General Digest, Vol 151, Issue 20 > **************************************** >
_______________________________________________ General mailing list [email protected] Manage your subscription at: http://developer.marklogic.com/mailman/listinfo/general
