xml-json-normalize
v1.1.0
Published
π A CLI tool that converts XML to sorted JSON and allows **comparing two XML files**.
Readme
xml-json-normalize
π A CLI tool that converts XML to sorted JSON and allows comparing two XML files.
Features
β
Convert XML to JSON
β
Sorts object keys alphabetically for deterministic comparison
β
Compare two XML files and show differences
β
Compact one-line diff output (default)
β
Detailed JSON diff output with --detailed flag
β
Automatically names the output file if none is provided
β
Prevents overwriting input files
β
Works with nested objects
π₯ Installation
Global Install (Recommended)
npm install -g xml-json-normalizeLocal Install (For Project Usage)
npm install --save-dev xml-json-normalizeπ Usage
1οΈβ£ Convert XML to JSON
xml-json-normalize convert <input.xml> [output.json]- If no output file is provided, it automatically saves as
input.jsonin the same directory.
Example
Input (input.xml)
<root>
<b>2</b>
<a>
<z>9</z>
<y>8</y>
</a>
<c>
<d>4</d>
<a>1</a>
</c>
</root>Command
xml-json-normalize convert input.xmlOutput (input.json)
{
"root": {
"a": {
"y": 8,
"z": 9
},
"b": 2,
"c": {
"a": 1,
"d": 4
}
}
}2οΈβ£ Compare Two XML Files
xml-json-normalize diff <file1.xml> <file2.xml>- This compares two XML files after converting them to sorted JSON and prints the differences.
Example
File 1 (file1.xml)
<root>
<a>1</a>
<b>2</b>
</root>File 2 (file2.xml)
<root>
<a>1</a>
<b>3</b>
<x>New Value</x>
</root>Command
xml-json-normalize diff file1.xml file2.xmlOutput (Compact Default)
π Compact Differences:
E | Edited: root.b | Old: 2 β New: 3
N | Added: root.x | Value: "New Value"E= EditedD= DeletedN= AddedA= Array Change
3οΈβ£ Compare Two XML Files (Detailed JSON Output)
xml-json-normalize diff file1.xml file2.xml --detailedThis prints the full structured JSON diff.
Example Output
[
{
"kind": "E",
"path": ["root", "b"],
"lhs": 2,
"rhs": 3
},
{
"kind": "N",
"path": ["root", "x"],
"rhs": "New Value"
}
]4οΈβ£ Prevents Overwriting Input Files
xml-json-normalize convert myfile.xml myfile.xmlβ Error: Prevents overwriting myfile.xml with JSON.
π Development
Clone the Repository
git clone https://github.com/yourusername/xml-json-normalize.git
cd xml-json-normalizeInstall Dependencies
npm installBuild the Project
npm run buildRun Locally
node dist/index.js convert input.xml
node dist/index.js diff file1.xml file2.xmlπ Contributing
- Fork the repo
- Create a new branch (
feature/your-feature) - Commit your changes
- Push to your fork
- Create a Pull Request
π License
This project is licensed under the MIT License. See the LICENSE file for details.
π Like this project? Give it a star! β
