Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Getting Value from specific key from json data dump

by Athanasius (Archbishop)
on Jun 11, 2019 at 04:23 UTC ( [id://11101224]=note: print w/replies, xml ) Need Help??


in reply to Getting Value from specific key from json data dump

Hello bizactuator,

Here is a proof-of-concept using the JSON module from CPAN:

use strict; use warnings; use JSON; my $string = read_json(); my $hashref = decode_json($string); for my $item (@{ $hashref->{items} }) { if (exists $item->{orderItemTitle}) { printf "Order Item Title: %s\n", $item->{orderItemTitle}; } } sub read_json { my $string =<<'EOS'; {\"order\\\\.shipTo\\\\.country\":\"US\",\"euddAccepted\":false,\"isFr +eeCart\":false,\"discount\ ... < most of input string omitted here > ... \"hasAnyDelayedDeliveryItems\":false,\"addOnSubtotal\":\"$0.00\",\"sto +ry\":\"FCF6F91A\"} EOS $string =~ s{ \\" }{"}gx; return $string; }

Output:

14:22 >perl 2006_SoPW.pl Order Item Title: Freelance Profit Academy 14:22 >

Notes:

  1. The input string requires some cleanup, as shown.
  2. To find the structure of the decoded JSON hash reference, dump it using a module such as Data::Dumper or Data::Dump. (Deciphering the output is the tricky part!)

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-23 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found