in reply to Re^5: Perl Database Entries
in thread Perl Database Entries

Yes sorry about that, my api_results variable has
{"id":1084,"slug":"twitter-sportchek","name":"Twitter - SportChek","pr +ovider":{"id":44,"slug":"twitter","name":"Twitter - Shared","access_t +ype_cd":null,"login_method":"spider","launch_timeout":45,"capabilitie +s":{"has_import":false,"has_password_reset":true,"has_provision":fals +e,"has_saml":false,"has_sync":false,"unsupported_browsers":[],"servic +es":"authenticatable","auth_or_directory":false,"has_ou":false},"icon +s":{"small":"https://am.com/a/images/apps/twitter_sm.png","large":"ht +tps://assets.bitium.com/a/images/apps/twitter_lg.png","color_code":"7 +c8d95","first_letter":"t"}},"configuration":{"saml":false,"login":rtc +hek","url":"https://twier.com","extra":null,"install_type":"shared"," +authentication_type":"browser","launch_method":"get","namespace":["tw +itter.com"]},"labels":[],"created_at":"2017-06-09T23:22:15.571-07:00" +,"status":"active","key_vault":false,"requires_mfa_code":false},{"id" +:109291,"slug":"rei-spredfast","name":"REI - Spredfast","provider":{" +id":5561,"slug":"spredfast","name":"Spredfast","access_type_cd":null, +"login_method":"spider","launch_timeout":45,"capabilities":{"has_impo +rt":false,"has_password_reset":false,"has_provision":false,"has_saml" +:false,"has_sync":false,"unsupported_browsers":[],"services":"authent +icatable","auth_or_directory":false,"has_ou":false},"icons":{"small": +"https://assom/a/images/apps/spredfast_sm.png","large":"https://asset +s.bitium.com/a/images/apps/spredfast_lg.png","color_code":"65c6bb","f +irst_letter":"s"}},"configuration":{"saml":false,"login":"reiteamocia +l","url":"https://www.spredfast.com","extra":null,"install_type":"sha +red","authentication_type":"browser","launch_method":"get","namespace +":["www.spredfast.com"]},"labels":[],"created_at":"2017-06-19T12:33:3 +3.561-07:00","status":"active","key_vault":false,"requires_mfa_code": +false}]

But after running the code I am getting the error Not a HASH reference at line 79, <> line 2. line 79 is push @insert,$row->{$var};

Replies are listed 'Best First'.
Re^7: Perl Database Entries
by huck (Prior) on Jul 16, 2017 at 08:15 UTC

    No that is not your $api_results, is it? Again that is invalid json, when i fixed it i got the same result you did. If you lie to us why do you expect to get valid advice?

    And you dont follow instructions well do you? you changed THREE major parts of what i had posted, resulting in a whole different result from what you would have gotten if you had just cut & pasted what i wrote.

    but not knowing what jg did, i misinterpreted part of what was needed to make this work, so i included the fix in this version. See if you can figure out what you did wrong and why was it wrong from what i had first posted. Then try to figure out what i did to simulate how jg worked. But i suspect both tasks are beyond your abilities.

    $app_insert = $dbh->prepare("INSERT INTO bitium_apps(id, app_slug, app +_Name, team_id, provider_id, provider_slug, provider_saml, config_saml, confi +g_login, config_url, created_date, status, key_vault, mfa_code, configuration_i +nstall) VALUES (?, ?, ?, \"$response\", ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?") o +r die $dbh->errstr; use JSON; my $json=new JSON; my $unjson=$json->allow_nonref->decode($api_results); for my $row (@$unjson) { my @insert; for my $var (qw/.id .slug .name .provider.id .provider.name .provider.capabilities.has_saml .configuration.sam +l .configuration.login .configuration.url .created_a +t .status .key_vault .requires_mfa_code .configurati +on.install_type /){ my $var2=substr($var,1); my @parts=split('\.',$var2); my $base=$row; my $terminal=pop @parts; while (scalar(@parts)>0) { my $subpart=shift @parts; $base=$base->{$subpart}; } push @insert,$base->{$terminal}; } # var $app_insert->execute(@insert[0,1,2,4 .. 15]) or die $app_insert->e +rrstr; } # row
    Dont screw up again, and try to learn about what you are doing rather than just trying to get someone to do it for you

      I don't know about you , but I am here to learn.And as a seasoned member, you would be more progressive in your didactic efforts if your prose were less drenched with condescension and judgmental overtones. If you look through my posts I have never come here for a handout or for anyone to complete my work for me and any solution that I use I have to understand it before implementing it(that's just me).Why would I LIE about anything?? My boss keeps changing requirements and I adjusted them without properly informing the thread, I will own that.Nevertheless I thank you , you helped me to learn another method of doing something and fixed errors in my code as well , so here goes my explanation of code beyond my abilities lol

      $app_insert = $dbh->prepare("INSERT INTO bitium_apps(id, app_slug, app +_Name, team_id, provider_id, provider_slug, provider_saml, config_saml, confi +g_login, config_url, created_date, status, key_vault, mfa_code, configuration_i +nstall) VALUES (?, ?, ?, \"$response\", ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?") o +r die $dbh->errstr; use JSON; my $json=new JSON; my $unjson=$json->allow_nonref->decode($api_results); for my $row (@$unjson) { my @insert;

      ##This stores all the headers you want from the json output into a variable vars

      for my $var (qw/.id .slug .name .provider.id .provider.name .provider.capabilities.has_saml .configuration.sam +l .configuration.login .configuration.url .created_a +t .status .key_vault .requires_mfa_code .configurati +on.install_type /){

      #Next since the headers have . in them, we will substr and split to just have the header names by themself. Substr takes each header and with an offset of 1 stores those headers with a single . in the format we want into $var2

          my $var2=substr($var,1);

      ##There are however some complex json headers so we will use split to remove those as well, storing our desired result into an array

           my @parts=split('\.',$var2);

      ##This transfers out json output which is in a hash format into another variable

         my $base=$row;

      ##Since we have complex headers we will need to store them into multiple parts and assemble them together again.From the array @parts which houses our headers void of . we will now use pop to put the ending/last of each json header into the terminal variable

         my $terminal=pop @parts;

      ##I believe this converts the array into a scalar so its easier to determine when it is empty

         while (scalar(@parts)>0) {

      ##The broken first section or single header values will now be removed from @parts so shift will now move them into variable subparts

        my $subpart=shift @parts;

      ## Here we begin the reforming of header process referencing the array hash of our json format in base which we transferred from $row

      $base=$base->{$subpart}; }

      ##Then we are going to complete the reforming of our headers referencing our array hash which includes our initial header values from subpart, using terminal then push our derived text output based on our headers into an array @insert

      push @insert,$base->{$terminal}; } # var

      ##Use the perl DBI module to insert our desired values into the database.

      $app_insert->execute(@insert[0,1,2,4 .. 15]) or die $app_insert->errs +tr; } # row

        What is the difference between  my $unjson=$json->allow_nonref->decode($api_results); and

        my @unjson=$json->allow_nonref->decode($api_results);
        What happened differently when you replaced the $ with @? what was the contents of @unjson in your version

        What is the difference between

        for my $row (@unjson) {
        and
        for my $row (@$unjson) {
        What happened differently between the two after the change to @unjson=

        What is the difference between

        for my $var (qw/.id .slug .name .provider.id .provider.name .../){
        and
        for my $var (qw/.id, .slug, .name, .provider.id, .provider.name,.../) +{
        What happened differently after you made that change

        Yes the substr removed the leading dot, but its presence was more to remove a leading blank token after the split. The purpose of the split had less to do with removing the dots but instead broke the value into its subparts that were separated by the periods. $row is a hash reference, we did not transfer any json data in that assignment but instead made a copy of the pointer into a working variable that we could then modify without consequence. The scalar value of an array is the number of members in that array. The loop will only first execute if there was more than one period separated subpart to the name/value and will continue to execute while there are still subparts to deal with. $subpart represents the next value in the chain of array references we want to deal with. $base=$base->{$subpart}; then places into $base the reference to the subarray we named via $subpart. if $var was a.b.c on the first loop of the while $base now points to the hash $row->{a} instead of $row. on the second pass of the while loop $base will become $base->{b} which would be the same as $row->{a}{b}. As pop removed subparts from the array the loop finishes when there are no more subparts left in the array and $base is left as the pointer to the deepest hash we need so that $base->{$terminal} will point to the value of the last name of the original @parts. IF there was only 1 subart in the original @parts $base still points to the original hash of $row. if there were 2 subparts left after terminal was popped off, such as in a.b.c $base now points to $row->{a}{b} and $row->{a}{b}{c} would be placed into @insert. If there was only a terminal in $var (such as d) $row->{d} would be placed into @insert since $base would have never been changed from its original setting of $row.

        What was the difference between what you posted as $api_results in Re^4: Perl Database Entries and Re^6: Perl Database Entries. What was still wrong with the supposed json in Re^6: Perl Database Entries, (hint there were two errors that poj fixed for you without mentioning them)

        So you skipped explaining what went wrong when you changed what i posted, you still dont understand how not telling us what your data was or why posting incorrect data presented a problem in determining what needed to be done, and you dont understand what happened when you changed the for $var (qw/.../) sequence. Given the explanation above ill let you grade yourself on your understanding of what the perl simulation of jg did

        .
Re^7: Perl Database Entries
by poj (Abbot) on Jul 16, 2017 at 08:15 UTC

    Try this, check the log and if OK un-comment the database commands

    #!/usr/bin/perl use strict; use JSON; use DBI; use Data::Dump 'pp'; my $api_results = q! [{ "id":1084, "slug":"twitter-sportchek", "name":"Twitter - SportChek", "provider":{ "id":44, "slug":"twitter", "name":"Twitter - Shared", "access_type_cd":null, "login_method":"spider", "launch_timeout":45, "capabilities":{ "has_import":false, "has_password_reset":true, "has_provision":false, "has_saml":false, "has_sync":false, "unsupported_browsers":[], "services":"authenticatable", "auth_or_directory":false, "has_ou":false }, "icons":{ "small":"https://am.com/a/images/apps/twitter_sm.png", "large":"https://assets.bitium.com/a/images/apps/twitter_lg.png" +, "color_code":"7c8d95", "first_letter":"t" } }, "configuration":{ "saml":false, "login":"rtchek", "url":"https://twier.com", "extra":null, "install_type":"shared", "authentication_type":"browser", "launch_method":"get", "namespace":["twitter.com"] }, "labels":[], "created_at":"2017-06-09T23:22:15.571-07:00", "status":"active", "key_vault":false, "requires_mfa_code":false }, { "id":109291, "slug":"rei-spredfast", "name":"REI - Spredfast", "provider":{ "id":5561, "slug":"spredfast", "name":"Spredfast", "access_type_cd":null, "login_method":"spider", "launch_timeout":45, "capabilities":{ "has_import":false, "has_password_reset":false, "has_provision":false, "has_saml":false, "has_sync":false, "unsupported_browsers":[], "services":"authenticatable", "auth_or_directory":false, "has_ou":false }, "icons":{ "small":"https://assom/a/images/apps/spredfast_sm.png", "large":"https://assets.bitium.com/a/images/apps/spredfast_lg.pn +g", "color_code":"65c6bb", "first_letter":"s" } }, "configuration":{ "saml":false, "login":"reiteamocial", "url":"https://www.spredfast.com", "extra":null, "install_type":"shared", "authentication_type":"browser", "launch_method":"get", "namespace":["www.spredfast.com"] }, "labels":[], "created_at":"2017-06-19T12:33:33.561-07:00", "status":"active", "key_vault":false, "requires_mfa_code":false }]!; my $json = new JSON; my $data = $json->allow_nonref->decode($api_results); pp $data; my $dbh;# connect my @cols = qw( id app_slug app_Name provider_id provider_slug provider_saml config_saml config_login config_url created_date status key_vault mfa_code configuration_install ); my $cols = join ',',@cols; my $sql_insert = " INSERT IGNORE INTO apps ( $cols ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)" ; #my $sth = $dbh->prepare($sql_insert) or die $dbh->errstr; my $logfile = 'log.dat'; open LOG,'>',$logfile or die "Could not open $logfile : $!"; print LOG join "\t",@cols,"\n"; my $count; for (@$data){ my @row; push @row,$_->{'id'}; push @row,$_->{'slug'}; push @row,$_->{'name'}; push @row,$_->{'provider'}{'id'}; push @row,$_->{'provider'}{'slug'}; push @row,$_->{'provider'}{'capabilities'}{'has_saml'}; push @row,$_->{'configuration'}{'saml'}; push @row,$_->{'configuration'}{'login'}; push @row,$_->{'configuration'}{'url'}; push @row,$_->{'created_at'}; push @row,$_->{'status'}; push @row,$_->{'key_vault'}; push @row,$_->{'requires_mfa_code'}; push @row,$_->{'configuration'}{'install_type'}; #$sth->execute(@row) or die $sth->errstr; print LOG join "\t",@row,"\n"; ++$count; } close LOG; print "$count records inserted - see $logfile";
    poj
      Thank you