Convert XML and JSON from the command-line

Save time and run batch tasks to convert between XML and JSON for any number of input documents using the command line tool included with XML ValidatorBuddy.

This page shows in detail how to create the configuration files to convert between XML and JSON from the command line. This allows you to quickly convert any number of JSON and XML files and also to create a scheduled task which runs as a batch process (on your server).

Convert from JSON to XML

As usual the command-line tool needs a configuration XML to run the conversion as batch task. You can specify the folder and any number of file extensions to identify your JSON data. The following sample shows how to create a configuration file to convert all JSON documents located in the folder "D:\Documents\JSON" The batch task will also scan any sub-folders (attribute subfolders="true" of the folder_settings elements). Use the type attribute of the "batch_unit" element and set the value to "json2xml" to run the task for the conversion from JSON to XML.

<?xml version="1.0" encoding="UTF-8"?>
<batch_settings>
    <batch_units>
        <log_document path="D:\Documents\convert_json_log.xml"/>
        <options notify_finish="false" omit_results="false" omit_time="false" run_as_test="false" save_logdoc="true" timeout="-1" use_xmldate_format="true"/>
        <batch_unit output_folder="D:\Documents\XML-converted" overwrite="true" type="json2xml">
            <folder_settings folder="D:\Documents\JSON" subfolders="true"/>
            <file_extensions>
                <extension>json</extension>
            </file_extensions>
        </batch_unit>
    </batch_units>
</batch_settings>
            

Use the file_extensions element to set a list of file extensions which should be included on converting the JSON input.

Once you have created a configuration XML to set the options and parameters for the JSON to XML conversion you can call the command line tool in the following way. The name of the executable is "valbuddy.exe" and it is located in the standard installation folder of XML ValidatorBuddy:

valbuddy.exe myJSON2XMLconfig.xml

If you don’t specify an output folder using the "output_folder" attribute, the resulting XML files are written next to the JSON input documents. Any folder structure from the source directory is also copied to the output folder. You can also set the overwrite attribute to overwrite="true" or "false" of the "batch_unit" element to control if any already existing output documents are replaced.

Convert from XML to JSON

Use the command-line tool in a similar way to convert from XML to JSON:

<?xml version="1.0" encoding="UTF-8"?>
<batch_settings>
    <batch_units>
        <log_document path="D:\Documents\convert_xml_json_log.xml"/>
        <options notify_finish="false" omit_results="false" omit_time="false" run_as_test="false" save_logdoc="true" timeout="-1" use_xmldate_format="true"/>
        <batch_unit output_folder="D:\Documents\JSON-converted" overwrite="true" type="xml2json">
            <folder_settings folder="D:\Documents\XML" subfolders="true"/>
            <file_extensions>
                <extension>xml</extension>
            </file_extensions>
        </batch_unit>
    </batch_units>
</batch_settings>

Set the value of the type attribute of the "batch_unit" element to "xml2json" to convert XML data to JSON.

Copyright © 2017, xml-buddy.com, Vienna