1

I have below JSON from which i need to fetch the value of issuedIdentValue where issuedIdentType = PANCARD

{
    "issuedIdent": [ 
        {"issuedIdentType":"DriversLicense","issuedIdentValue":"9797979797979797"},
        {"issuedIdentType":"SclSctyNb","issuedIdentValue":"078-01-8877"},
        {"issuedIdentType":"PANCARD","issuedIdentValue":"078-01-8877"}
    ]
}

I can not hard-code the index value [2] in my below query as the order of these records can be changed. So want to get rid off any hardcoded index.

select json_value(
    '{"issuedIdent": [{"issuedIdentType":"DriversLicense","issuedIdentValue":"9797979797979797"},{"issuedIdentType":"SclSctyNb","issuedIdentValue":"078-01-8877"},  {"issuedIdentType":"PANCARDSctyNb","issuedIdentValue":"078-01-8877"}]}', 
    '$.issuedIdent[2].issuedIdentValue'
) as output 
from d1entzendev.ExternalEventLog 
where 
    eventname = 'CustomerDetailsInqSVC' 
    and applname = 'digitalBANKING' 
    and requid = '4fe1fa1b-abd4-47cf-834b-858332c31618';

What changes will need to apply in json_value function to achieve the expected result

1 Answer 1

1

In Oracle 12c or higher, you can use JSON_TABLE() for this:

select value
from json_table(
    '{"issuedIdent": [{"issuedIdentType":"DriversLicense","issuedIdentValue":"9797979797979797"},{"issuedIdentType":"SclSctyNb","issuedIdentValue":"078-01-8877"},  {"issuedIdentType":"PANCARD","issuedIdentValue":"078-01-8877"}]}',
    '$.issuedIdent[*]' columns
         type   varchar(50) path '$.issuedIdentType',
         value  varchar(50) path '$.issuedIdentValue'
) t
where type = 'PANCARD'

This returns:

| VALUE       |
| :---------- |
| 078-01-8877 |
Sign up to request clarification or add additional context in comments.

3 Comments

If i have to get 5 different array sets from same JSON then i will have to run 5 SQL statements. Isn't there any other solution in which i could get the expected result from json_value or any other single function...Just want to avoid multiple select statements
any suggestion on my previous comment?
@deltaforce: without seeing actual data, I can’t really tell. I would suggest that you ask a new question for this, providing proper sample data and desired results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.