This is a simple C++ program that converts a JSON file to an XML file. It uses:
nlohmann/json– for parsing JSONpugixml– for writing XML
You can install both libraries via Homebrew on macOS:
brew install nlohmann-json pugixmlAlternatively, you can use the source files directly (see below).
g++ -std=c++17 main.cpp -o json2xml \
-I/opt/homebrew/opt/nlohmann-json/include \
-I/opt/homebrew/opt/pugixml/include \
-L/opt/homebrew/opt/pugixml/lib -lpugixml-
Download:
-
Place them in the same folder as
main.cpp. -
Compile:
g++ -std=c++17 main.cpp pugixml.cpp -o json2xml./json2xml input.json output.xmlExample:
./json2xml sample.json result.xml{
"name": "Ashkan",
"age": 28,
"skills": ["C++", "Python", "XML"]
}<root>
<name>Ashkan</name>
<age>23</age>
<skills>
<item>C++</item>
<item>Python</item>
<item>XML</item>
</skills>
</root>- Arrays are written as repeated
<item>nodes — you can change it in the code. - The root XML element is always named
<root>— you can change it in the code. - For large files, this implementation loads everything into memory. For massive JSON files, consider a streaming parser (e.g. cuJSON, simdjson).