I have two data structures in JSON format. They are deeply nested hashes. How can I deeply compare these structures?
Asked
Active
Viewed 2,809 times
-4
-
2And you should have tried something... – Zaid Apr 15 '13 at 07:49
1 Answers
3
You can decode the JSON using JSON which just uses JSON::XS if it is installed on your system.
use JSON;
use Data::Compare;
my $h1 = JSON->new->utf8->decode($perl_scalar1);
my $h2 = JSON->new->utf8->decode($perl_scalar2);
my $c = Data::Compare->new($h1, $h2);
print 'structures of $h1 and $h are ',
$c->Cmp ? "" : "not ", "identical.\n";

Sinan Ünür
- 116,958
- 15
- 196
- 339
-
If you're going to `encode` the data, then you might as well just use `eq` instead of Data::Compare. – Quentin Apr 15 '13 at 06:55
-
-
3The OP has two JSON strings, you need to decode them to Perl data structures. – Zaid Apr 15 '13 at 07:29
-