9

I want to use Perl script that gets the JSON data and converts it into XML file. How can I do that in Perl?

3
  • 3
    json.org links to search.cpan.org/search?query=JSON Commented Oct 9, 2009 at 10:38
  • 1
    Don't put in jokey tags. It makes it a lot harder for people to find questions and answers. Commented Oct 9, 2009 at 11:11
  • 3
    @Alex Reynolds: Some questions deserve answers and some do not. Specifically, send-me-teh-codez type questions do not. Sometimes, however, the question can be salvaged. In this case, after a second consideration, I decided to fix up the question. In the future, you may also want to fix the question as well as editing the tags. As for my attitude as to when a question should be fixed versus left to languish, see meta.stackexchange.com/questions/24838/… Commented Oct 9, 2009 at 13:23

2 Answers 2

14
use JSON;

my $json_string = '................';

my $deserialized = from_json( $json_string );

That's all - your JSON data is parsed and stored in $deserialized.

Sign up to request clarification or add additional context in comments.

2 Comments

If performance is at all a concern, also install JSON::XS (JSON will use it if it's there...no code changes needed).
Well, sure it doesn't. There is no information how the xml should look (for example: what should be attribute, and what tag), so it's impossible to build xml out of data without schema specification. If schema is "any, just make it valid" - use XML::Simple; print XMLout( $deserialized );
10

Install: XML::XML2JSON with

sudo cpan XML::XML2JSON

and then try:

use XML::XML2JSON;
my $JSON = '{"entry":{"name":"Douglas Crockford","phone":"555 123 456"}}';
my $XML2JSON = XML::XML2JSON->new();
my $Obj = $XML2JSON->json2obj($JSON);
my $XML = $XML2JSON->obj2xml($Obj);
print $XML;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.