0

I have a JSON file like this one (it is a piece of the entire JSON file):

{
  id => "mgp1310",
  samples => [
    {
      envPackage => {
        data => {
          diss_carb_dioxide => {
            aliases    => ["sediment_diss_carb_dioxide"],
            definition => "concentration of dissolved carbon dioxide",
            mixs       => 1,
            required   => 0,
            type       => "text",
            unit       => "",
            value      => "17 mM",
          },
        },
        id => "mge64559",
      },
    },
  ],
}

This has been decoded by module JSON, using:

use Data::Dumper;
use JSON;

open($fh, '<', 'hola.txt' );
$json_text   = <$fh>;
$perl = decode_json($json_text);
print Dumper($perl);  

Now I know that $perl has a hash. So I'd like to print the id of the JSON file using print $perl{"id"};. However, it prints nothing I don't know why.

Miller
  • 34,962
  • 4
  • 39
  • 60
user2979409
  • 773
  • 1
  • 12
  • 23

1 Answers1

5

I found the answer by adding use strict at my code. It threw the following error:

Global symbol "%perl" requires explicit package name at json.pl line 12.

The variable $perl is a scalar, not a hash!. Of course... I didn't thought about that. So I cannot access to the hash writing $perl{"id"}. The correct way is $perl->{id}.

user2979409
  • 773
  • 1
  • 12
  • 23