Is JSON PLSQL like XSLT?

In my last ODTUG CodeTalk about PLSQL JSON an attendee asked if it was like XSLT. I’m not familiar with XSLT so I looked it up and found these definitions on the w3schools site.

  • XSL (eXtensible Stylesheet Language) is a styling language for XML
  • XSLT stands for XSL Transformations

To me this means that XSLT is passed some XML and it transforms it into some different XML. The PLSQL JSON functionality can do that for JSON data (amongst many other things).

Here is a simple example:

SQL> CREATE TABLE json_data
 2   ( json_pk NUMBER NOT NULL PRIMARY KEY,
 3     json_col CLOB
 4   CONSTRAINT is_json CHECK ( json_col IS JSON ) );
Table created.

SQL> INSERT INTO json_data
 2   VALUES(1,
 3   '{"KEY1":"VALUE1"}');
1 row created.

SQL> CREATE OR REPLACE FUNCTION xslt ( p_json CLOB )
 2                     RETURN CLOB IS
 3     v_object JSON_OBJECT_T;
 4   BEGIN
 5     v_object := JSON_OBJECT_T.PARSE(p_json);
 6     IF v_object.HAS('KEY1') THEN
 7       v_object.PUT('KEY1','V1');
 8       v_object.RENAME_KEY('KEY1','K1');
 9     END IF;
 10    RETURN(v_object.TO_CLOB);
 11  END;
 12 /
Function created.

SQL> SELECT json_col,
 2          xslt(json_col) xslt
 3    FROM json_data;

JSON_COL             XSLT
-------------------- --------------------
{"KEY1":"VALUE1"}    {"K1":"V1"}

I hope this answers the attendee’s question.

Thanks for reading!