Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^4: Bug in my perl script

by rahul_lfo (Initiate)
on Oct 17, 2013 at 11:27 UTC ( [id://1058620]=note: print w/replies, xml ) Need Help??


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

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.

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

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1058620]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-24 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found