2

I am using .net core 3.1 and System.Text.Json. I have a string in a sub element of a json with element claims whom i wanted to load in some .net element and then validate the keys in it

{
  "scope": "openid MyScop",
  "claims": "{\"premiuminfo\":{\"country\":{\"value\":\"country1\"},\"town\":{\"value\":\"town1\"},\"given_name\":{\"value\":\"given_name1\"},\"postal_code\":{\"value\":\"postal_code1\"},\"family_name\":{\"value\":\"family_name1\"},\"houseno_or_housename\":{\"value\":\"test house number\"}}}",  
}

I have able to load the claims object in JsonElement

 JsonElement o = JsonSerializer.Deserialize<JsonElement>(s);

But unable to find any way to check the keys like premiuminfo , county e.t.c.

Can please someone help me in using it

3 Answers 3

3

You might be looking for TryGetProperty

TryGetProperty(ReadOnlySpan, JsonElement)

Looks for a property named propertyName in the current object, returning a value that indicates whether or not such a property exists. When the property exists, the method assigns its value to the value argument.

Sign up to request clarification or add additional context in comments.

Comments

2

Were using TryGetProperty wrongly Following is the way i were able to validate

string s = "{\"premiuminfo\":{\"country\":{\"value\":\"country1\"},\"town\":{\"value\":\"town1\"},\"given_name\":{\"value\":\"given_name1\"},\"postal_code\":{\"value\":\"postal_code1\"},\"family_name\":{\"value\":\"family_name1\"},\"houseno_or_housename\":{\"value\":\"houseno_or_housename1\"}}}";
//s = jwtData.ContainsKey("claims");
try
{
    JsonElement o = JsonSerializer.Deserialize<JsonElement>(s);

    if (o.TryGetProperty("premiuminfo", out var premiuminfo))
    {
        if (!premiuminfo.TryGetProperty("name", out var _) && (!premiuminfo.TryGetProperty("given_name", out var _) || !premiuminfo.TryGetProperty("family_name", out var _)))
        {
            processingResult = new ProcessingResultObject { ErrorResult = new ErrorResult { error = ErrorTypes.invalid_request.ToString(), error_description = "name or given_name, family_name is missing in claims" }, StatusCode = 400 };
        }
        else if (premiuminfo.TryGetProperty("name", out var _) && (premiuminfo.TryGetProperty("given_name", out var _) || !premiuminfo.TryGetProperty("family_name", out var _)))
        {
            processingResult = new ProcessingResultObject { ErrorResult = new ErrorResult { error = ErrorTypes.invalid_request.ToString(), error_description = "name and (given_name, family_name) both exists in claims" }, StatusCode = 400 };
        }
        else if (!premiuminfo.TryGetProperty("address", out var _) && (!premiuminfo.TryGetProperty("houseno_or_housename", out var _) || !premiuminfo.TryGetProperty("postal_code", out var _)))
        {
            processingResult = new ProcessingResultObject { ErrorResult = new ErrorResult { error = ErrorTypes.invalid_request.ToString(), error_description = "address is missing in claims" }, StatusCode = 400 };
        }
        else if (premiuminfo.TryGetProperty("address", out var _) && (premiuminfo.TryGetProperty("houseno_or_housename", out var _) || premiuminfo.TryGetProperty("postal_code", out var _)))
        {
            processingResult = new ProcessingResultObject { ErrorResult = new ErrorResult { error = ErrorTypes.invalid_request.ToString(), error_description = "address and (houseno_or_housename, postal_code) both exists in claims" }, StatusCode = 400 };
        }
        else
        {
            processingResult.Result = true;
        }
    }
    else
    {
        processingResult = new ProcessingResultObject { ErrorResult = new ErrorResult { error = ErrorTypes.invalid_request.ToString(), error_description = "premiuminfo are not found in claims" }, StatusCode = 400 };
    }
}
catch (Exception ex)
{
    processingResult = new ProcessingResultObject { ErrorResult = new ErrorResult { error = ErrorTypes.invalid_request.ToString(), error_description = "premiuminfo are not found in claims. ex" }, StatusCode = 400 };
}

Comments

0

I would recommend using Newtonsoft.Json library that gives you more functionality. You can parse your main json and then the string object you have for claims as well to access the properties within claims.

example

var obj = JObject.Parse(json);
var claims = JObject.Parse(obj["claims"].ToString());

Console.WriteLine(claims["premiuminfo"].ToString()); // Prints entire claim
Console.WriteLine(claims["premiuminfo"]["country"].ToString()); // prints the country.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.