I'm trying to dynamically find the names of leaf nodes of a JSON object whose structure isn't known in advance. First, I'm parsing the string into a list of JTokens, like this:
string req = @"{'creationRequestId':'A',
'value':{
'amount':1.0,
'currencyCode':'USD'
}
}";
var tokens = JToken.Parse(req);
Then I'd like to identify which are leaves. In the above example, 'creationRequestId':'A'
, 'amount':1.0
, and 'currencyCode':'USD'
are the leaves, and the names are creationRequestId
, amount
, and currencyCode
.
Attempt that works, but is slightly ugly
The below example recursively traverses the JSON tree and prints the leaf names:
public static void PrintLeafNames(IEnumerable<JToken> tokens)
{
foreach (var token in tokens)
{
bool isLeaf = token.Children().Count() == 1 && !token.Children().First().Children().Any();
if (token.Type == JTokenType.Property && isLeaf)
{
Console.WriteLine(((JProperty)token).Name);
}
if (token.Children().Any())
PrintLeafNames(token.Children<JToken>());
}
}
This works, printing:
creationRequestId
amount
currencyCode
However, I'm wondering if there's a less ugly expression for determining whether a JToken is a leaf:
bool isLeaf = token.Children().Count() == 1 && !token.Children().First().Children().Any();