in reply to Re: reading a JSON object
in thread reading a JSON object

Thanks. That's exactly the information I needed.

EDITED to add: the question below has been answered. Thanks so much.

Now I have another ignorant question. Below is my current working code. How do I get the size of the JSON object $json_string, and how do I loop over the first index?

use strict; use warnings; use Mojo::UserAgent; use LWP::UserAgent; use HTTP::Request::Common qw(GET); # use JSON; use Data::Dump qw(dd); my $outputFilename = "test_output.txt"; my $fh_data; my $urlkey_result; my $timestamp_result; my $original_result; my $mimetype_result; my $statuscode_result; my $digest_result; my $length_result; open( $fh_data, '>', $outputFilename ) or die "Could not open file '$outputFilename' $!"; my $url = "http://web.archive.org/cdx/search/cdx?url=anautismobserver.wordpress. +com&output=json"; # &limit=3 my $ua = Mojo::UserAgent->new; my $json_string = $ua->get($url)->res->json; # dd $json_string; # for debug $urlkey_result = $json_string->[0][0]; $timestamp_result = $json_string->[0][1]; $original_result = $json_string->[0][2]; $mimetype_result = $json_string->[0][3]; $statuscode_result = $json_string->[0][4]; $digest_result = $json_string->[0][5]; $length_result = $json_string->[0][6]; print $fh_data "\n$urlkey_result\t$timestamp_result\t$original_result\ +t$mimetype_result\t$statuscode_result\t$digest_result\t$length_result +"; $urlkey_result = $json_string->[1][0]; $timestamp_result = $json_string->[1][1]; $original_result = $json_string->[1][2]; $mimetype_result = $json_string->[1][3]; $statuscode_result = $json_string->[1][4]; $digest_result = $json_string->[1][5]; $length_result = $json_string->[1][6]; print $fh_data "\n$urlkey_result\t$timestamp_result\t$original_result\ +t$mimetype_result\t$statuscode_result\t$digest_result\t$length_result +"; $urlkey_result = $json_string->[2][0]; $timestamp_result = $json_string->[2][1]; $original_result = $json_string->[2][2]; $mimetype_result = $json_string->[2][3]; $statuscode_result = $json_string->[2][4]; $digest_result = $json_string->[2][5]; $length_result = $json_string->[2][6]; print $fh_data "\n$urlkey_result\t$timestamp_result\t$original_result\ +t$mimetype_result\t$statuscode_result\t$digest_result\t$length_result +"; $urlkey_result = $json_string->[3][0]; $timestamp_result = $json_string->[3][1]; $original_result = $json_string->[3][2]; $mimetype_result = $json_string->[3][3]; $statuscode_result = $json_string->[3][4]; $digest_result = $json_string->[3][5]; $length_result = $json_string->[3][6]; print $fh_data "\n$urlkey_result\t$timestamp_result\t$original_result\ +t$mimetype_result\t$statuscode_result\t$digest_result\t$length_result +"; close $fh_data;

Replies are listed 'Best First'.
Re^3: reading a JSON object
by AnomalousMonk (Archbishop) on May 09, 2022 at 18:39 UTC

    I'm confused.
        my $json_string = $ua->get($url)->res->json;
    Is $json_string in fact a string, or is it a decoded JSON object as shown in the example here, i.e., a reference to an array of arrays (in that particular example)? Your use of the $json_string->[0][0] syntax suggests the latter.

    If $json_string is in fact a ref. to an AoA, you might do something like this:

    Win8 Strawberry 5.30.3.1 (64) Mon 05/09/2022 14:30:45 C:\@Work\Perl\monks >perl use strict; use warnings; use JSON; use Data::Dump qw(dd); my $json_string = <<'EOJ'; [["urlkey","timestamp","original","mimetype","statuscode","digest","le +ngth"], ["org,archive)/", "19970126045828", "http://www.archive.org:80/", "text/html", "200", "Q4YULN754FHV2U6Q5JUT6Q2P57WEWNNY", "1415"], ["org,archive)/", "19971011050034", "http://www.archive.org:80/", "text/html", "200", "XAHDNHZ5P3GSSSNJ3DMEOJF7BMCCPZR3", "1402"], ["org,archive)/", "19971211122953", "http://www.archive.org:80/", "text/html", "200", "XAHDNHZ5P3GSSSNJ3DMEOJF7BMCCPZR3", "1405"]] EOJ my $ar_decoded_json = decode_json $json_string; # dd $ar_decoded_json; # for debug for my $ar_jentry (@$ar_decoded_json) { # handle each decoded JSON en +try printf "\n'%s' '%s' '%s' '%s' '%s' '%s' '%s'", @$ar_jentry; } ^Z 'urlkey' 'timestamp' 'original' 'mimetype' 'statuscode' 'digest' 'leng +th' 'org,archive)/' '19970126045828' 'http://www.archive.org:80/' 'text/ht +ml' '200' 'Q4YULN754FHV2U6Q5JU T6Q2P57WEWNNY' '1415' 'org,archive)/' '19971011050034' 'http://www.archive.org:80/' 'text/ht +ml' '200' 'XAHDNHZ5P3GSSSNJ3DM EOJF7BMCCPZR3' '1402' 'org,archive)/' '19971211122953' 'http://www.archive.org:80/' 'text/ht +ml' '200' 'XAHDNHZ5P3GSSSNJ3DM EOJF7BMCCPZR3' '1405'
    (I used different formatting just to try to make things fit better and to clearly delineate the strings. Sorry about the wraparound.)


    Give a man a fish:  <%-{-{-{-<

      EDITED to add: this has been solved. Thanks so much.

      Yes, $json_string is a decoded JSON object, not a string. That's what comes from copy-pasting inappropriately when I don't know what I'm doing.

      I can't emphasize enough how useful this feedback is to me. It doesn't just make the difference between easy and hard, it makes the difference between easy and impossible.

      My latest problem is that when I try to load the output text file into a Google spreadsheet, I get the following error message (no matter how long I wait beforehand) as soon as I try to manipulate any of the cells:

      "There was a problem. These cells are currently being loaded. Please retry when loading completes."

      Below is my current working code.

      use strict; use warnings; use Mojo::UserAgent; use LWP::UserAgent; use HTTP::Request::Common qw(GET); use Data::Dump qw(dd); my $outputFilename = "test_output.txt"; my $fh_data; open( $fh_data, '>', $outputFilename ) or die "Could not open file '$outputFilename' $!"; my $url = "http://web.archive.org/cdx/search/cdx?url=anautismobserver.wordpress. +com&output=json" ; # &limit=3 my $ua = Mojo::UserAgent->new; my $json_aoa = $ua->get($url)->res->json; # dd $json_aoa; # for debug for my $ar_jentry (@$json_aoa) { # handle each decoded JSON entry printf $fh_data "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", @$ar_jentry; } close $fh_data;

      Thank you again.

        ... load the output text file into a Google spreadsheet ...

        Now we're straying beyond the realm of my experience. There are many monks around the monastery who are much better qualified to answer this sort of question than I am. I will defer to their wisdom.


        Give a man a fish:  <%-{-{-{-<

      For future reference: How do I get the size numerically of an array of arrays (AoA)?

        ... the size numerically of an array of arrays (AoA) ...

        What does "size" mean in this context? Remember that a Perl array or hash is always and only an array or hash of scalars. However, any of these scalars may be a reference to another array or hash (and a few other things). To find the total number of elements in a nested array/hash structure, you have to figure out a way to recurse through the entire structure from top to bottom and add up the elements in any and all nested arrays/hashes. See the Perl Data Structure Cookbook.

        However, for a simple array, the process is straightforward. An array evaluated in scalar context yields the number of elements in the array. An array identifier or reference dereferenced with the $# sigil yields the index of the highest element in the array.

        Win8 Strawberry 5.8.9.5 (32) Mon 05/09/2022 19:13:22 C:\@Work\Perl\monks >perl use strict; use warnings; my @array = (9, 8, 7); my $arrayref = \@array; print "elements of array: (@array) (@$arrayref) \n"; printf "number of array elements: %d %d \n", scalar(@array), scalar(@$arrayref); print "highest array index: $#array $#$arrayref \n"; ^Z elements of array: (9 8 7) (9 8 7) number of array elements: 3 3 highest array index: 2 2

        But why do you need to explicitly know the "size" of an array or array structure? There are many techniques that allow one to iterate over the elements of an array or recurse through a nested structure without ever knowing the number of elements present. It all depends on just what you are trying to do.


        Give a man a fish:  <%-{-{-{-<