0

I saved json data to a field in the database.I want to parse the json file with Oracle . How can I do it?

{
  "widget": {
    "debug": "on",
    "window": {
      "title": "Sample Konfabulator Widget",
      "name": "main_window",
      "width": 500,
      "height": 500
    },
    "image": {
      "src": "Images/Sun.png",
      "name": "sun1",
      "hOffset": 250,
      "vOffset": 250,
      "alignment": "center"
    },
    "text": {
      "data": "Click Here",
      "size": 36,
      "style": "bold",
      "name": "text1",
      "hOffset": 250,
      "vOffset": 100,
      "alignment": "center",
      "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
  }
}
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
mbilgil
  • 19
  • 1
  • 2
  • 6

1 Answers1

2

If you are using Oracle 12c or later then you can use JSON_TABLE:

SELECT *
FROM   JSON_TABLE(
         '{
           "widget": {
             "debug": "on",
             "window": {
               "title": "Sample Konfabulator Widget",
               "name": "main_window",
               "width": 500,
               "height": 500
             },
             "image": { 
               "src": "Images/Sun.png",
               "name": "sun1",
               "hOffset": 250,
               "vOffset": 250,
               "alignment": "center"
             },
             "text": {
               "data": "Click Here",
               "size": 36,
               "style": "bold",
               "name": "text1",
               "hOffset": 250,
               "vOffset": 100,
               "alignment": "center",
               "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
             }
           }
         }',
         '$.widget'
         COLUMNS
           debug       VARCHAR2(3)  PATH '$.debug',
           title       VARCHAR2(50) PATH '$.window.title',
           window_name VARCHAR2(20) PATH '$.window.name',
           width       NUMBER(5,0)  PATH '$.window.width',
           height      NUMBER(5,0)  PATH '$.window.height',
           src         VARCHAR2(50) PATH '$.image.src',
           image_name  VARCHAR2(20) PATH '$.image.name',
           hOffset     NUMBER(5,0)  PATH '$.image.hOffset',
           vOffset     NUMBER(5,0)  PATH '$.image.vOffset',
           align       VARCHAR2(10) PATH '$.image.alignment'
       );

which outputs:

DEBUG | TITLE                      | WINDOW_NAME | WIDTH | HEIGHT | SRC            | IMAGE_NAME | HOFFSET | VOFFSET | ALIGN 
:---- | :------------------------- | :---------- | ----: | -----: | :------------- | :--------- | ------: | ------: | :-----
on    | Sample Konfabulator Widget | main_window |   500 |    500 | Images/Sun.png | sun1       |     250 |     250 | center

db<>fiddle here

MT0
  • 143,790
  • 11
  • 59
  • 117