devkits-json-xml
v1.0.3
Published
Convert between JSON and XML formats — bidirectional converter, zero dependencies
Maintainers
Readme
devkits-json-xml
Convert between JSON and XML formats. Bidirectional converter with zero dependencies.
Install
npm install -g devkits-json-xmlUsage
CLI
# JSON to XML
jsonxml to-xml '{"name":"Alice","age":30}'
jsonxml to-xml '{"items":[1,2,3]}' -r data
# XML to JSON
jsonxml to-json '<root><name>Alice</name></root>'
# Custom options
jsonxml to-xml '{"a":1}' -r custom -i 4Programmatic API
const { jsonToXml, xmlToJson } = require('devkits-json-xml');
// JSON to XML
const xml = jsonToXml({ name: 'Alice', age: 30 });
console.log(xml);
// <root>
// <name>Alice</name>
// <age>30</age>
// </root>
// XML to JSON
const json = xmlToJson('<root><name>Alice</name></root>');
console.log(json);
// { root: { name: 'Alice' } }
// Custom root and indent
jsonToXml({ a: 1 }, 'data', ' ');Conversion Rules
JSON → XML
| JSON | XML |
|------|-----|
| {"key": "value"} | <key>value</key> |
| {"key": 123} | <key>123</key> |
| {"key": null} | <key/> |
| {"key": []} | <key/> |
| {"key": [1,2]} | <key><item>1</item><item>2</item></key> |
| Nested objects | Nested elements |
XML → JSON
| XML | JSON |
|-----|------|
| <key>value</key> | { key: "value" } |
| <key>123</key> | { key: 123 } |
| <key/> | { key: "" } |
| <key attr="1"/> | { key: { "@attr": "1" } } |
| <a><b>1</b></a> | { a: { b: 1 } } |
Options
| Option | Alias | Default | Description |
|--------|-------|---------|-------------|
| --root | -r | root | Root element name |
| --indent | -i | 2 | Indentation spaces |
Examples
# Simple object
$ jsonxml to-xml '{"name":"DevKits"}'
<?xml version="1.0" encoding="UTF-8"?>
<root>
<name>DevKits</name>
</root>
# Nested object
$ jsonxml to-xml '{"user":{"name":"Alice","age":30}}'
<?xml version="1.0" encoding="UTF-8"?>
<root>
<user>
<name>Alice</name>
<age>30</age>
</user>
</root>
# Array
$ jsonxml to-xml '{"items":[1,2,3]}'
<?xml version="1.0" encoding="UTF-8"?>
<root>
<items>
<item>1</item>
<item>2</item>
<item>3</item>
</items>
</root>
# XML to JSON
$ jsonxml to-json '<user><name>Alice</name><age>30</age></user>'
{
"user": {
"name": "Alice",
"age": 30
}
}Use Cases
- API integration — Convert between REST (JSON) and SOAP (XML) APIs
- Data migration — Transform legacy XML data to JSON
- Configuration — Convert config files between formats
- Testing — Generate test data in different formats
- ETL pipelines — Process data from XML sources to JSON targets
Features
- Bidirectional — Both JSON→XML and XML→JSON
- Type preservation — Numbers, booleans detected automatically
- Attribute support — XML attributes prefixed with
@ - Nested structures — Full support for nested objects/arrays
- Custom root — Configurable root element name
- Pretty printing — Configurable indentation
Related Tools
🔍 Build something amazing? Check out API Monitor — simple, affordable API monitoring for indie hackers. Get alerted before your customers notice. Just $9/mo.
See Also
- DevKits — All 82 free developer tools in one place
- JSON Formatter — Format and validate JSON
- Base64 Encoder — Encode/decode Base64
- JWT Decoder — Decode JWT tokens
- UUID Generator — Generate UUIDs
- Hash Generator — Create MD5, SHA1, SHA256 hashes
- CSS Tools — CSS minifier, formatter, validator
- HTML Tools — HTML formatter, minifier
- URL Tools — URL encode/decode, parse
- Color Converter — Convert HEX, RGB, HSL colors
- Regex Tester — Test and debug regular expressions
Support DevKits
If you find this tool useful, please consider supporting the project:
Open Collective
Become a sponsor: Open Collective - DevKits
Crypto Donations
Prefer crypto? We accept:
- ETH (Ethereum):
0xDea4a6A20fCB44467e45Ef378972F54B22dC59db - USDC (ERC-20):
0xDea4a6A20fCB44467e45Ef378972F54B22dC59db
Donation Perks:
- $5+ — Supporter Badge
- $10+ — Pro Access (1 month)
- $29+ — Pro Access (1 year)
- $100+ — Lifetime Pro + Priority Support
After donating, email your transaction hash to [email protected] to claim perks.
License
MIT — DevKits Team
