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

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 :

{
   "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’

1 Like

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

[
	{
		"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"
	}
]

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?

Hi,

I am using below tool.

Best Regards,
Siva.K

@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

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

[
{
“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

Thank you very much @uday_kilambiCTS !

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

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