I want to check 3 cases regarding JSON files
In order to illustrate those, here 3 samples
{
"menu":{
"id":"file",
"popup":{
"menuitem":[
]
}
}
}
[ ]
[ i ]
I tried to manage the scenario 1. and 3. I guess, but I can't find a way to process the scenario 2. so I need your hints on this.
#!/usr/bin/perl
use strict;
use warnings;
use JSON qw( decode_json );
use JSON qw( from_json );
# JSON file
my $json_f = '/home/test.json';
# JSON text
my $json_text = do {
open (TOP, "<", $json_f);
local $/;
<TOP>
};
my $data = from_json($json_text);
my $json_out = eval { decode_json($json_text) };
if ($@) {
print "JSON file is invalid :$@\n";
} else {
print "JSON file is correct", "\n";
}
I think I'd probably do something like this:
-e $file
-s $file
(0 means zero bytes). You can also look at the content you read: length($data) == 0
.But, it seems like you have another case where you have valid JSON but the array or objects have no values. That's perfectly valid so you get another step. You either have a value, an array, or an object. Check each type; if it's an array or object, check that it has elements or keys.