Convert between XML and JSON using the BadgerFish specification
Find a file
Image Edwin van Leeuwen 6a9b174cd2
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Merge tag 'v0.2.0' into develop
Move from simplejson to gleam/json
2024-12-21 20:54:01 +00:00
src test: Fix the test so it does not rely on json keys order being maintained 2024-12-21 20:50:05 +00:00
test test: Fix the test so it does not rely on json keys order being maintained 2024-12-21 20:50:05 +00:00
.gitignore feat: Initial commit 2024-12-15 17:51:19 +00:00
.woodpecker.yaml ci: Add woodpecker ci 2024-12-21 16:28:37 +00:00
gleam.toml release: Update version to 0.2.0 2024-12-21 20:53:07 +00:00
LICENSE feat: Initial commit 2024-12-15 17:51:19 +00:00
README.md docs: Small clarification in the README 2024-12-21 13:13:04 +00:00

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