I have a JSON structure in the likes of
{
"a": {"b": "value"},
"c": {"b": "value"},
"d": {"b": "value"},
"a": {"e": {"b": ["value"]}}
}
The expected answer is 4
. b
could be anywhere in the tree structure.
I want to know (for testing) how many instances of b I have. Google tells me to use JSON.stringify
and then work on the string. In XML, I could have used XPath to count the number of elements. Is there a JS/JSON-native way to do this other than iterating over each field’s keys?