Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

The this2that library provides utility functions for converting from one object to another

General Functions

xml2json

xml2json(<string>|,<map>) - convert a string of XML to JSON

  jstr = this2that:xml2json("<foo type=\"bar\">XML element</foo>");
result: {  "@encoding":"UTF-8",
  "@version":"1.0",
  "foo":{
   "$t":"XML element",
   "@type":"bar"}
}
 

Through optional parameters, some control of the resultant JSON is possible

Given:

<sample_dataset>
        <seed name="Strawberries" type="fruit">
                <harvest_time>4 Hours</harvest_time>
                <cost type="coins">10</cost>
                <xp>1</xp>
        </seed>
</sample_dataset>

With attribute_prefix you can change the attribute prefix from the default '$'

jstr = this2that:xml2json(xmlstring,{
	'attribute_prefix' : '_:_'
});
result: {
  "_O_encoding":"UTF-8",
  "sample_dataset":{
    "seed":{
      "cost":{
        "$t":"10",
        "_O_type":"coins"
      },
      "xp":{
         "$t":"1"
      },
      "_O_name":"Strawberries",
      "harvest_time":{
        "$t":"4 Hours"
      },
      "_O_type":"fruit"
    }
  },
  "_O_version":"1.0"
 }

A similar option is available to change the content key

jstr = this2that:xml2json(xmlstring,{
	'content_key' : '-t-'
});

result: {
  "cost":{
    "-t-":"10",                  
    "@type":"coins"

  } ....
}

Other parameters include

  • empty_elements
  • private_attributes
  • private_elements

Each of these requires an array the fields that you desire to either suppress the value or the entire element

Base64

string2base64

string2base64(<string>|,<eol>)

Specifying an end-of-line character will format the base64 string into 74 character blocks

b64 = this2that:string2base64("the rain in spain","\n")

Omitting the <eol> character will return an unformatted base64 string

base642string

txt = this2that:base642string("U3VwZXJEdXBlcjogYXNjaWk=")

url2base64

The url-safe base64 conversions use "-" and "_" instead of "+" and "/"

url64 = this2that:url2base64("3+4/7 = 1");

base642url

urltxt = this2that:base642url("Mys0LzcgPSAx");
  • No labels