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

Re: search json for key with particular value

by hippo (Bishop)
on Dec 15, 2020 at 15:25 UTC ( [id://11125237]=note: print w/replies, xml ) Need Help??


in reply to search json for key with particular value

Sounds like a job for Data::DPath:

#!/usr/bin/env perl use strict; use warnings; use JSON::MaybeXS; use Data::DPath 'dpath'; use Data::Dumper; my $json = <<EOT; { "infra": { "config": { "rack": [ { "componentId": "xxx-001", "model": "xxx", "server": [ { "componentId": "server-001", "type": "xxxx", "model": "xxxx", "role": "Management", "specificAttributes": "" } ] }] }, "platform": { "config": { "mgmtser": [{ "componentId": "sr-001", "domainName": "xxxxx", "thinDiskMode": true, "deployment": "small" } ]} } } } EOT my $data = decode_json ($json); my @res = dpath ('//*[ key eq "componentId"]/..')->match ($data); print Dumper (\@res);

🦛

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-25 22:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found