Help needed in finding peropty in Json data dynamically

Born2Achieve 21 Reputation points
2021-02-02T19:41:35.28+00:00

I need to extract the property value from jason string dynamically at run time. The below is the structure of my response body and Result will have any kind of object and object will have MemberId. the object inside the Result will not be unique.. it can contain object and if object has property "MemberId" then i need to extract the value of it. is is possible to do?
{
"Response": {
"Version": "1",
"StatusCode": "OK",
"Result": {

      },
    "Message": {
      "Code": 1000,
      "Description": "Success"
    }
  }
}

Sample Code:
{
"Response": {
"Version": "1",
"StatusCode": "OK",
"Result": {
"Profile": {
"UserName": "SampleUser",
"FirstName": "Ethan",
"LastName": "Chung",
"IsMobileNumberVerified": false,
"MobilePhoneNumber": null,
"MemberId": 9472267
},

      "lstEnrollment": null
    },
    "Message": {
      "Code": 1000,
      "Description": "Success"
    }
  }
}

On the above example, MemberID is part of profile object. but this can change and MemberID can be part of any other object as well. so, inside Result, we need to find MemberID part of any object.</div> <div>please help me with some sample code.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,095 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 118.4K Reputation points
    2021-02-02T20:28:31.017+00:00

    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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Born2Achieve 21 Reputation points
    2021-02-02T22:36:46.277+00:00

    I tried to troubleshoot in deep and finally am able to achieve with the below way.

      JsonElement jsonElement = z.Value;
                        if (jsonElement.ValueKind == JsonValueKind.Object)
                        {
                            string r = findMembeIdr(z.Value);
                            if (r != null) return r;
                        }
    

    Please let me know is there any better way to do this.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.