First, if your column datatype is JSON rather than JSONB, you can only create index
on the whole column demo.elements. Only JSONB columns can have indices on json keys.
You might consider to change your datatype if you specify JSON in this table.
Then, I modify your situation into a test case as follows.
create table demo(
elements jsonb
);
insert into demo values(
'{
"data": [
{
"ownr": "1",
"sigUsr": [
2
],
"sigStat": "APPR",
"modifiedOn": 1494229698039,
"isDel": "false",
"parentId": "nil",
"disName": "exmp.json",
"uniqueId": "d88cb52",
"usrType": "owner",
"usrId": "1",
"createdOn": 1494229698039,
"obType": "file"
}
]
}'
);
And query you ask can be achieved in the following ways as I can imagine.
postgres=# -- First possible query
postgres=# select elements->'data'->0->'usrId', elements->'data'->0->'sigUsr' from demo;
?column? | ?column?
----------+----------
"1" | [2]
(1 row)
postgres=# -- Second possible query, with jsonb_array_elements()
postgres=# select obj->>'usrId', obj->>'sigUsr' from demo d, jsonb_array_elements(d.elements->'data') as obj;
?column? | ?column?
----------+----------
1 | [2]
(1 row)
postgres=#
I can only create index with the first one, which is a restrictive use case. you need to write specific array entry in your index (in this case is the 0th element).
postgres=# create index ON demo ((elements->'data'->0->'usrId'));
CREATE INDEX
postgres=#
I can not create index for the second approach since jsonb_array_elements() returns setof jsonb type.
postgres=# create index ON demo ((jsonb_array_elements(elements->'data')->>'usrId'));
ERROR: set-returning functions are not allowed in index expressions
LINE 1: create index ON demo ((jsonb_array_elements(elements->'data'...
^
postgres=#
I think you should store sub-json entries in individual rows rather than store them into a json array.