简体   繁体   中英

Oracle Split Delimited String Into Rows

tough one here. We are running 11g, so we do not have JSON support right now. My goal is to split a JSON string on a delimiter into rows, then filter on each of those rows. Is this possible? Here is an example:

{
   "section_1": {
      "section_publish": true,
       "section_body": "<p style=\"text-align: justify;\">
    },
   "section_2": {
      "section_publish": false,
       "section_body": "<p style=\"text-align: justify;\">
    },
   "section_3": {
      "section_publish": true,
       "section_body": "<p style=\"text-align: justify;\">
    }
}

So, basically I am hoping to split the sections, on }, Then, once those are in "rows" filter on "section_publish": true, then filter on those.

The JSON strings are kept in a table, so this will be part of a SELECT statement:

SELECT id, name.....
FROM json_table
WHERE {json result from above} LIKE '%string to compare%';

Does it make sense? Can this be done in SQL?

The example below may not work for all variations of JSON that you may have, but this query does work for the example JSON provided.

Query

WITH
    json_test (json_val) AS (SELECT EMPTY_CLOB () || '{
  "section_1": {
    "section_publish": true,
    "section_body": "<p style=\"text-align: justify;\">"
  },
  "section_2": {
    "section_publish": false,
    "section_body": "<p style=\"text-align: justify;\">"
  },
  "section_3": {
    "section_publish": true,
    "section_body": "<p style=\"text-align: justify;\">"
  }
}' FROM DUAL),
    json_split
    AS
        (    SELECT TRIM ('"' FROM REGEXP_SUBSTR (json_val,
                                                  '".*"',
                                                  1,
                                                  1 + ((LEVEL - 1) * 3)))    AS section,
                    TRIM (TRIM (',' FROM SUBSTR (REGEXP_SUBSTR (json_val,
                                                                '"section_publish".*',
                                                                1,
                                                                LEVEL),
                                                 19)))                       AS section_publish,
                    REPLACE (
                        TRIM ('"' FROM TRIM (TRIM (',' FROM SUBSTR (REGEXP_SUBSTR (json_val,
                                                                                   '"section_body".*',
                                                                                   1,
                                                                                   LEVEL),
                                                                    16)))),
                        '\"',
                        '"')                                                 AS section_body
               FROM json_test
         CONNECT BY LEVEL < REGEXP_COUNT (json_val, '{'))
SELECT TO_CHAR (section)             AS section,
       TO_CHAR (section_publish)     AS section_publish,
       TO_CHAR (section_body)        AS section_body
  FROM json_split
 WHERE TO_CHAR (section_publish) = 'true';

Result

     SECTION    SECTION_PUBLISH                        SECTION_BODY
____________ __________________ ___________________________________
section_1    true               <p style="text-align: justify;">
section_3    true               <p style="text-align: justify;">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM