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

Version 1 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

 

  • No labels