[XQuery] How to Convert from JSON Message to XML Message with XQuery

Hi guys,

I'm in a job of converting a restful webservice to soap. Tool for convertation uses XQuery.

Now i need to convert a message like this:
{
"firstName": "John",
"midName": null,
"lastName": "Smith",
"married": false,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [ "212 555-1234", "646 555-4567" ]
}
is equivalent to the following XML representation:

<pair name="firstName" type="string">John</pair>
<pair name="midName" type="null"/>
<pair name="lastName" type="string">Smith</pair>
<pair name="married" type="boolean">false</pair>
<pair name="address" type="object">
<pair name="streetAddress" type="string">21 2nd Street</pair>
<pair name="city" type="string">New York</pair>
<pair name="state" type="string">NY</pair>
<pair name="postalCode" type="number">10021</pair>
</pair>
<pair name="phoneNumbers" type="array">
<item type="string">212 555-1234</item>
<item type="string">646 555-4567</item>
</pair>

I guest this a general case so there must be an example for this somewhere on the internet but i haven't found it yet. So i put on forum and hope someone already knows this.

Thank for u help.

Regards,
Tien86.

You can use an example distributed with Sausalito (28msec.com). Install the coresdk and execute:

sausalito create template -n json

then you can see and play with some json conversions in json/handlers/doublesearch.xq (online:28msec.com/source/show/json/handlers/doublesearch.xq) and json/lib/controller/search.xq (online:28msec.com/source/show/json/lib/controller/search.xq).

In particular, what you need is the parse function from this module 28msec.com/api/reference/datatypes/json#parse-1

Sorry, for posting that many links, but I hope it helps.

hi, but my environment is OSB, it looks like it just offer only xquery to parse the message, i don't know whether i can reference to another source :frowning:

Mmmh...

Is the restful webservice you are trying to integrate on the web?

Then you could use sausalito to do the transformation job as you can deploy it in the cloud. You could then access the SOAP wrapped app in the cloud like any other service on the web.

there is a user group where you could post that question if this might be a solution: groups.google.com/group/sausalito-users

hope that helps. I am not familiar with OSB, so, I am sorry if this doesn't help...

well i will do it in easier way for my hurry job...

Now, in above example, i can make a loop to get the value column.

John
null
Smith
false
21 2nd Street
....
//in here $string is the restful input message

for $c in fn:tokenize($string, ",") where fn:string-length($c) != 0
let $cut := fn:tokenize($c, ":")[last()]
let $value := fn:replace($cut,"\{|\}","")
let $seq1 := fn:insert-before($seq1,1,$value)
return <param value="{ $value }" />

====================================

So what i want to do now is : create a $seq with value is ("John", "null", "Smith" .... ) . This code is not work. Would you please take a look at my code?

Tien86

hi all thank you for watching, OSB provide a method using java callout. So i have solved this problem by java insted of using XQuery