user3270760
user3270760

Reputation: 1504

Convert json to array using Perl

I have a chunk of json that has the following format:

{"page":{"size":7,"number":1,"totalPages":1,"totalElements":7,"resultSetId":null,"duration":0},"content":[{"id":"787edc99-e94f-4132-b596-d04fc56596f9","name":"Verification","attributes":{"ruleExecutionClass":"VerificationRule"},"userTags":[],"links":[{"rel":"self","href":"/endpoint/787edc99-e94f-4132-b596-d04fc56596f9","id":"787edc99-e94f-...

Basically the size attribute (in this case) tells me that there are 7 parts to the content section. How do I convert this chunk of json to an array in Perl, and can I do it using the size attribute? Or is there a simpler way like just using decode_json()?

Here is what I have so far:

my $resources = get_that_json_chunk();  # function returns exactly the json you see, except all 7 resources in the content section
my @decoded_json = @$resources;

foreach my $resource (@decoded_json) {

I've also tried something like this:

my $deserialize = from_json( $resources );
my @decoded_json = (@{$deserialize});

I want to iterate over the array and handle the data. I've tried a few different ways because I read a little about array refs, but I keep getting "Not an ARRAY reference" errors and "Can't use string ("{"page":{"size":7,"number":1,"to"...) as an ARRAY ref while "strict refs" in use"

Upvotes: 3

Views: 3116

Answers (1)

user3270760
user3270760

Reputation: 1504

Thank you to Matt Jacob:

my $deserialized = decode_json($resources); 
print "$_->{id}\n" for @{$deserialized->{content}};

Upvotes: 5

Related Questions