Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Need to get the key information from key and value pair

by chandantul (Scribe)
on Sep 13, 2021 at 06:39 UTC ( #11136700=perlquestion: print w/replies, xml ) Need Help??

chandantul has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Need to get the key information from key and value pair
by marto (Cardinal) on Sep 13, 2021 at 08:21 UTC

    "I have iterate above json data by below code"

    The code you posted pulls in 10 modules, yet none of the code posted makes any use of them. You can't have run the code you posted as perl would have pointed out your mistake, which is visible if you look at the few lines. Again How do I post a question effectively?.

Re: Need to get the key information from key and value pair
by AnomalousMonk (Bishop) on Sep 13, 2021 at 10:27 UTC

    If you want to iterate through the key/value pairs of the "appLinks" hash, here's an example. Note that the OPed JSON string must be fixed before it can be decoded (misplaced commas), and also that I've added some more pairs for the example.

    Win8 Strawberry 5.8.9.5 (32) Mon 09/13/2021 6:17:03 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use JSON; use Data::Dump qw(dd); # fixed and enhanced JSON string. my $json_string = ' [ { "visio": { "Launch": false, "hide": { "iOS": true }, "appLinks": { "preview_sandbox_1_link": true, "fooble": false, "wibble": 42, "blarfl": "darble" } } } ] '; # print "$json_string \n"; # for debug my $arrayref = decode_json $json_string; # dd $arrayref; # for debug # convenience variable. my $appLinks_hashref = $arrayref->[0]{'visio'}{'appLinks'}; for my $appLink_key (keys %{ $appLinks_hashref }) { print "appLink key '$appLink_key' -> "; print "appLink value '$appLinks_hashref->{$appLink_key}' \n"; } ^Z appLink key 'wibble' -> appLink value '42' appLink key 'blarfl' -> appLink value 'darble' appLink key 'preview_sandbox_1_link' -> appLink value 'true' appLink key 'fooble' -> appLink value 'false'
    I hope this helps a bit.


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

Re: Need to get the key information from key and value pair
by The Perlman (Scribe) on Sep 13, 2021 at 11:03 UTC
    As far as I understand you want something like  keys  %{ $response->[0]{visio}{appLinks} } Well adjusted to your real data.
    - Ron
Re: Need to get the key information from key and value pair
by Anonymous Monk on Sep 13, 2021 at 06:57 UTC
    Please check my below code let me know the reason i was not able to print the key value.

    it's because even after three years here you don't know basic Perl or how to post a question.

    .oO{ troll? }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2021-12-09 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (36 votes). Check out past polls.

    Notices?