I want to store raw API responses in Clobs, and through a procedure parse the json contained in the Clobs in a generic manner - meaning I have to construct the json-format dynamically. One method of doing this would be to regex the first Key:Value pairs, and extract one line of all the Keys as the Column Headers. These Column Headers could then be used in some way to dynamically create the json-format, and the rest of the json in the given clob could then be parsed.
I have this standard snippet here for parsing a known json-string:
SELECT * FROM json_table((select json_response from Some_Table where Condition=#input_parameter#), '$[*]'
COLUMNS
column1 varchar(256) PATH '$.column1',
column2 varchar(256) PATH '$.column2',
column3 varchar(256) PATH '$.column3',
)
Here, column1, column2, and column3 are known and defined. Say that a different Clob is selected, with additional columns - I would need to regex the key-values from the json as column headers, then dynamically create the column definition seen above (standard type varchar(256) to keep it simple).
The Json within the clob is a list of non-nested json-strings. The json has this format, always:
[{"column1":value1,"column2":"value2","column3":value3}, {...}, {...}, ...]
So the regex would capture all key values between the first {}-pairs, and extract the values of all fields to the left of the semi-colon separating key-value pairs.
Going from here, how can I dynamically create the column definition from the regex results?
The given example data above provides this table with the above query:
<table style="width: 5%;" border="2" cellpadding="5">
<tbody>
<tr>
<td> column1 </td>
<td> column2 </td>
<td> column3 </td>
</tr>
<tr>
<td> value1</td>
<td> value2</td>
<td> value3</td>
</tr>
</tbody>
</table>
Given a clob containing any number of columns, I need to extract the columns in a similar way and create a table, like this:
<table style="width: 5%;" border="2" cellpadding="5">
<tbody>
<tr>
<td style="width: 20%;"> column1</td>
<td style="width: 16.2539%;">column2 </td>
<td style="width: 16.7461%;">column3 </td>
<td style="width: 17%;">... </td>
<td style="width: 19%;">column_n </td>
</tr>
<tr>
<td style="width: 20%;"> value1</td>
<td style="width: 16.2539%;"> value2</td>
<td style="width: 16.7461%;"> value3</td>
<td style="width: 17%;">...</td>
<td style="width: 19%;"> value</td>
</tr>
<tr>
<td style="width: 20%;"> ...</td>
<td style="width: 16.2539%;"> ... </td>
<td style="width: 16.7461%;"> ...</td>
<td style="width: 17%;">... </td>
<td style="width: 19%;"> ...</td>
</tr>
<tr>
<td style="width: 20%;"> value</td>
<td style="width: 16.2539%;"> value</td>
<td style="width: 16.7461%;"> value</td>
<td style="width: 17%;">... </td>
<td style="width: 19%;"> value</td>
</tr>
</tbody>
</table>