in reply to Re: Bug in my perl script
in thread Perl Script to create jsons(Issue with comma)

Hi , Thanks for the replies. I updated my json which is as follows--
use strict; use warnings; use JSON; open(FILE, ">>bookmarkmirror.json"); my %bookmarks; my @charset = ('1','0'); my @charset2 =(''); my @charset3 =(''); my @charset4 = (('A'..'Z'), ('a'..'z')); my @charset5 = (('A'..'Z'), ('a'..'z')); my @charset6 = ('1' .. '1000'); my @charset7 =('http://www.','https://www.'); my @charset8=('.com','.co','.in','.info','.org','.net','.biz','.us','. +me','.mobi','.co.in','.firm.in','.gen.in','.ind.in','.net.in','.org.i +n','.tv','.ag','.am','.asia','.at','.be','.bz','.ca','.cc','.co.nz',' +.co.uk','.com.ag','.com.au','.com.br','.com.bz','.com.co','.com.es',' +.com.mx','.com.pe','.com.so','.com.tw','.cz','.de','.es','.eu','.fm', +'.fr','.gs','.idv.tw','.it','.jobs','.jp','.la','.me.uk','.ms','.mx', +'.net.ag','.net.au','.net.br','.net.bz','.net.co','.net.nz','.net.pe' +,'.net.so','.nl','.nom.co','.nom.es','.nom.pe','.org.ag','.org.au','. +org.es','.org.nz','.org.pe','.org.so','.org.tw','.org.uk','.pe','.se' +,'.so','.tk','.tw','.ws','.xxx'); my $i=0; print "Enter the limit : "; chomp(my $lim = <STDIN>); while ( $lim-- ) { push @{$bookmarks{browserBookMarksData}}, { bookMark => $charset[ rand(@charset) ], created => $charset2[ rand(@charset2) ], date => $charset3[ rand(@charset3) ], title => join '', @charset4[map {int rand @charset4} (1..8) +], url => join '', @charset5[map {int rand @charset5} (1..8) +], visits => $charset6[rand(@charset6) ], }; } print FILE to_json \%bookmarks; close(FILE);
The ouput is pasted below-

{"browserBookMarksData":{"created":"","bookMark":"1","date":"","title":"YvJzwzBRurlOgWzGkJEvisits843"},{"created":"","bookMark":"1","date":"","title":"COubQkTRurlfVbzdDGUvisits393"}}

Now the problem is that the url and visit fields get merged .There is no comma seperation between there the url and the visits fields and also how to join the three character set ie. charset4 ,charset7 and charset 8 to make the url field.

Replies are listed 'Best First'.
Re^3: Bug in my perl script
by hdb (Monsignor) on Oct 17, 2013 at 09:36 UTC

    In order to avoid join to consume the rest of the list, try

    title => join( '', @charset4[map {int rand @charset4} (1..8 +)]), url => join( '', @charset5[map {int rand @charset5} (1..8 +)]),
      Thanks Perl GOD ..You are awesome...
      Hi Monks, Just another query that can we use push for creating multiple beans. So that the output can be as given below-
      {"contactsData":[{"m_UID":"963","m_display_name":"Lynne","m_emailList" +:[{"m_emailID":"jhs@email.com","m_emailLabel":"","m_emailType":"2"}], +"m_eventList":[{"m_startDate":"1991-10-25","m_eventLabel":"","m_etype +":"1"}],"m_firstName":"Lynne","m_imList":[{"m_CustomProtocol":"test", +"m_CustomType":"","m_imAddress":"Lynne","m_imProtocol":"-1","m_imType +":"3"}],"m_isStarred":"1","m_lastName":"ROWLAND","m_middleName":"Lynn +e","m_namePrefix":"","m_nameSuffix":"","m_nickname":"Lynne","m_note": +"","m_organizationList":[{"m_JobDepartment":"","m_JobDescription":"", +"m_OfficeLocation":"Ruzhou","m_OfficePhoneticName":"","m_OfficeSymbol +":"","m_TypeLabel":"test","m_organizationName":"Rio Tinto Group","m_t +itle":"","m_type":"0"}],"m_phoneNumberList":[{"m_label":"","m_number" +:"9841003417","m_type":"21"}],"m_phonetic_fname":"Lynne","m_phonetic_ +lname":"Lynne","m_phonetic_mname":"Lynne","m_photoBitmap":"","m_posta +lAddresses":[{"m_address":"787-4199 Orci Avenue","m_label":"","m_regi +on":"West Africa","m_city":"Ruzhou","m_country":"Guinea-bissau","m_ne +ighbourhood":"","m_PoBox":"251861","m_street":"","m_zipcode":"444610" +,"m_type":"1"}],"m_source":"Google","m_vPredefinedTags":"1","websiteL +ist":[{"m_website":"","m_websiteLabel":"","m_websiteType":"3"}],"rela +tionship":[{"m_relationLabel":"","m_relationName":"ROWLAND","m_relati +onType":"6"}],"socialList":[{"m_Type":"0","m_ProfileName":"ROWLAND"," +m_CustomProfileName":"test"}],"contactMatching":"true"} ]}
      Thanks it Worked.

        Yes, you can but you have to deal with more complex data structures in Perl. Have a look at perldsc.

        In order to find out, what structure you need to create a desired JSON format, transform your JSON back into a Perl datastructure and use Data::Dumper to analyze it:

        use strict; use warnings; use JSON; use Data::Dumper; my $js = <<'JSON'; {"contactsData":[{"m_UID":"963","m_display_name":"Lynne","m_emailList" +:[{"m_emailID":"jhs@email.com","m_emailLabel":"","m_emailType":"2"}], +"m_eventList":[{"m_startDate":"1991-10-25","m_eventLabel":"","m_etype +":"1"}],"m_firstName":"Lynne","m_imList":[{"m_CustomProtocol":"test", +"m_CustomType":"","m_imAddress":"Lynne","m_imProtocol":"-1","m_imType +":"3"}],"m_isStarred":"1","m_lastName":"ROWLAND","m_middleName":"Lynn +e","m_namePrefix":"","m_nameSuffix":"","m_nickname":"Lynne","m_note": +"","m_organizationList":[{"m_JobDepartment":"","m_JobDescription":"", +"m_OfficeLocation":"Ruzhou","m_OfficePhoneticName":"","m_OfficeSymbol +":"","m_TypeLabel":"test","m_organizationName":"Rio Tinto Group","m_t +itle":"","m_type":"0"}],"m_phoneNumberList":[{"m_label":"","m_number" +:"9841003417","m_type":"21"}],"m_phonetic_fname":"Lynne","m_phonetic_ +lname":"Lynne","m_phonetic_mname":"Lynne","m_photoBitmap":"","m_posta +lAddresses":[{"m_address":"787-4199 Orci Avenue","m_label":"","m_regi +on":"West Africa","m_city":"Ruzhou","m_country":"Guinea-bissau","m_ne +ighbourhood":"","m_PoBox":"251861","m_street":"","m_zipcode":"444610" +,"m_type":"1"}],"m_source":"Google","m_vPredefinedTags":"1","websiteL +ist":[{"m_website":"","m_websiteLabel":"","m_websiteType":"3"}],"rela +tionship":[{"m_relationLabel":"","m_relationName":"ROWLAND","m_relati +onType":"6"}],"socialList":[{"m_Type":"0","m_ProfileName":"ROWLAND"," +m_CustomProfileName":"test"}],"contactMatching":"true"} ]} JSON my $perl = from_json $js; print Dumper $perl;

        In your example, the output would begin like this:

        $VAR1 = { 'contactsData' => [ { 'm_note' => '', 'm_display_name' => 'Lynne', 'relationship' => [ { 'm_relationName' + => 'ROWLAND', 'm_relationType' + => '6', 'm_relationLabel +' => '' } ], 'm_UID' => '963', 'm_lastName' => 'ROWLAND', etc...

        which means that you could create it like this:

        my $perl; $perl->{contactsData}->[0]->{m_note} = ''; $perl->{contactsData}->[0]->{m_display_name} = 'Lynne'; $perl->{contactsData}->[0]->{relationship}->[0]->{m_relationName} = 'R +OWLAND'; ...etc

        Hope this helps.