© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
6 replies
Eple

❔ JObject find value

I have the following JSON
{
    "sale": {
        "id": "x",
        "transaction": {
            "id": "y",
            "state": "Completed"
        }
    }
}
{
    "sale": {
        "id": "x",
        "transaction": {
            "id": "y",
            "state": "Completed"
        }
    }
}

I want to check if
sale.transaction.state == "Completed"
sale.transaction.state == "Completed"

But since the keys
sale
sale
,
transaction
transaction
and
state
state
are optional and not always in the response, I must check for their presence.
Is there a better way than what I have below?
var responseObject = JObject.Parse(responseString);
if (responseObject["sale"] != null && responseObject["sale"]["transaction"] != null && responseObject["sale"]["transaction"]["state"] != null)
{
    if (responseObject.Value<string>("sale.transaction.state") == "Completed")
    {
        Console.WriteLine("Yes");
    }
}
var responseObject = JObject.Parse(responseString);
if (responseObject["sale"] != null && responseObject["sale"]["transaction"] != null && responseObject["sale"]["transaction"]["state"] != null)
{
    if (responseObject.Value<string>("sale.transaction.state") == "Completed")
    {
        Console.WriteLine("Yes");
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Sorting a JObject by child values
C#CC# / help
4y ago
✅ How to fill JObject?
C#CC# / help
3y ago
✅ Web API Controller Response JObject
C#CC# / help
2y ago
Get multiple objects in JObject path
C#CC# / help
4y ago