How to check if a specific key value exists in this json object

I want to check if key “type” exists in this json object .

Can someone please help me with that ?

Key Name : type
Key value : termination

JObject(1) { JProperty(1) { JArray(1) { JObject(32) { JProperty(1) { [RITM0093198] }, JProperty(1) { [SCTASK0122707] }, JProperty(1) { [452d13fc1b2e8a10128698e8bc4bcbd8] }, JProperty(1) { [REQ0087140] }, JProperty(1) { JObject(1) { JProperty(1) { JArray(0) { } } } }, JProperty(1) { [SN-PROD-IT-ITIL-Onboarding] }, JProperty(1) { [test c1b] }, JProperty(1) { [2024-05-31 04:06:17] }, JProperty(1) { [Open] }, JProperty(1) { }, JProperty(1) { [Task for Offboarding group] }, JProperty(1) { }, JProperty(1) { [Termination] }, JProperty(1) { [PROD] }, JProperty(1) { [123] }, JProperty(1) { [456] }, JProperty(1) { [123.456@c1b.com] }, JProperty(1) { [123] }, JProperty(1) { [123] }, JProperty(1) { [456] }, JProperty(1) { [123,456] }, JProperty(1) { [2024-05-31] }, JProperty(1) { [2024-05-31] }, JProperty(1) { [abc] }, JProperty(1) { [xyz] }, JProperty(1) { [123] }, JProperty(1) { [456] }, JProperty(1) { [ssss] }, JProperty(1) { [2024-06-04] }, JProperty(1) { [2024-06-05] }, JProperty(1) { [sss] }, JProperty(1) { [dc] } } } } }

1 Like

Hi @Ragavi_Rajasekar

keyExists = If(jObject.SelectToken("type") IsNot Nothing, True, False)
keyExists is of DataType System.Boolean

This line of code checks if the key "type" exists in the JSON object jObject after derserializing the JSON String. If it exists, it sets keyExists to True ; otherwise, it sets it to False .

Or
If you want to check whether the key "type" exists in JSON Object and print termination as value then use below syntax:

typeValue = If(jObject("type") IsNot Nothing, jObject("type").ToString(), String.Empty)
typeValue is of DataType SYstem.String

Regards

@Ragavi_Rajasekar,

Try this in Assign activity. This will return True or False.

IsNothing(JObject("type"))

Thanks,
Ashok :slight_smile:

We recommend to share with us details of the JSON structure or the JSON as Textfile.

When it is about a property existence Check we can do it with the dedicated methods.

As an alternative we can also do

YourJSonObjectVar.SelectTokens("..YOURKEYNAME").Any()

With the .. we check for Any Property (with the defined name), which can also be located within deeper structures

For checking all Properties of Key “Type” with the value “Termination” we extend above along with a where filter Linq / or we define the where filtering directly within the JSON Path Expression

UPD1 - Spellings and Style adaptions

That’s not actually your JSON. If you want to see the actual JSON (which is just a string) then use yourJSONObjectVar.ToString

I tried with that expression . But even when the type key is there … it gives the bool value as false


Hi @Ragavi_Rajasekar

Can you please share the Json file ?

Regards

Kindly Note. The Property IS maybe in deeper level. Therefore WE mentioned

{
“response”: {
“result”: {
“tasks”: {
“request”: “REQ0087140”,
“assignment_group”: “SN-PROD-IT-ITIL-Onboarding”,
“agency_code”: “123”,
“roles”: “123,456”,
“description”: “”,
“title”: “Task for Offboarding group”,
“type”: “Termination”,
“number”: “SCTASK0122707”,
“opened_by”: “test c1b”,
“manager_id”: “456”,
“Ritmnumber”: “RITM0093198”,
“state”: “Open”,
“first_name”: “123”,
“email”: “123.456@c1b.com”,
“assigned_to”: “”,
“modified_on”: “2024-05-31”,
“agency_name”: “xyz”,
“sysid”: “452d13fc1b2e8a10128698e8bc4bcbd8”,
“agent_id_external”: “456”,
“last_name”: “456”,
“requested_on”: “2024-06-04”,
“hire_date”: “2024-06-05”,
“middle_name”: “123”,
“opened_at”: “2024-05-31 04:06:17”,
“manager_name”: “abc”,
“environment”: “PROD”,
“requested_by”: “ssss”,
“application”: “sss”,
“created_on”: “2024-05-31”,
“user_id”: “dc”,
“approver_details”: “”,
“agent_id_internal”: “123”
}
}
}
}

This worked … but after obtaining the jobject …for each is used to loop through all the items in that jobject .

For example

For each — type argument is newtonsoft.json.ling.jtoken

For each item in jobject(“result”)(“tasks”)

Inside this loop when an item is picked , i should see if the key type is present or not .

Not Sure If above JSON or other Sample IS meant

Regarding second Sample maybe you are askiong for Looping over the Properties

jobject(“result”)(“tasks”).Properties

Propname - loopvar.Name
propvalue - loopvar.Value.value(of String)

There can be many items in the jobject … thats why for each is used to pick each and every item and fetch the required values from that item

Hi @Ragavi_Rajasekar

From this if type is there you want to print it as True otherwise False right. If possible provide entire JSON and expected Output.

Regards