Solved

Perl JSON example

Posted on 2011-03-16
1
1,768 Views
Last Modified: 2012-06-21
Trying to do basic example. Have text, turn to JSON format, take JSON format convert back to text, and print.

What is wrong with this code.

Thanks
#!/usr/bin/perl -w
use JSON -support_by_pp;
   
   
  eval{
    $content = {
name => 'bob',
country => 'US',
age => 34,
sex => 'm'
   
    };
    my $json = new JSON;
   
    # these are some nice json options to relax restrictions a bit:
    my $json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($content);
   
   
    $perl_scalar = from_json( $json_text, { utf8  => 1 } );
 $pretty_printed = $json->pretty->encode( $perl_scalar );
 print " $pretty_printed";
  } ;                                                                                                                                                                                                
   
  # catch crashes:
  if($@){
    print "[[JSON ERROR]] JSON parser crashed! $@\n";
  }

Open in new window

0
Comment
Question by:freshgrill
1 Comment
 
LVL 5

Accepted Solution

by:
group0 earned 500 total points
ID: 35152923
You're trying to decode a non-JSON string.  Change:

    my $json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($content);

Open in new window


to:

    my $json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->encode($content);

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syslog text file into database or into .CSV 19 110
delete query using perl dbi 3 96
Move Function in Perl Script 2 65
Executing multiple sybase statements in perl dbi 2 88
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now