Xbasic

json_to_xml Function

Syntax

c xml = json_to_xml(json as C [, attribute as C [, format as C ]])

Arguments

jsonCharacter

attributeCharacter

formatCharacter

Format to use when generating the XML.

Description

json_to_xml Function - Convert JSON to XML - Takes a string of JSON data and converts it to XML. json_to_xml Function - ATOM Format - The json_to_xml function can now generate XML using the "atom" format:

Example 

dim json as c 
json = <<%txt%
{ "firstname" : "john" ,
    "lastname" : "public" ,
    "num" : [1,2,3] ,
    "flag" : true ,
    "place" : { "state" : "Texas"} } 
%txt%

dim xml as c
xml = json_to_xml(json, "person")
showvar(xml)

The resulting XML string looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<person>
   <firstname>john</firstname>
   <lastname>public</lastname>
   <num>1</num>
   <num>2</num>
   <num>3</num>
   <flag>true</flag>
   <place>
      <state>Texas</state>
   </place>
</person>

Formatting XML in ATOM Format 

XML can also be formatted by specifying an optional third parameter, format. Consider the following example:

js = <<%str%
{
    "OrderId":10248,
    "ProductID":11,
    "UnitPrice":14,
    "Quantity":12,
    "Discount":0
}
%str%

dim xml as c 
xml = json_to_xml(js)
showvar(xml)

This generates xml using the default encoding:

<?xml version="1.0" encoding="UTF-8"?>
<data>
   <OrderId>10248</OrderId>
   <ProductID>11</ProductID>
   <UnitPrice>14</UnitPrice>
   <Quantity>12</Quantity>
   <Discount>0</Discount>
</data>

To convert the JSON to xml using the atom format, the format is defined and passed into the json_to_xml function:

dim format as c 
format = <<%str%
{ "format" : "atom"}
%str%

dim xml as c = json_to_xml(js,"",format)
showvar(xml)

The resulting XML is in Atom format:

<?xml version="1.0" encoding="UTF-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
  <atom:content type="application/xml">
     <m:properties>
         <d:OrderId>10248</d:OrderId>
         <d:ProductID>11</d:ProductID>
         <d:UnitPrice>14</d:UnitPrice>
         <d:Quantity>12</d:Quantity>
         <d:Discount>0</d:Discount>
     </m:properties>
  </atom:content>
</atom:entry>

Here is another example that demonstrates generating xml using the Atom format:

js = <<%str%
{
    "OrderId":10248,
    "ProductID":11,
    "UnitPrice":14,
    "Quantity":12,
    "Discount":0
}
%str%

dim xml as c 
xml = json_to_xml(js)
? xml
= <?xml version="1.0" encoding="UTF-8"?>
<data>
   <OrderId>10248</OrderId>
   <ProductID>11</ProductID>
   <UnitPrice>14</UnitPrice>
   <Quantity>12</Quantity>
   <Discount>0</Discount>
</data>

dim format as c 
format = <<%str%
{ "format" : "atom"}
%str%

dim xml as c = json_to_xml(js,"",format)
?xml
= <?xml version="1.0" encoding="UTF-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
  <atom:content type="application/xml">
     <m:properties>
         <d:OrderId>10248</d:OrderId>
         <d:ProductID>11</d:ProductID>
         <d:UnitPrice>14</d:UnitPrice>
         <d:Quantity>12</d:Quantity>
         <d:Discount>0</d:Discount>
     </m:properties>
  </atom:content>
</atom:entry>

See Also