Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need example of Perl JSON usage

Tags:

json

perl

how do I parse a JSON string in Perl?

Note: I am a C++/Java/C# software engineer, not a Perl-wielding scripter.

I have fully read the JSON.pm "documentation" to find a working example, but those docs turned out to be complete and utter garbage".

I also read this post, but no one seems to know how to get the JSON key, value pairs out of the decoded perl variable.

Here is my script. Can someone help?

use JSON;

my $json_string = '{"foo": "bar"}';
my $decoded_json = decode_json($json_text);
print $decoded_json["foo"];

std::map<string, string> mymap = convert_to_cplusplus(decoded_json); # doesn't work
my asfjsa;fjsa;fwe # doesn't work

I got frustrated near the end.

like image 935
stackoverflowuser2010 Avatar asked Dec 12 '22 09:12

stackoverflowuser2010


1 Answers

You need to use $decoded_json->{"foo"}---note the curly brackets, not the square ones. Also note the arrow ->---decode_json returns a reference.

I will readily admit that the documentation in this case is no help for someone who doesn't already know Perl, for example, to the level of knowing what $perl_hash_or_arrayref means. (A seasoned Perl programmer knows instantly it means you have to use ->.)

like image 180
Chris Jester-Young Avatar answered Jan 02 '23 09:01

Chris Jester-Young