C
C#17mo ago
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" But since the keys sale, transaction and 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");
}
}
5 Replies
canton7
canton717mo ago
You can use ? with []: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and- So, if (responseObject["sale"]?["transaction"]?["state"] ...) or so?
Member access and null-conditional operators and expressions:
C# operators that you use to access type members or null-conditionally access type members. These operators include the dot operator - ., indexers - [, ], ^ and .., and invocation - (, ).
Anton
Anton17mo ago
Like this ig?
var sale = responseObject["sale"];
if (sale is null)
return false;

var transaction = sale["transaction"];
if (transaction is null)
return false;

var state = transaction["state"];
if (state is null)
return false;

if (state.Value<string>() == "Completed")
return true;

return false;
var sale = responseObject["sale"];
if (sale is null)
return false;

var transaction = sale["transaction"];
if (transaction is null)
return false;

var state = transaction["state"];
if (state is null)
return false;

if (state.Value<string>() == "Completed")
return true;

return false;
easier to debug
canton7
canton717mo ago
Why not just responseObject["sale"]?["transaction"]?["state"]?.Value<string>() == "Completed"?
Eple
Eple17mo ago
These are the kind of answers that I come to this channel for. Smart and brilliant ones.
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Dall-E Image VariationHello, I am new to C# I have tried Image Creation with C# and it is working fine. but i don't have a✅ Suggestions for something like ConcurrentQueue with eventsC# beginner question... I have an app with multiple threads. I need to pass data from one thread to ❔ "The server has not been started or no web application was configured."I have two test classes that inherit from` ITestFixture<WebAppFactory>` just like you see in the Mic✅ Making a library as accessible for different .NET versions as possibleI'm developing a simple library meant to be an API wrapper for .NET projects that might want to acce❔ VSCode Disable Particular Auto-AdditionsWhat are these additions/autocorrects called and how do I turn them off? I've scanned through my C# Advice for how best to adapt a Python habit to C#(This is code for a Game of Life board's `Populate` method, that aims to create a 2D array of tiles ❔ Database with SQL (coming from a Rails background)So for those that don't know Rails, we generate our model like this ``rails g model product name:s❔ I am getting CORS error when I deploy Duende Identity Server but it work fine locallyI get 400 Bad request trying to login on Duende identity server when I deploy it onto a remote serve❔ New to C# i am looking for a source to learn WPF and develop desktop apps, any recommendation?Filling the gap❔ How to convert object type to double. Pls help me``` [Route("/")] public async Task<IActionResult> Index() { strin