https://github.com/bennyhat/xml_json
Should you convert XML to JSON? Probably not. If you have to though, try a convention based converter.
https://github.com/bennyhat/xml_json
convention json parker xml
Last synced: 9 months ago
JSON representation
Should you convert XML to JSON? Probably not. If you have to though, try a convention based converter.
- Host: GitHub
- URL: https://github.com/bennyhat/xml_json
- Owner: bennyhat
- License: mit
- Created: 2020-10-04T01:47:25.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-02-10T20:49:37.000Z (over 1 year ago)
- Last Synced: 2025-04-05T20:11:18.503Z (about 1 year ago)
- Topics: convention, json, parker, xml
- Language: Elixir
- Homepage:
- Size: 63.5 KB
- Stars: 6
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# XmlJson
[](https://hex.pm/packages/xml_json)
[](https://hex.pm/packages/xml_json)
[](https://hex.pm/packages/xml_json)
[](https://github.com/bennyhat/xml_json)
Should you convert XML to JSON? Probably not.
If you have to though (and have decent control over the providers and consumers
of the XML and JSON), then there are some decent conventions out there for
lossless and near-lossless conversion, such as:
- [`abdera`](http://wiki.open311.org/JSON_and_XML_Conversion/#the-abdera-convention)
- [`badgerfish`](http://www.sklar.com/badgerfish/)
- [`cobra`](http://wiki.open311.org/JSON_and_XML_Conversion/#the-cobra-convention)
- [`gdata`](http://wiki.open311.org/JSON_and_XML_Conversion/#the-gdata-convention)
- [`parker`](https://developer.mozilla.org/en-US/docs/Archive/JXON#The_Parker_Convention) (pretty lossy, but my personal favorite)
- [`yahoo`](https://developer.yahoo.com/yql/guide/response.html#response-xml-to-json) (okay, maybe they're not all great, but they tried)
- [`aws-api`](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/making-api-requests.html) (conventions seen in AWS XML based APIs)
Presently this only supports Parker, BadgerFish and AWS API and is largely happy path
testing with the examples provided by each convention. That being said,
eventually this will come up to a `> 0` major version when that is all worked
out.
Otherwise, this does NOT handle massive XML documents with grace, as it
converts to a full JSON/Map object in memory.
A port of the great Python library [`xmljson`](https://pypi.org/project/xmljson/)
## Usage
### Parker
```elixir
iex> XmlJson.Parker.deserialize("cat", preserve_root: true)
{:ok, %{"root" => %{"dog" => "cat"}}}
iex> XmlJson.Parker.serialize(%{"root" => %{"dog" => "cat"}}, preserve_root: "root")
{:ok, "cat"}
```
### BadgerFish
```elixir
iex> XmlJson.BadgerFish.deserialize("cat")
{:ok, %{"root" => %{"@attr" => "hello", "dog" => %{"$" => "cat"}}}}
iex> XmlJson.BadgerFish.serialize(%{"root" => %{"@attr" => "hello", "dog" => %{"$" => "cat"}}})
{:ok, "cat"}
```
### AWS API
Based on common conventions seen in at least the EC2 and ELBv2 XML APIs
```elixir
iex> XmlJson.AwsApi.deserialize("cat")
{:ok, %{"root" => [%{"dog" => "cat"}]}}
iex> XmlJson.AwsApi.serialize(%{"root" => [%{"dog" => "cat"}]})
{:ok, "cat"}
iex> XmlJson.AwsApi.serialize_as_params(%{"root" => [%{"dog" => "cat"}, %{"dog" => "horse"}]})
{:ok, %{"root.member.1.dog" => "cat", "root.member.2.dog" => "horse"}}
```
## Installation
The package can be installed by adding `xml_json` to your list of dependencies
in `mix.exs`:
```elixir
def deps do
[
{:xml_json, "~> 0.5.0"}
]
end
```
The docs can be found at [https://hexdocs.pm/xml_json](https://hexdocs.pm/xml_json).
## License
[MIT](LICENSE) Copyright (c) 2020 Ben Brewer