Deserialize Json - If condition

Hello,

I want to get for each item the “queueId” and the “wrapupcode”.

For this I deserialize the Json and put a loop:

For each item in Deserialize(“results”).Values(Of JObject)
Assign queueid = item(“group”)(“queueId”).Value(Of String)
Assign wrapupcode = item(“group”)(“wrapUpCode”).Value(Of String)

However as you can see from my result below, there is not always a wrapupcode. Therefore I get the error: “Assign: Value cannot be null. Parameter name:value”.

I want to add a condition, that it should only assign the wrapupcode, if it exists. However I have not been able to do it. Has anyone an idea?

Thank you in advance for the help.

JSON RESULT:

{
“results”: [
{
“group”: {
“mediaType”: “email”,
“queueId”: “58c45614-705-4ee8-917a-3ac4879a959a”
},
“data”: [
{
“interval”: “2022-01-31T23:00:00.000Z/2022-02-28T23:00:00.000Z”,
“metrics”: [
{
“metric”: “tWait”,
“stats”: {
“max”: 581409889,
“min”: 55626,
“count”: 2500,
“sum”: 746920725339
}
}
]
}
]
},
{
“group”: {
“mediaType”: “email”,
“queueId”: “58c45614-7805-4e8-917-3ac4879a959a”,
“wrapUpCode”: “2402b3fb-392-4b3d-94e-e05959ca44c0”
},
“data”: [
{
“interval”: “2022-01-31T23:00:00.000Z/2022-02-28T23:00:00.000Z”,
“metrics”: [
{
“metric”: “tHandle”,
“stats”: {
“max”: 28510555,
“min”: 11401,
“count”: 141,
“sum”: 236363651
}
}
]
}
]

@regina2112 put this code in if statement,
if true then you can extract wrapupcode value, otherwise this key doesnt exist!

jObject.Parse(item("group").toString).ContainsKey("wrapUpCode")
image

One of many options - a defensive Property occurence check:


item("group").Value(Of JObject).Properties.Any(Function (p) p.Name.Equals("wrapUpCode"))

which can be merged also with the approach from Jack
item("group").Value(Of JObject).ContainsKey("wrapUpCode")

other options eg. property occurence lists are also possible. But it requires more code and so we prefer the more compact approaches:

Just for demo purpose:
grafik