Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: search json for key with particular value

by hippo (Chancellor)
on Dec 15, 2020 at 15:25 UTC ( #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
Node Status?
node history
Node Type: note [id://11125237]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2021-02-28 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?