Solved

Perl JSON example

Posted on 2011-03-16
1
1,759 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

22 Experts available now in Live!

Get 1:1 Help Now