Introduction to JSON in Perl

Discussion in 'Perl' started by pradeep, Nov 25, 2011.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    JSON has become a popular data representation format, and can been seen as the successor of XML which used to be the most popular format used for data exchange between systems. Notable examples of JSON being used for data representation are Facebook Graph API & Flickr API, amongst many others. Javascript frameworks like jQuery & and the likes have in-built support for automatic handling of JSON responses in AJAX calls.

    JSON Parsing in Perl



    Like most of the Perl modules JSON module is found on CPAN, which under the hood has a C API (JSON::XS) as well as Pure-Perl API (JSON:PP) which is used in case the C-API is unavailable, the C API being faster of the two.

    Now let's see an example:

    Open http://graph.facebook.com/pradeep.deepz in your web browser and you'll get the following content.

    Code:
    {
       "id": "703880701",
       "name": "Pradeep Deepz",
       "first_name": "Pradeep",
       "last_name": "Deepz",
       "link": "http://www.facebook.com/pradeep.deepz",
       "username": "pradeep.deepz",
       "gender": "male",
       "locale": "en_IN"
    }
    
    Here is the code to parse the JSON and print name & locale.

    Code:
    use strict;
    use warnings;
    use LWP::Simple;
    use JSON;
    
    my $json_data = get(q[http://graph.facebook.com/pradeep.deepz]);
    my $json_obj = new JSON;
    my $perl_data = $json_obj->decode($json_data);
    
    printf('Name: %s & locale: %s',$perl_data->{name},$perl_data->{locale})
    
    Output:
    Code:
    Name: Pradeep Deepz & locale: en_IN
    

    JSON generation in Perl



    Like we parse JSON we may also output data in JSON format, like example for AJAX calls, for data exchange with other systems, etc.

    Code:
    use strict;
    use warnings;
    use JSON;
    
    my $json_obj = new JSON;
    
    ## Build some Perl data
    my %perl_data;
    my @friends1 = qw[Shabbir Anjan Sajal Navin];
    my @friends2 = qw[Sanket Taneesha Sreekutty];
    
    $perl_data{Pradeep} = { locale => 'en_IN', friends => \@friends1 };
    $perl_data{Anjali} = { locale => 'en_IN', friends => \@friends2 };
    
    print $json_obj->pretty->encode(\%perl_data);
    
    Output:
    Code:
    {
       "Pradeep" : {
          "friends" : [
             "Shabbir",
             "Anjan",
             "Sajal",
             "Navin"
          ],
          "locale" : "en_IN"
       },
       "Anjali" : {
          "friends" : [
             "Sanket",
             "Taneesha",
             "Sreekutty"
          ],
          "locale" : "en_IN"
       }
    }
    
    Hopefully, this will help people start off with handling JSON in their Perl scripts, next time I will write about the advanced features of the JSON module and how they will be of help.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice