Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep JSON in oracle database

Tags:

java

json

oracle

I have some JSON data in Java and I want to keep in an oracle database. I want to keep it in one specific field because it will not be queried. What oracle datatype should be used for that column to store JSON format? Should i convert it to string and keep it as that or is there something specific for JSON in oracle.

like image 450
user1985273 Avatar asked Mar 24 '15 14:03

user1985273


People also ask

How to work with JSON data in Oracle Database?

In general, you will perform the following tasks when working with JSON data in Oracle Database: (1) create a JSON column with an is json check constraint, (2) insert JSON data into the column, and (3) query the JSON data.

Can we store data in JSON?

Unlike relational data, both can be stored, indexed, and queried without any need for a schema that defines the data. Oracle Database supports JSON natively with relational database features, including transactions, indexing, declarative querying, and views.

What is JSON_transform function in Oracle?

The JSON_TRANSFORM function was introduced in Oracle database 21c to simplify the modification of JSON data. Oracle database 19c introduced the JSON_MERGEPATCH function for updating JSON documents.

What is JSON_query in SQL?

Oracle SQL Function JSON_VALUE – select a scalar value from some JSON data, as a SQL value. Oracle SQL Function JSON_QUERY – select one or more values from some JSON data, as a SQL string representing the JSON values. Used especially to retrieve fragments of a JSON document, typically a JSON object or array.


2 Answers

If it is JSON then it is a string. So I would consider the standard Oracle text types for storing it, for example CLOB like the comments suggested. Also the official Oracle documentation of storing JSON data in Oracle can come handy.

like image 125
ytg Avatar answered Nov 12 '22 13:11

ytg


Oracle Database supports JSON data natively with relational database features, including transactions, indexing, declarative querying, and views. Unlike XML data, which is stored using SQL data type XMLType, JSON data is stored in Oracle Database using SQL data types VARCHAR2, CLOB, and BLOB. When possible, Oracle recommends that you use BLOB storage. In particular, doing so obviates the need for any character-set conversion (see JSON: Character Sets and Character Encoding in Oracle Database).

Maximum size of a VARCHAR2 or NVARCHAR2 column is only 4kb. Under 12c, if you have the MAX_STRING_SIZE server property set to EXTENDED, this limit can be increased to 32kb, but still nowhere near 150kb.

Using IS JSON in a Check Constraint to Ensure JSON Data is Strictly Well-Formed (Standard)

CREATE TABLE DTI_REPORT
   (id          RAW (16) NOT NULL,
    date_loaded TIMESTAMP WITH TIME ZONE,
    dti_document CLOB
    CONSTRAINT ensure_json CHECK (dti_document IS JSON (STRICT)));


INSERT INTO DTI_REPORT VALUES (
          SYS_GUID(),
          SYSTIMESTAMP,
          '{"Person"           : {"name":"Musa","age":45},
            "Report"           : "DTI0001",
            "Loans"            : {"loan1":{...},"loan2":{...}},
            "Salary"           : 4000,
            "DTI"           : "30%"}'
);
like image 30
Musa Avatar answered Nov 12 '22 14:11

Musa