# JSONPath expression to find elements in array based on nested array values

**URL:** <https://developer.sailpoint.com/discuss/t/jsonpath-expression-to-find-elements-in-array-based-on-nested-array-values/35291>\
**Category:** SHF Discussion and Questions\
**Tags:** workflows\
**Created:** [March 14, 2024, 12:38pm UTC](https://developer.sailpoint.com/discuss/t/jsonpath-expression-to-find-elements-in-array-based-on-nested-array-values/35291 "2024-03-14T12:38:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![maciej\_itrun](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@maciej\_itrun](https://developer.sailpoint.com/discuss/u/maciej_itrun)\
**Post date:** [March 14, 2024, 12:38pm UTC](https://developer.sailpoint.com/discuss/t/jsonpath-expression-to-find-elements-in-array-based-on-nested-array-values/35291/1 "2024-03-14T12:38:27Z")

</div>

In my IDN Workflow - with trigger set to Provisioning Completed - my goal is to filter using JSONPath. I want to get all accountRequests objects that have source.name == ‘IdentityNow’, that have provisioningTarget == ‘IdentityNow’ and lastly that have any attributeRequests nested array objects that have attributeName == ‘assigned Roles’. So far i came up with this expression:

`$.accountRequests[?(@.source.name == 'IdentityNow' && @.provisioningTarget == 'IdentityNow' && @.attributeRequests[?(@.attributeName == 'assignedRoles')])]`

All online JSONPath evaluators are telling me there is an error in the last part of the expression.

Sample JSON looks like this :

```auto
{
   "accountRequests":[
        {
            "source":{
                "id":"xxxxxx",
                "name":"IdentityNow",
                "type":"SOURCE"
            },
            "accountId":"xxxx",
            "accountOperation":"Modify",
            "provisioningResult":"SUCCESS",
            "provisioningTarget":"IdentityNow",
            "ticketId":"7263219262",
            "attributeRequests":[
                {
                    "operation":"Add",
                    "attributeName":"memberOf",
                    "attributeValue":"xxxxx"
                },
 				{

                  "attributeName":"assignedRoles",

                  "attributeValue":"Test 1",

                  "operation":"Add"

               }
            ]
        },
     {
            "source":{
                "id":"xxxxxx",
                "name":"IdentityNow",
                "type":"SOURCE"
            },
            "accountId":"xxxx",
            "accountOperation":"Modify",
            "provisioningResult":"SUCCESS",
            "provisioningTarget":"IdentityNow",
            "ticketId":"7263219262",
            "attributeRequests":[
                {
                    "operation":"Add",
                    "attributeName":"test2",
                    "attributeValue":"xxxxx"
                },
 				{

                  "attributeName":"test3",

                  "attributeValue":"Test 1",

                  "operation":"Add"

               }
            ]
        }
    ]
}

```

My JSONPath expression should only return first accountRequest object because the second one doesnt have any attributeRequests with attributeName == ‘assignedRoles’

---

<div class="post-metadata">

**Author:** ![Sivakrishna1993](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/sivakrishna1993/32/25424_2.png) [@Sivakrishna1993](https://developer.sailpoint.com/discuss/u/Sivakrishna1993)\
**Post date:** [March 14, 2024, 2:06pm UTC](https://developer.sailpoint.com/discuss/t/jsonpath-expression-to-find-elements-in-array-based-on-nested-array-values/35291/2 "2024-03-14T14:06:50Z")

</div>

Hi,

I am getting output without any error. Can you share error message what you are getting?

I used same path and checked in the JSON validator. Below is the result

```auto
[
	{
		"accountId": "xxxx",
		"provisioningResult": "SUCCESS",
		"provisioningTarget": "IdentityNow",
		"accountOperation": "Modify",
		"source": {
			"name": "IdentityNow",
			"id": "xxxxxx",
			"type": "SOURCE"
		},
		"attributeRequests": [
			{
				"attributeValue": "xxxxx",
				"attributeName": "memberOf",
				"operation": "Add"
			},
			{
				"attributeValue": "Test 1",
				"attributeName": "assignedRoles",
				"operation": "Add"
			}
		],
		"ticketId": "7263219262"
	},
	{
		"accountId": "xxxx",
		"provisioningResult": "SUCCESS",
		"provisioningTarget": "IdentityNow",
		"accountOperation": "Modify",
		"source": {
			"name": "IdentityNow",
			"id": "xxxxxx",
			"type": "SOURCE"
		},
		"attributeRequests": [
			{
				"attributeValue": "xxxxx",
				"attributeName": "test2",
				"operation": "Add"
			},
			{
				"attributeValue": "Test 1",
				"attributeName": "test3",
				"operation": "Add"
			}
		],
		"ticketId": "7263219262"
	}
]

```

---

<div class="post-metadata">

**Author:** ![maciej\_itrun](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@maciej\_itrun](https://developer.sailpoint.com/discuss/u/maciej_itrun)\
**Post date:** [March 14, 2024, 2:26pm UTC](https://developer.sailpoint.com/discuss/t/jsonpath-expression-to-find-elements-in-array-based-on-nested-array-values/35291/3 "2024-03-14T14:26:53Z")

</div>

> [@maciej\_itrun](#):
>
> ```auto
> {
> "accountRequests":[
> {
> "source":{
> "id":"xxxxxx",
> "name":"IdentityNow",
> "type":"SOURCE"
> },
> "accountId":"xxxx",
> "accountOperation":"Modify",
> "provisioningResult":"SUCCESS",
> "provisioningTarget":"IdentityNow",
> "ticketId":"7263219262",
> "attributeRequests":[
> {
> "operation":"Add",
> "attributeName":"memberOf",
> "attributeValue":"xxxxx"
> },
> {
> 
> "attributeName":"assignedRoles",
> 
> "attributeValue":"Test 1",
> 
> "operation":"Add"
> 
> }
> ]
> },
> {
> "source":{
> "id":"xxxxxx",
> "name":"IdentityNow",
> "type":"SOURCE"
> },
> "accountId":"xxxx",
> "accountOperation":"Modify",
> "provisioningResult":"SUCCESS",
> "provisioningTarget":"IdentityNow",
> "ticketId":"7263219262",
> "attributeRequests":[
> {
> "operation":"Add",
> "attributeName":"test2",
> "attributeValue":"xxxxx"
> },
> {
> 
> "attributeName":"test3",
> 
> "attributeValue":"Test 1",
> 
> "operation":"Add"
> 
> }
> ]
> }
> ]
> }
> 
> ```

I’m getting - jsonPath: Unexpected token ‘?’: \_$\_v.source.name == ‘IdentityNow’ && \_$\_v.provisioningTarget == ‘IdentityNow’ && \_$_v.attributeRequests[?(_$\_v.attributeName == ‘assignedRoles’

Which exact tool are you using to check JSONPath?

---

<div class="post-metadata">

**Author:** ![Sivakrishna1993](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/sivakrishna1993/32/25424_2.png) [@Sivakrishna1993](https://developer.sailpoint.com/discuss/u/Sivakrishna1993)\
**Post date:** [March 14, 2024, 2:39pm UTC](https://developer.sailpoint.com/discuss/t/jsonpath-expression-to-find-elements-in-array-based-on-nested-array-values/35291/4 "2024-03-14T14:39:16Z")

</div>

Hi,

I am using below tool.

> **[Online JsonPath Evaluator Tool| JavaInUse](https://www.javainuse.com/jsonpath)**
>
> Online JsonPath Evaluator Tool to evaluate JSONPath values and normalized paths

Best Regards,  
Siva.K

---

<div class="post-metadata">

**Author:** ![uday\_kilambi](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/uday_kilambi/32/31482_2.png) [@uday\_kilambi](https://developer.sailpoint.com/discuss/u/uday_kilambi)\
**Post date:** [March 14, 2024, 3:51pm UTC](https://developer.sailpoint.com/discuss/t/jsonpath-expression-to-find-elements-in-array-based-on-nested-array-values/35291/5 "2024-03-14T15:51:39Z")

</div>

@maciej_itrun ,

I had a similar requirement in the past where the output is to filtered from nested array of json objects. But, I couldn’t find a way in json path expression to achieve that. All I could do is one of the below approaches:

To get the attribute request information:  
$.accountRequests[?(@.source.name == “IdentityNow” && @.provisioningTarget == “IdentityNow”)].attributeRequests[?(@.attributeName == “assignedRoles”)]

To get the access request information based on the last entry of attributeRequest, as in my request I always have one entry, that didn’t cause any issue for me. -1 ensures to check on the last entry of the array. But, I hope that case doesn’t work for you.

$.accountRequests[?(@.source.name == “IdentityNow” && @.provisioningTarget == “IdentityNow” && @.attributeRequests[-1].attributeName == “assignedRoles”)]

I am eager to know if there is another supported json expression pattern which can support filtering inside the array.

Regards,  
Uday

---

<div class="post-metadata">

**Author:** ![uday\_kilambi](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/uday_kilambi/32/31482_2.png) [@uday\_kilambi](https://developer.sailpoint.com/discuss/u/uday_kilambi)\
**Post date:** [March 14, 2024, 4:08pm UTC](https://developer.sailpoint.com/discuss/t/jsonpath-expression-to-find-elements-in-array-based-on-nested-array-values/35291/6 "2024-03-14T16:08:18Z")

</div>

Hi @maciej_itrun ,

Finally I found the solution after some trail and error. This should work for you as well.  
Try this json path expression:

$.accountRequests[?(@.source.name == “IdentityNow” && @.provisioningTarget == “IdentityNow” && “assignedRoles” in @.attributeRequests…attributeName)]

For the nested array, we need to utilize the in expression to recursively search for attribute Name.

This will give the desired result of only the needed account request with assignedRoles attribute request in it. Here is the output I received when tried on [https://www.javainuse.com/jsonpath](https://www.javainuse.com/jsonpath)

[  
{  
“accountId”: “xxxx”,  
“provisioningResult”: “SUCCESS”,  
“provisioningTarget”: “IdentityNow”,  
“accountOperation”: “Modify”,  
“source”: {  
“name”: “IdentityNow”,  
“id”: “xxxxxx”,  
“type”: “SOURCE”  
},  
“attributeRequests”: [  
{  
“attributeValue”: “xxxxx”,  
“attributeName”: “memberOf”,  
“operation”: “Add”  
},  
{  
“attributeValue”: “Test 1”,  
“attributeName”: “assignedRoles”,  
“operation”: “Add”  
}  
],  
“ticketId”: “7263219262”  
}  
]

Regards,  
Uday Kilambi

---

<div class="post-metadata">

**Author:** ![maciej\_itrun](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@maciej\_itrun](https://developer.sailpoint.com/discuss/u/maciej_itrun)\
**Post date:** [March 15, 2024, 1:39pm UTC](https://developer.sailpoint.com/discuss/t/jsonpath-expression-to-find-elements-in-array-based-on-nested-array-values/35291/7 "2024-03-15T13:39:57Z")

</div>

Thank you very much @uday_kilambi !

---

<div class="post-metadata">

**Author:** ![maciej\_itrun](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@maciej\_itrun](https://developer.sailpoint.com/discuss/u/maciej_itrun)\
**Post date:** [March 15, 2024, 1:45pm UTC](https://developer.sailpoint.com/discuss/t/jsonpath-expression-to-find-elements-in-array-based-on-nested-array-values/35291/8 "2024-03-15T13:45:12Z")

</div>

I’m not sure however if IDN Workflows JSONPath accepts “in” operator.  
I received this error while trying to update my workflow with the provided JSONPath expression:

![image](https://global.discourse-cdn.com/sailpoint/original/2X/6/67bf57004b9a8029adbd9af309260f51dccd64e9.png)

---

<div class="post-metadata">

**Author:** ![uday\_kilambi](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/uday_kilambi/32/31482_2.png) [@uday\_kilambi](https://developer.sailpoint.com/discuss/u/uday_kilambi)\
**Post date:** [March 15, 2024, 1:46pm UTC](https://developer.sailpoint.com/discuss/t/jsonpath-expression-to-find-elements-in-array-based-on-nested-array-values/35291/9 "2024-03-15T13:46:27Z")

</div>

I didn’t try it out in IDN Workflow though, let me give a try

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sailpoint/original/2X/f/f2136700ed5e3703e0b85e02f6be799dacca7735.png) [@system](https://developer.sailpoint.com/discuss/u/system)\
**Post date:** [May 14, 2024, 1:47pm UTC](https://developer.sailpoint.com/discuss/t/jsonpath-expression-to-find-elements-in-array-based-on-nested-array-values/35291/10 "2024-05-14T13:47:19Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
