Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Need something like a SQL query for JSON data structures (JSON::XS)

by walkingthecow (Friar)
on Sep 06, 2013 at 06:07 UTC ( #1052653=note: print w/ replies, xml ) Need Help??


in reply to Re: Need something like a SQL query for JSON data structures (JSON::XS)
in thread Need something like a SQL query for JSON data structures (JSON::XS)

Thank you! Thank you! Thank you!!! That perfectly solved my issue; though I'm not entirely sure how to grep the value of all keys without knowing what they keys are named, but that much I can figure out. I really appreciate the fast help! Thanks again ;)

Update

Figured it out:

#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use JSON::XS; use File::Slurp qw(read_file); my $lines = decode_json( read_file("test.json") ); my @results = grep { grep { $_ =~ /taco/ } values %$_ } @{ $lines }; print Dumper \@results;
Thanks again hdb!


Comment on Re^2: Need something like a SQL query for JSON data structures (JSON::XS)
Download Code
Re^3: Need something like a SQL query for JSON data structures (JSON::XS)
by Laurent_R (Priest) on Sep 06, 2013 at 06:17 UTC

    With a simple hash, to grep the keys:

    my @results = grep /taco/, keys %hash;

    To grep the values:

    my @results = grep /taco/, values %hash;

    To get the keys whose values match a regex:

    my @results = map {$hash{$_} =~ /taco/ ? $_:  ()} keys %hash;
      json_decode in my context returns an array of hash references. I posted the method that worked for me, though there may be an easier/better way to do it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-04-20 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (487 votes), past polls