read-json.pl 607 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env perl
  2. # License: CC0
  3. use warnings;
  4. use strict;
  5. use Data::Dumper;
  6. # To install JSON::Tiny:
  7. # $ cpanm -n JSON::Tiny || cpan -T JSON::Tiny
  8. use JSON::Tiny qw(decode_json);
  9. sub read_json {
  10. my $json_filepath = shift;
  11. open( my $json_file, "<", "$json_filepath" ) or die("File $json_filepath not found");
  12. $/ = undef;
  13. my $json = <$json_file>;
  14. close($json_file);
  15. return decode_json( $json );
  16. }
  17. my $hash = read_json('info.json');
  18. print "-- Below are cars:\n";
  19. print Dumper($hash->{cars});
  20. print "-- Below are people:\n";
  21. foreach my $person (@{$hash->{people}}) {
  22. print Dumper($person);
  23. }