Json format to represent soap: Difference between revisions

(How to translate xml soap calls to JSON and visceversa)
 
No edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Learn how to translate a soap request to JSON'''
{{Archive}}{{Unsupported}}
 
=Learn how to translate a soap request to JSON=


Zimbra has a special way to represent soap xml in JSON.  
Zimbra has a special way to represent soap xml in JSON.  
Line 5: Line 7:
Here is the documentation of how i've learned it works.
Here is the documentation of how i've learned it works.


Example #1:
==Example #1==


<root>
<root>
     <name username="fernandoflorez">Fernando</name>
     <name username="fernandoflorez">Fernando</name>
</root>
</root>


Translating it to json:
Translating it to json:
Line 15: Line 17:
JSON structures always start being an object:
JSON structures always start being an object:


{}
{}


Nodes of type 3 (element nodes) are objects:
Nodes of type 3 (element nodes) are objects:


{root: {}} -> <root />
{root: {}} -> <root />
 
{root: {name: {}}} -> <root><name /></root>
{root: {name: {}}} -> <root><name /></root>


Object properties are changed to node attributes if they are of a type Number or String:
Object properties are changed to node attributes if they are of a type Number or String:


{root: {name: {username: "fernandoflorez"}}} -> <root><name username="fernandoflorez" /></root>
{root: {name: {username: "fernandoflorez"}}} -> <root><name username="fernandoflorez" /></root>


Nodes of type 1 (text nodes) are represented by the reserved "_content" parameter name:
Nodes of type 1 (text nodes) are represented by the reserved "_content" parameter name:


{root: {name: {username: "fernando", _content: "Fernando"}}}
{root: {name: {username: "fernando", _content: "Fernando"}}}


That's it for Example #1.
That's it for Example #1.




Example #2:
==Example #2==


<users>
<users>
    <user name="fernando"/>
    <user name="fernando"/>
    <user name="frank"/>
    <user name="frank"/>
</users>
</users>


If an element node has different siblings of the same node name then the object is translated into an array of object:
If an element node has different siblings of the same node name then the object is translated into an array of object:


{users: {user: [{name: "fernando"}, {name: "frank"}]}}
{users: {user: [{name: "fernando"}, {name: "frank"}]}}




Example #3:
==Example #3==


<users xmlns="urn:namespace">
<users xmlns="urn:namespace">
     <user name="fernando"/>
     <user name="fernando"/>
     <user name="frank"/>
     <user name="frank"/>
</users>
</users>


Node namespaces are also represented by a reserved parameter name: "_jsns"
Node namespaces are also represented by a reserved parameter name: "_jsns"


{users: {_jsns: "urn:namespace", user: [{name: "fernando"}, {name: "frank"}]}}
{users: {_jsns: "urn:namespace", user: [{name: "fernando"}, {name: "frank"}]}}




Line 61: Line 63:




'''Suggestions for improvement:'''
=Suggestions for improvement=


There is no way to reconvert from json to xml and mantain the same data structure order. For example if we translate this json:
There is no way to reconvert from json to xml and mantain the same data structure order. For example if we translate this json:


{users: {_jsns: "urn:namespace", user: [{name: "fernando"}, {name: "frank"}]}}
{users: {_jsns: "urn:namespace", user: [{name: "fernando"}, {name: "frank"}]}}


There is no way to be 100% sure it will be converted to this because of how loops in ECMA script are performed.
There is no way to be 100% sure it will be converted to this because of how loops in ECMA script are performed.


<users xmlns="urn:namespace">
<users xmlns="urn:namespace">
     <user name="fernando"/>
     <user name="fernando"/>
     <user name="frank"/>
     <user name="frank"/>
</users>
</users>


A solution for this would be to have an optional reserved parameter name "_o" that will have a zero index based value (int).
A solution for this would be to have an optional reserved parameter name "_o" that will have a zero index based value (int).


--[[User:Fernandoflorez|Fernandoflorez]] 19:07, 22 February 2009 (UTC)
--[[User:Fernandoflorez|Fernandoflorez]] 19:07, 22 February 2009 (UTC)
{{Article Footer|unknown|2/22/2009}}
[[Category:SOAP]]

Latest revision as of 13:21, 24 March 2015


Learn how to translate a soap request to JSON

Zimbra has a special way to represent soap xml in JSON.

Here is the documentation of how i've learned it works.

Example #1

<root>
   <name username="fernandoflorez">Fernando</name>
</root>

Translating it to json:

JSON structures always start being an object:

{}

Nodes of type 3 (element nodes) are objects:

{root: {}} -> <root />

{root: {name: {}}} -> <root><name /></root>

Object properties are changed to node attributes if they are of a type Number or String:

{root: {name: {username: "fernandoflorez"}}} -> <root><name username="fernandoflorez" /></root>

Nodes of type 1 (text nodes) are represented by the reserved "_content" parameter name:

{root: {name: {username: "fernando", _content: "Fernando"}}}

That's it for Example #1.


Example #2

<users>
    <user name="fernando"/>
    <user name="frank"/>
</users>

If an element node has different siblings of the same node name then the object is translated into an array of object:

{users: {user: [{name: "fernando"}, {name: "frank"}]}}


Example #3

<users xmlns="urn:namespace">
   <user name="fernando"/>
   <user name="frank"/>
</users>

Node namespaces are also represented by a reserved parameter name: "_jsns"

{users: {_jsns: "urn:namespace", user: [{name: "fernando"}, {name: "frank"}]}}


Just if you wonder, why all reserved parameter names start with an underscore character? Thats because underscores at the beginning of the word are not xml valid names so it's a way to avoid name conflicts.


Suggestions for improvement

There is no way to reconvert from json to xml and mantain the same data structure order. For example if we translate this json:

{users: {_jsns: "urn:namespace", user: [{name: "fernando"}, {name: "frank"}]}}

There is no way to be 100% sure it will be converted to this because of how loops in ECMA script are performed.

<users xmlns="urn:namespace">
   <user name="fernando"/>
   <user name="frank"/>
</users>

A solution for this would be to have an optional reserved parameter name "_o" that will have a zero index based value (int).

--Fernandoflorez 19:07, 22 February 2009 (UTC)


Verified Against: unknown Date Created: 2/22/2009
Article ID: https://wiki.zimbra.com/index.php?title=Json_format_to_represent_soap Date Modified: 2015-03-24



Try Zimbra

Try Zimbra Collaboration with a 60-day free trial.
Get it now »

Want to get involved?

You can contribute in the Community, Wiki, Code, or development of Zimlets.
Find out more. »

Looking for a Video?

Visit our YouTube channel to get the latest webinars, technology news, product overviews, and so much more.
Go to the YouTube channel »

Jump to: navigation, search