0

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.

user1882536
  • 13
  • 1
  • 5
  • 4
    That's not a JSON string, that's Data::Dumper output. – TLP Feb 26 '13 at 13:36
  • Have you looked at [JSON.pm](http://search.cpan.org/dist/JSON/lib/JSON.pm#from_json) – vol7ron Feb 26 '13 at 13:36
  • yes its Data::Dumper output – user1882536 Feb 26 '13 at 13:37
  • 1
    @user1882536 I know, I already said that. So question is, is this the *actual* input you have to work with, or is it a variable that you thought it would be nice to print out its content with for this question? – TLP Feb 26 '13 at 13:39
  • 1
    @user1882536 — Please answer TLP's question instead of repeating your original question. – Quentin Feb 26 '13 at 13:44
  • @TLP yes i need print out print $keywords; – user1882536 Feb 26 '13 at 13:46
  • @user1882536 — That wasn't his question. You have posted some data in the question. It looks like the output of Data::Dumper. Do you have that text in a file, in that format, that you have to parse? If not, what is the real input that you have to deal with? Your title mentions JSON but there is no JSON there. The *implication* is that you have already parsed some JSON and just need to navigate a Perl data structure, but that isn't a certainty. – Quentin Feb 26 '13 at 13:49
  • *Where did the data come from that you have dumped using `Data::Dumper`?* – Borodin Feb 26 '13 at 13:50
  • @Quentin i have edit and add full code please see – user1882536 Feb 26 '13 at 13:52
  • @Borodin i have add full perl code in question – user1882536 Feb 26 '13 at 13:58

3 Answers3

1

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'} };
TLP
  • 66,756
  • 10
  • 92
  • 149
  • Can't use an undefined value as an ARRAY reference at testjson.pl line 31. – user1882536 Feb 26 '13 at 14:12
  • `$decoded_json` can't both be the thing you printed in Data::Dumper and undefined. So, no. That cannot happen. – TLP Feb 26 '13 at 14:14
  • same code copy past you give and add last print $keywords; but get error Can't use an undefined value as an ARRAY reference – user1882536 Feb 26 '13 at 14:17
  • @user1882536 Yes, you don't have to repeat obvious things. It is impossible for you to have a variable that is both undefined and contains data. It's not a thing that can happen at the same time. So NO, what you just said cannot happen. If it did happen, then that is because you changed something else so that your Dumper output is now `$VAR1 = undef`. It's got nothing to do with my code. – TLP Feb 26 '13 at 14:22
  • please sir test your self full code posted and check Dumper output also put your code in my full code – user1882536 Feb 26 '13 at 14:28
  • @user1882536 The 'genres' key contains an array that contains hashes, of which the key 'name' contains the various genres, so you cannot use an array for that. `$VAR1->[0]{'genres'}[0]{name}` equals "Drama", `$VAR1->[0]{'genres'}[1]{name}` equals "Action" etc. The easiest way would be to loop over `@{ $VAR1->[0]{'genres'} }` and push `$_->{'name'}` to an array. See more about perl data structures in http://perldoc.perl.org/perldata.html – TLP Feb 26 '13 at 15:16
0
say qq{ @{ $arrayref->[0]->{'keywords'} } };

As TLP pointed out, all you've shown is a combination of perl arrays/hashes. But you should look at the JSON.pm documentation, if you have a JSON string.

vol7ron
  • 40,809
  • 21
  • 119
  • 172
0

The result you present is similar to json, but the Perl-variant of it. (ie => instead of : etc). I don't think you need to look into the json part of it, As you already got the data. You just need to use Perl to join the data into a text string.

Just to eleborate on the solution to vol7ron :

#get a reference to the list of keywords
my $keywords_list = $decoded_json->[0]{'keywords'};
#merge this list with commas
my $keywords = join(',', @$keywords_list );
print $keywords;
FtLie
  • 773
  • 4
  • 14
  • i get error Can't use an undefined value as an ARRAY reference – user1882536 Feb 26 '13 at 14:09
  • Then $decoded_json must be undef. Try print Dumper $decoded_json; and if no data inside, then the error is somewhere else. if 'keywords' somethimes is undef, and to avoid special cases, use #ie default to a ref to an empty array my $keywords_list = $decoded_json->[0]{'keywords'} || [] ; – FtLie Feb 27 '13 at 09:51