in reply to reading a JSON object

Re: Clarification:

Win8 Strawberry 5.30.3.1 (64) Mon 05/09/2022 0:45:49 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 my $first_timestamp = $ar_decoded_json->[1][1]; print "'$first_timestamp' \n"; ^Z '19970126045828'


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

Replies are listed 'Best First'.
Re^2: reading a JSON object
by anautismobserver (Sexton) on May 09, 2022 at 18:01 UTC

    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;

      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.

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

Re^2: reading a JSON object
by anautismobserver (Sexton) on May 15, 2022 at 21:48 UTC

    How do I convert timestamps to numbers so I can subtract them without getting the following error message:

    Argument "timestamp" isn't numeric in subtraction

      The error message indicates you're using the string timestamp in subtraction.

      $ perl -wE 'say "timestamp" - 2' Argument "timestamp" isn't numeric in subtraction (-) at -e line 1. -2

      I don't see how the string can be meaningfully converted to a number. Please, show the code that demonstrates the problem. Are you using the string directly instead of using it as a hash key?

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Note that the first row in your array consists of the column names. Maybe you are not skipping that first row properly?

      Other than that, choroba's advice of showing the relevant code and data still applies.