3

I have a table called DISCOUNT_CODES with two columns, one having JSON values:

CODE           ACTIVE
1234           {"active":"Y"}
2468           {"active":"N"}
1359           {"active":"Y"}

As the second column is in JSON, I was wondering if there was a way I can select the codes that are active without having to do a select query like:

SELECT CODE FROM DISCOUNT_CODES WHERE ACTIVE = '{"active":"Y"}';

In pseudo-code, I am wondering if something like this is possible:

SELECT CODE FROM DISCOUNT_CODES WHERE JSON(ACTIVE $.active) = "Y";

Edit: I am using Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production

2
  • 1
    what version db do you have? the native JSON() support in Oracle starts with 12c, before that you might need a helper package (APEX has one for example) Commented Aug 6, 2020 at 20:03
  • I am using Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production Commented Aug 6, 2020 at 20:10

2 Answers 2

1

Yes, you can use a SELECT statement containing JSON_TABLE() function for the DB version 12c+ such as the one below :

SELECT d.code
  FROM discount_codes d
 CROSS JOIN JSON_TABLE(active, '$' COLUMNS (
                                            active VARCHAR2(100) PATH '$.active'
                                           )
                      ) j
 WHERE j.active = 'Y' 

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

You can also use JSON_EXISTS

SELECT CODE FROM DISCOUNT_CODES
WHERE json_exists(ACTIVE, '$?(@.active == "Y")');

Comments

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.