This looks to me as a fundamental data model problem.  While it 'works' to some 
extent in JSON,
its often problematic to mix attribute *values* with field/element *names*.


As in this example

        "attributes": [
          "entities/0C4kKs7/attributes/CountryOfOrigin",
...

          "entities/0C4kKs7/attributes/CountryOfOrigin": 
"2016-02-02T12:49:49.539Z",

That data model is a difficult one to work with in any language.
A great deal of programming design patterns in many languages will have a hard 
time with this,
the most common being a mapping of JSON field names to program language 
variables names.

Do you have any ability to change the data model at its source ?
e.g. to use a fixed set of field names/keys  like

    "attributes" : [
              {   "name" :   "entities/0C4kKs7/attributes/CountryOfOrigin" ,
                  "updated" : "2016-02-02T12:49:49.539Z"
             },
         ...
Then this could end in XML like

<attributes>
    <name> entities/0C4kKs7/attributes/CountryOfOrigin</name>
    <updated>: "2016-02-02T12:49:49.539Z"
'...

or


<attributes>
    <name 
updated="2016-02-02T12:49:49.539Z">entities/0C4kKs7/attributes/CountryOfOrigin</name>
    ...
( the latter is probably easier to work with )
It will need some configuration options set from

https://docs.marklogic.com/json:config

to guide the transformation of arrays to repeated elements and fields to 
attributes.


This solves both the XML transformation problem and the next problem you will 
likely encounter -- how to use the data.

---

The alternative solution is to use the "basic" configuration (   json:config() 
) --
That can handle any JSON you give it and produce valid XML that will round trip 
exactly.
What it won't do is give you XML that 'looks nice'

The "custom" strategy is better at controlling XML to JSON conversion then the 
reverse.
The "basic" strategy is 100% round trip from JSON to XML  then back
The "full" strategy is the reverse. It is high fidelity (not quite 100%)  round 
trip form XML to JSON and back.



From: general-boun...@developer.marklogic.com 
[mailto:general-boun...@developer.marklogic.com] On Behalf Of 
santhosh.rajasekar...@cognizant.com
Sent: Friday, February 19, 2016 5:15 PM
To: general@developer.marklogic.com
Subject: Re: [MarkLogic Dev General] Custom JSON objects

Hi All,

                We are trying to convert json to xml. But some of our json key 
contains "/" character in it.
Due to which the transformation (json:transform-from-json()) is failing with 
invalid lexical.

Is there any other option to transform such json.

Is there any other method / custom code to do the transformation to resolve 
this issue.

Error:

XDMP-QNAMELEXFORM: fn:QName("", "entities/0C4kKs7/attributes/CountryOfOrigin") 
-- Invalid lexical form for Qname

Xquery:

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


let $custom := json:config("custom")


let $transform :=

json:transform-from-json('
{
"crosswalks": [
      {
        "uri": "entities/0C4kKs7/crosswalks/1T5YWYSh",
        "type": "configuration/sources/Reltio",
        "value": "0C4kKs7",
        "reltioLoadDate": "2016-02-19T01:01:42.324Z",
        "createDate": "2016-02-02T12:48:43.627Z",
        "updateDate": "2016-02-02T12:48:43.627Z",
        "attributes": [

          "entities/0C4kKs7/attributes/CountryOfOrigin",
          "entities/0C4kKs7/attributes/IsVoid/BaNdOmc",
          "entities/0C4kKs7/attributes/CountryOfOrigin/OaZKd0G",
          "entities/0C4kKs7/attributes/OriginalReleaseYear/1T5YWLfv",
          "entities/0C4kKs7/attributes/MPMNumber/1T5YW0Md",
          "entities/0C4kKs7/attributes/Identifiers/1T5YW8t9/ID/1T5YWHPf",
          
"entities/0C4kKs7/attributes/BoxOffice/1T5YWPwB/DomesticBoxOffice/1T5YWUCR",
          "entities/0C4kKs7/attributes/OriginalMadeFor/1QDXr7WM",
          "entities/0C4kKs7/attributes/BoxOffice/1T5YWPwB",
          "entities/0C4kKs7/attributes/Type/1QDXqqTK",
          "entities/0C4kKs7/attributes/LibraryTitle/1T5YW4ct",
          "entities/0C4kKs7/attributes/CountryOfOrigin/OaZI9Yy",
          "entities/0C4kKs7/attributes/Identifiers/1T5YW8t9/Type/1T5YWD9P",
          "entities/0C4kKs7/attributes/Identifiers/1T5YW8t9"
        ],
        "singleAttributeUpdateDates": {

          "entities/0C4kKs7/attributes/CountryOfOrigin": 
"2016-02-02T12:49:49.539Z",
          "entities/0C4kKs7/attributes/IsVoid/BaNdOmc": 
"2016-02-19T01:01:42.324Z",
          "entities/0C4kKs7/attributes/CountryOfOrigin/OaZKd0G": 
"2016-02-02T13:01:51.933Z",
          "entities/0C4kKs7/attributes/OriginalMadeFor/1QDXr7WM": 
"2016-02-02T12:50:57.215Z",
          "entities/0C4kKs7/attributes/Type/1QDXqqTK": 
"2016-02-02T12:49:09.023Z",
          "entities/0C4kKs7/attributes/CountryOfOrigin/OaZI9Yy": 
"2016-02-02T12:49:34.433Z"
        }
      }
    ]
}
', $custom)

return
$transform



Thanks & Regards,
Santhosh
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.
_______________________________________________
General mailing list
General@developer.marklogic.com
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to