Search code examples
jsonperlperl-data-structures

Parsing JSON Data::Dumper output array in Perl


I'm trying to edit an old perl script and I'm a complete beginner. The request from the server returns as:

$VAR1 = [
          {
            'keywords' => [
                            'bare knuckle boxing',
                            'support group',
                            'dual identity',
                            'nihilism',
                            'support',
                            'rage and hate',
                            'insomnia',
                            'boxing',
                            'underground fighting'
                          ],
          }
        ];

How can I parse this JSON string to grab:

$keywords = "bare knuckle boxing,support group,dual identity,nihilism,support,rage and hate,insomnia,boxing,underground fighting"

Full perl code

#!/usr/bin/perl

use LWP::Simple;                # From CPAN
use JSON qw( decode_json );     # From CPAN
use Data::Dumper;               # Perl core module
use strict;                     # Good practice
use warnings;                   # Good practice
use WWW::TheMovieDB::Search;
use utf8::all;
use Encode;
use JSON::Parse 'json_to_perl';
use JSON::Any;
use JSON;

my $api = new WWW::TheMovieDB::Search('APIKEY');
my $img = $api->type('json');
$img = $api->Movie_imdbLookup('tt0137523');

my $decoded_json = decode_json( encode("utf8", $img) );

print Dumper $decoded_json;

Thanks.


Solution

  • Based on comments and on your recent edit, I would say that what you are asking is how to navigate a perl data structure, contained in the variable $decoded_json.

    my $keywords = join ",", @{ $decoded_json->[0]{'keywords'} };