Need help filtering the result with JSON Array

Hi

I have the below document structure .

{
		"account": "ABC",
		"eLimits": [
			{
				"amount": 213,
				"cId": 3
			},
			{
				"amount": 213,
				"cId": 2
			}
		]
	}

My requirement is fetch account and eLimits information which have CId=3 .

The result for the Query -
select account,eLimits from data
, without any filter conditon , fetches below result
I need to add filter condition to this query , so that it will only have any eLimits with CId= 3

[
	{
		"account": "ABC",
		"eLimits": [
			{
				"amount": 213,
				"cId": 3
			},
			{
				"amount": 213,
				"cId": 2
			}
		]
	},
	{
		"account": "CDE",
		"eLimits": [
			{
				"amount": 213,
				"cId": 3
			},
			{
				"amount": 213,
				"cId": 1
			}
		]
	},
	{
		"account": "FGH",
		"eLimits": [
			{
				"amount": 213,
				"cId": 3
			},
			{
				"amount": 213,
				"cId": 4
			}
		]
	}
]

Desire Output:

[
	{
		"account": "ABC",
		"eLimits": [
			{
				"amount": 213,
				"cId": 3
			}
		]
	},
	{
		"account": "CDE",
		"eLimits": [
			{
				"amount": 213,
				"cId": 3
			}
		]
	},
	{
		"account": "FGH",
		"eLimits": [
			{
				"amount": 213,
				"cId": 3
			}
		]
	}
]
  • select p.account,OBJECT_CONCAT({“amount”:c.amount},{“cId”:c.cId}) AS eLimits
    from test as u
    unnest u as p
    unnest p. eLimits as c
    where c.cId =3

OR

select p.account, ARRAY_AGG({c.cId, c.amount}) as eLimits
from test as u
unnest u as p
unnest p.eLimits as c
where c.cId =3
group by p

There may be an easy way.
This is what I can think of right now
I hope it helps

SELECT d.*, ARRAY v FOR v IN d.eLimits WHEN e.cId = 3 END AS eLimits
FROM default AS d
WHERE  .....

OR

SELECT d.*,
      (SELECT e.* FROM d.eLimits AS e WHERE e.cId = 3) AS eLimits
FROM default AS d
WHERE  .....

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