Bootstrap FreeKB - Perl (Scripting) - Getting Started with JSON::Parse
Perl (Scripting) - Getting Started with JSON::Parse

Updated:   |  Perl (Scripting) articles

The JSON::Parse module can be installed using cpan or cpanm.

cpanm JSON::Parse

 

The JSON::Parse module can be used to parse a JSON string or JSON file.

use JSON::Parse 'parse_json';

 

parse_json can be used to parse a JSON string. It's a good idea to use the eval function and to ensure the JSON string is valid.

my $parsed_json = "";

eval {
  $parsed_json = parse_json('{"foo_key":"Hello", "bar_key":"World"}');
};

if ($@) {
  print "ERROR! Invalid JSON: $@ \n";
  exit 1;
}
else {
  print "OK. The JSON is valid.\n";
}

 

Or like this, storing the JSON string in a variable named $json_string.

my $json_string = '{"foo_key":"Hello", "bar_key":"World"}';
my $parsed_json = "";

eval {
  $parsed_json = parse_json($json_string);
};

if ($@) {
  print "ERROR! Invalid JSON: $@ \n";
  exit 1;
}
else {
  print "OK. The JSON is valid.\n";
}

 

Or, if the JSON is in a file, json_file_to_perl can be used.

use JSON::Parse 'json_file_to_perl';

 

Using json_file_to_perl is fairly similar to using parse_json.

my $parsed_json = "";

my $json_file = "/path/to/example.json";

eval {
  $parsed_json = json_file_to_perl($json_file);
};

if ($@) {
  print "ERROR! Invalid JSON: $@ \n";
  exit 1;
}
else {
  print "OK. The JSON is valid.\n";
}

 

Sometimes assert_valid_json can be used to determine if the JSON parsed by parse_json or json_file_to_perl is valid. However, I've had hit and miss luck with assert_valid_json so I tend to not use assert_valid_json, instead going with the eval function to determine if the JSON parsed by parse_json or json_file_to_perl is valid

use JSON::Parse 'parse_json', 'assert_valid_json';

 

But, if you want to give assert_valid_json a try, it would be used like this.

 

eval {
  assert_valid_json ($raw_json);
};

if ($@) {
  print "ERROR! Invalid JSON: $@ \n";
  exit 1;
}
else {
  print "OK. The JSON is valid.\n";
}

 

Assuming the JSON is valid, attempting to print the parsed JSON . . .

print $parsed_json;

 

... with print either HASH ...

HASH

 

... or ARRAY.

ARRAY

 

Dumper can be used to print the parsed json.

use Data::Dumper;
print Dumper $parsed_json;

 

Dumper should produce something like this. There are no [ ] brackets, thus it's a hash.

$VAR1 = { 
          'foo_key' => 'Hello',
          'bar_key' => 'World'
        };

 

Or like this. There are [ ] brackets, thus it's an array.

$VAR1 = [
          { 
            'foo_key' => 'Hello',
            'bar_key' => 'World'
          }
        ];

 

If it's a hash, the following will print the value in foo_key.

print $parsed_json->{foo_key};

 

Which should print Hello.

Hello

 

If it's an array, the following will print the value in foo_key.

foreach my $line (@{$parsed_json}) {
  print $line->{foo_key};
}

 

Which should print Hello.

Hello

 

You can then loop through each key/value pair in the JSON file and print each key.

foreach my $line (@{$parsed_json}) {

  foreach my $key (keys(%{$line})) {
    print $key;
  }

  foreach my $value (values(%{$line})) {
    print $value;
  }

}

 

The print $key line will print each key, which is just foo_key and bar_key in this example.

foo
bar

 

The print $value line will print each value.

Hello
World

 

Instead of looping through each value, you can just get the value from the key.

foreach my $line (@{$parsed_json}) {

  foreach my $key (keys(%{$line})) {
    print $key;
  }

  my $value = $key->{foo_key};
  print $value;

}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 989293 in the box below so that we can be sure you are a human.