xmljson

Converts XML to JSON and vice versa. The JSON schema used is badgerfish. Badgerfish is used because it is mostly lossless, although there are rare cases where information could be lost in the conversion.

Package Version Hex Docs

gleam add xmljson
import xmljson

pub fn main() {
  let input = "<alice>bob</alice>"

  let json = xmljson.to_json(input)
  // "{ \"alice\": { \"$\" : \"bob\" } }"
  xmljson.to_xml_fragment(json)
  // "<alice>bob</alice>"

  // Multiple values become a list
  let input = "<alice><bob>charlie</bob><bob>david</bob></alice>"
  let json = xmljson.to_json(input)
  // "{ \"alice\": { \"bob\" : [{\"$\": \"charlie\" }, {\"$\": \"david\" }] } }"

  // Attributes are labelled with an @
  let input = "<alice charlie=\"david\">bob</alice>"
  let json = xmljson.to_json(input)
  // "{ \"alice\": { \"$\" : \"bob\", \"@charlie\" : \"david\" } }"
}

Further documentation can be found at https://hexdocs.pm/xmljson.

Development

gleam run   # Run the project
gleam test  # Run the tests
Search Document