
JSON and XML are to formats commonly used to represent structured data in a text file. Sometimes, data exported from a tool in JSON format needs to be imported in a different tool that only accepts XML as input.
This post explain how to perform the conversion from JSON to XML using a perl script.
1. Reading a JSON file in perl
In perl, reading a JSON file is easily done using the decode_json()
routine from the JSON module, as already explained in our previous post: How to read and write JSON files in Perl
2. Writing a XML file in perl
Writing an XML file is also easy in perl, using XML::Simple
module. We have also covered reading and writing XML files in perl in our posts:
How to read and write XML files in Perl using XML::Simple
and
How to read and write XML files in perl using XML::Parse::PerlSAX
3. Converting from JSON to XML
Using the information in the previous posts, a script to convert from JSON to XML can be written as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#!/usr/bin/perl use strict; use warnings; binmode STDOUT, ":utf8"; use utf8; use JSON; use XML::Simple; # Read input file in json format my $json; { local $/; #Enable 'slurp' mode open my $fh, "<", "data.json"; $json = <$fh>; close $fh; } # Convert JSON format to perl structures my $data = decode_json($json); # Output as XML print "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; print XMLout($data); print "\n"; |
4. Testing the script
To verify that the script is working as expected, a sample input file “data.json” in JSON format can be used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
{ "boss": { "Name" : "John", "Age": 28, "Hobbies": ["Music", "Movies", "Tennis"], "Residence": "Birmingham" }, "employees": [ { "Name" : "Hellen", "Age": 26, "Hobbies": ["Movies", "Tennis"], "Residence": "Birmingham" }, { "Name" : "Richard", "Age": 31, "Hobbies": ["Football"], "Residence": "Birmingham" } ] } |
The output from the script should be the following data in XML format:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <opt> <boss Age="28" Name="John" Residence="Birmingham"> <Hobbies>Music</Hobbies> <Hobbies>Movies</Hobbies> <Hobbies>Tennis</Hobbies> </boss> <employees Age="26" Name="Hellen" Residence="Birmingham"> <Hobbies>Movies</Hobbies> <Hobbies>Tennis</Hobbies> </employees> <employees Age="31" Name="Richard" Residence="Birmingham"> <Hobbies>Football</Hobbies> </employees> </opt> |
—
Index of posts related to perl programming
—
Hi,
I have to read newline delimited JSON object.
ie. one interaction per line.
something like this.
{“id”:1,”name”:”Smith”,”tags”:[“agent”,”program”]}
{“id”:2,”name”:”Neo”,”tags”:[“whoa”,”knows kungfu”]}
{“id”:3,”name”:”Egon”,”tags”:[“streams”,”wrong movie”]}
I have getting one single json file which contain newline delimited JSON object. Can you please help how to read the JSON file and convert into XML
Thanks,
Neeraj
Hi again, Neeraj
The easiest way is to convert the input file “data.json_not_ok” into valid JSON, and then perform the conversion as explained in the post mentioned above.
On a linux system, The sample data you gave can be converted to valid JSON with the command:
(echo "["; sed -e '$ ! s/$/,/' data.json_not_ok; echo "]") > data.json
The output from this command will be a correct JSON-formatted file data.json:
[
{"id":1,"name":"Smith","tags":["agent","program"]},
{"id":2,"name":"Neo","tags":["whoa","knows kungfu"]},
{"id":3,"name":"Egon","tags":["streams","wrong movie"]}
]