Don't ask to ask, just ask | |
PerlMonks |
Re: search json for key with particular valueby pryrt (Abbot) |
on Dec 15, 2020 at 14:46 UTC ( [id://11125235]=note: print w/replies, xml ) | Need Help?? |
The way I would approach the task is to recurse into each object, and return the current object if it has a "componentId" key whose value matches your restriction. Here is some code that does the recursion and returns all objects that include a "componentId". I marked the line where you would edit the logic to only return objects that have componentId whose value matches.
<Reveal this spoiler or all in this thread> I am not a JSON expert, so there may be a way using the JSON module or helper module to do a more efficient recursive search than my manual answer. I yield to wiser monks. -- edit: JSON module will be faster if you have installed JSON::XS, because it will try to use that if possible. edit2: updated code to use the OP-style data, as interpreted by hippo, rather than my proprietary data; also fixed post and code so "componentId" had correct case on the "d"
In Section
Seekers of Perl Wisdom
|
|