Page 1 of 1

Bulk Convert XML to JSON ?

Posted: Sun May 06, 2012 9:14 am
by sfguy
I have thousands of xml files in a folder that I need to convert to json files. However, the software only allows you to convert one at a time. Is there a script that can be run to accomplish this - bulk converting an entire folder of xml files to json?

Thanks in advance.

Re: Bulk Convert XML to JSON ?

Posted: Mon May 07, 2012 12:22 pm
by adrian
Hello,

Oxygen does not yet provide batch conversion from XML to JSON (v13.2). We plan to implement this support in a future version. I've added your vote for this feature request.

A possible workaround would be to use an XSLT stylesheet for converting to JSON, like this one:
http://www.bramstein.com/projects/xsltjson/
You can create a transformation scenario in Oxygen that uses "xml-to-json.xsl" and apply a batch transformation on your XML files. Use an XSLT 2.0 transformer: Saxon-HE/PE/EE
Make sure you specify an output file in the "Save As" field.
e.g. ${cfd}/json-out/${cfn}.json

Note that since this is XSLT based, if the input XML has a DOCTYPE declaration with a DTD that contributes to the XML model (e.g. attributes with default values), this will be reflected in the JSON output. This means that the content implied by the DTD (though not mentioned explicitly in the XML) will also be included in the resulting JSON file, so the conversion is actually XML+DTD to JSON.
I'm mentioning this because it comes as a surprise for some people.

Regards,
Adrian

Re: Bulk Convert XML to JSON ?

Posted: Tue Apr 22, 2014 8:04 pm
by tomjohnson1492
I'm just wondering if the bulk converter from XML to JSON was ever built. I'm also interested in this capability.

Re: Bulk Convert XML to JSON ?

Posted: Wed Apr 23, 2014 12:38 pm
by adrian
Hi,

No, this capability has not yet been built into Oxygen. We have this feature request logged on our issue tracking tool. I've added your vote.

Note that it's very easy to configure the XSLT transformation scenario (XML transformation with XSLT) that I mentioned in my previous post. You can then run a batch XSLT transformation with that scenario and have the same result as a batch XML to JSON conversion.
Let me know if you need more details about how this should be configured and used in Oxygen.

By using that XSL stylesheet you can actually run the batch XML to JSON conversion independently from Oxygen (from the command line, scripts, etc).

Regards,
Adrian

Re: Bulk Convert XML to JSON ?

Posted: Wed Apr 30, 2014 8:34 pm
by tomjohnson1492
Thanks Adrian. If you could provide more details about how to configure the batch XML to JSON conversion in Oxygen, that would be helpful to me.

One problem with the way the current conversion works is that the order of elements in the body gets rearranged. All the li elements are grouped together, the p elements grouped together, the heading elements grouped together, and so on, regardless of their actual order in the body. Is there anyway to leave the body field alone in the JSON export?

Re: Bulk Convert XML to JSON ?

Posted: Fri May 02, 2014 5:41 pm
by adrian
Hi,

You mentioned a problem with the order of the elements. What type of XML content are you converting to JSON?

Follow these instructions to configure the XML to JSON transformation scenario in Oxygen:
1. Open an XML file (one that you would want to convert to JSON).
2. From the main menu invoke Document -> Transformation -> "Configure Transformation Scenario(s)" (there's a corresponding button in the toolbar)
3. In the "Configure Transformation Scenario(s)" dialog press New and select "XML transformation with XSLT" to create a new scenario.
4. Give it an appropriate name (e.g. XML to JSON).
5. Leave the XML URL field to its default value (${currentFileURL}). This allows you to apply it on multiple XML files.
6. In the XSL URL field browse for the xml-to-json.xsl stylesheet you have saved from github.
7. In the Transformer combo select Saxon-PE (for XSLT 2.0).
8. In the Output tab specify an output file in the "Save As" field. Please note that the "Save as" field must refer to a single file, NOT an output directory. Use the editor variables to compose a generic name instead of a fixed one.
e.g. ${cfd}/json-out/${cfn}.json
This will create in the folder where the XML (${cfd}) is located a sub-folder named 'json-out' and place the output in a file of the same name as the input file (${cfn}) and the '.json' extension.
9. Press OK in the editing dialog and "Save and close".
10. To apply this scenario to several XML files (batch) first add them (or their parent folder) to your Oxygen project, select them (or their folder) in the Project view and from the contextual menu (right click) choose Transform -> "Transform with...", then select your scenario from the list and press "Apply selected scenarios".
The transformation scenario will be applied by Oxygen separately for each XML file.

Regards,
Adrian

Re: Bulk Convert XML to JSON ?

Posted: Fri May 02, 2014 10:59 pm
by tomjohnson1492
Thanks for posting the detailed instructions. They were really clear and easy to follow. Very helpful.

Re: Bulk Convert XML to JSON ?

Posted: Fri Nov 03, 2017 12:04 pm
by Radu
Hi,

As an update, if you are using Oxygen 19.1 and newer you can install a new add-on we developed:

https://github.com/oxygenxml/oxygen-resources-convertor

which allows for batch conversions between various file types, including XML and JSON.

Regards,
Radu