I have a JSON file below and I want to check 3 states
Within the array "categories" I have another array "children" which is currently null
How can I do to know
- if children array is null ?
- if children array is defined and contain at least one data ?
- if children array is completely missing from the JSON whereas I was expecting to be here
Here below the JSON file
{
"id": "Store::REZZ",
"name": "Rezz",
"categories": [
{
"id": "Category::0556",
"name": "Cinéma",
"children": []
},
{
"id": "Category::0557",
"name": "Séries",
"children": []
}
],
"images": [
{
"format": "logo",
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/1920px-Google_2015_logo.svg.png",
"withTitle": false
}
],
"type": "PLAY"
}
I tried something but I can manage only the case 1. for others cases I have an "Not a Hash reference" error message
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use JSON qw( decode_json );
use JSON qw( from_json );
# JSON file
my $json_f = '/home/test';
# JSON text
my $json_text = do {
open (TOP, "<", $json_f);
local $/;
<TOP>
};
my $data = from_json($json_text);
my @tags = @{ $data->{"categories"}{"children"} };
if (@tags) {
foreach (@tags) {
say $_->{"name"};
say "1. array is ok and contains data";
}
} elsif (@tags == 0) {
say "3. array is empty";
} else {
say "2. array is missing";
}
__END__