The next example shows a possible technique:
string example = @"
{
""Object1"": {
""Object2"": {
""MemberId"": ""12345"",
""AnotherProp1"": ""abc""
},
""AnotherProp2"": [""x"", ""y""]
}
}
";
JsonDocument doc = JsonDocument.Parse( example );
string findMemberId( JsonElement elem ) // (local helper function)
{
foreach( var z in elem.EnumerateObject( ) )
{
if( z.Name == "MemberId" ) return z.Value.ToString( );
string r = findMemberId( z.Value );
if( r != null ) return r;
}
return null;
}
string memberId = findMemberId( doc.RootElement );
Console.WriteLine( "Found MemberId: {0}", memberId );
To use it, add a reference to System.Text.Json package using “Manage NuGet Packages” feature.