The issue is that there’s no perfect lossless translation in both directions. 
(If there were, we’d have implemented that across the board.) Some complicating 
factors: XML namespaces, mixed content, JSON arrays, allowable characters in 
JSON keys, etc.
The use case for json:transform-to-json, is about getting a semantically 
similar representation of a JSON document in the XML data model, for search and 
manipulation in MarkLogic. The default translation used by the REST API is 
lossless, so you can get the JSON (semantically, not byte-for-byte) back that 
you put in. If you need more control over the JSON serialization, there are 
configuration options for json:transform-to-json. However, if you need to 
generate a very specific JSON representation json:object and json:array allow 
you the lowest-level control.

TL;DR: If you want to persist some JSON and search it, use 
json:transform-to-json, as the REST API does. If you need precise control over 
the JSON representation, use json:object/json:array (either through the 
built-ins or by generating an XML representation) along with xdmp:to-json.

Some extra color on Stack Overflow <http://stackoverflow.com/a/17707377/563324>.

Justin

Justin Makeig
Director, Product Management
MarkLogic Corporation
[email protected]<mailto:[email protected]>
www.marklogic.com<http://www.marklogic.com/>



On Jul 22, 2013, at 12:22 PM, Steve Spigarelli 
<[email protected]<mailto:[email protected]>>
 wrote:

Thank you for your responses.

I'm trying to write a REST extension that can create a JSON object with some 
objects previously uploaded using the REST interface.

What I see to be the problem is that the REST interface translates the JSON 
into one format, and the json:map and json:array functions generate completely 
different internal XML structures. The files ingested with the REST interface 
require json:transform-to-json and json:map and json:array require xdmp:to-json 
to translate from XML to JSON.

Starting with JSON of {"hello": "world"} I see the following:

REST Interface (after upload):

<json type="object" xmlns="http://marklogic.com/xdmp/json/basic";>
  <hello type="string">world</hello>
</json>

and using json:object() I get:

json:object(
  <json:object xmlns:xs="http://www.w3.org/2001/XMLSchema"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xmlns:json="http://marklogic.com/xdmp/json";>
    <entry key="hello">
      <json:value xsi:type="xs:string">world</json:value>
    </entry>
  </json:object>
)

I would expect them to be the same. Is there a way to convert from one to the 
other?

Thanks
Steve


--
Steve


On Mon, Jul 22, 2013 at 12:47 PM, Danny Sokolsky 
<[email protected]<mailto:[email protected]>> wrote:
You can try looking through this section of the doc for a little more info too:

http://docs.marklogic.com/guide/app-dev/json

-Danny

From: 
[email protected]<mailto:[email protected]>
 
[mailto:[email protected]<mailto:[email protected]>]
 On Behalf Of David Lee
Sent: Monday, July 22, 2013 11:44 AM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] json:object

json:object is the in-memory representation of maps used for json.
The only difference between map:map and json:object is that the keys are 
ordered (which actually is not something JSON guarentees ... but we do).
Your "magic function" is xdmp:to-json().

You dont do a transformation on json:object  TO JSON ... its already in the 
form needed ...  You can turn it into a JSON string using  xdmp:to-json() but 
thats not a transformation.
Transformations occur between XML <-> JSON

Did that answer your question ?



-----------------------------------------------------------------------------
David Lee
Lead Engineer
MarkLogic Corporation
[email protected]<mailto:[email protected]>
Phone: +1 812-482-5224<tel:%2B1%20812-482-5224>
Cell:  +1 812-630-7622<tel:%2B1%20812-630-7622>
www.marklogic.com<http://www.marklogic.com/>

From: 
[email protected]<mailto:[email protected]>
 
[mailto:[email protected]<mailto:[email protected]>]
 On Behalf Of Steve Spigarelli
Sent: Monday, July 22, 2013 2:39 PM
To: [email protected]<mailto:[email protected]>
Subject: [MarkLogic Dev General] json:object

I'm having a difficult time understanding what good the json:object() function 
is. From what I can tell after using it, it is a specialized map:map object. I 
have tried using json:transform-to-json on the object but the transform 
function doesn't like that.

So, what is the proper way to create a json:object and receive back the actual 
JSON string?

For example:

let $json-obj := json:object()
let $_ := map:put($json-obj, "key", "value")

return magic-function($json-obj) which would return {"key": "value"}

What I get instead by calling json:transform-to-json($json-obj) is an invalid 
coercion error. Even wrapping the object and passing the node doesn't work.

Any ideas? Has anyone actually used this function? From the comments in the 
documentation it doesn't have many comments. 
(http://docs.marklogic.com/json:transform-to-json)

--
Steve

_______________________________________________
General mailing list
[email protected]<mailto:[email protected]>
http://developer.marklogic.com/mailman/listinfo/general


_______________________________________________
General mailing list
[email protected]<mailto:[email protected]>
http://developer.marklogic.com/mailman/listinfo/general

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

Reply via email to