Skip to content

JSON Functions and Dot Notation

Machbase provides functions and JSON dot notation for querying and modifying data in JSON columns.

Quick Reference

Function/NotationSyntaxDescription
JSON dot notationcol.keyExtract an object key’s value
JSON_EXTRACTJSON_EXTRACT(doc, path)Extract a path value as a JSON string
JSON_EXTRACT_STRINGJSON_EXTRACT_STRING(doc, path)Extract a path value as a string
JSON_EXTRACT_INTEGERJSON_EXTRACT_INTEGER(doc, path)Extract a path value as an integer
JSON_EXTRACT_DOUBLEJSON_EXTRACT_DOUBLE(doc, path)Extract a path value as a floating-point number
JSON_TYPEOFJSON_TYPEOF(doc, path)Check the type at a JSON path
JSON_IS_VALIDJSON_IS_VALID(json_text)Validate a JSON string
JSON_SETJSON_SET(doc, path, scalar)Set a scalar value at a JSON path
JSON_SET_JSONJSON_SET_JSON(doc, path, json_text)Set a JSON subtree at a path
JSON_REMOVEJSON_REMOVE(doc, path)Remove a member at a JSON path

The path argument to JSON_TYPEOF is required. Use JSON_TYPEOF(doc, '$') to check the type of the entire document.


JSON Dot Notation

Append a dot (.) and key name to a JSON column to query that key’s value. Use this to access JSON members without writing a JSONPath string.

json_column.key
-- Extract a key from a JSON column
SELECT data.temperature AS temp FROM sensor_log;

-- Use in WHERE
SELECT * FROM sensor_log
 WHERE data.status = 'active';

To specify a JSONPath string explicitly, use the -> operator, as in data -> '$.temperature'. Keep this distinct from dot notation above.


JSON_SET

Stores a SQL scalar as a JSON scalar at the specified document path.

JSON_SET(json_doc, path, scalar)
  • path must be a full JSONPath, such as $.key.subkey.
  • JSON_SET(..., path, NULL) stores JSON null.
  • If the JSON document argument is SQL NULL, the result is SQL NULL.
  • Array element updates such as $.items[0] are not supported.
Mach> SELECT JSON_SET('{"ship":{"status":"READY"}}', '$.ship.status', 'DONE') FROM dual;
{"ship":{"status":"DONE"}}

Mach> SELECT JSON_SET('{"count":0}', '$.count', 42) FROM dual;
{"count":42}

JSON_SET_JSON

Parses the third argument as JSON text and stores an object or array subtree.

JSON_SET_JSON(json_doc, path, json_text)
  • If the third argument is SQL NULL, the result is SQL NULL.
  • Invalid JSON text causes an error.
  • Array element updates are not supported.
Mach> SELECT JSON_SET_JSON('{"ship":{}}', '$.ship.owner', '{"name":"machbase"}') FROM dual;
{"ship":{"owner":{"name":"machbase"}}}

Mach> SELECT JSON_SET_JSON('{"tags":{}}', '$.tags.sensors', '[1,2,3]') FROM dual;
{"tags":{"sensors":[1,2,3]}}

JSON_REMOVE

Removes a member or subpath from a JSON document.

JSON_REMOVE(json_doc, path)
  • path must be a full JSONPath.
  • A missing path is a no-op.
  • JSON_REMOVE(..., '$') is not allowed.
  • If the JSON document argument is SQL NULL, the result is SQL NULL.
Mach> SELECT JSON_REMOVE('{"owner":{"name":"machbase","team":"db"}}', '$.owner.team') FROM dual;
{"owner":{"name":"machbase"}}

Mach> SELECT JSON_REMOVE('{"a":1,"b":2}', '$.a') FROM dual;
{"b":2}

JSON Insertion Example

-- LOG table with a JSON column
CREATE LOG TABLE device_log (
    ts    DATETIME,
    data  JSON
);

-- Insert JSON data
INSERT INTO device_log VALUES (NOW, '{"temperature":23.5,"humidity":60,"status":"active"}');

-- Extract values with JSON dot notation
SELECT ts, data.temperature AS temp
  FROM device_log
 WHERE data.status = 'active';

JSON Support by Table Type

Table TypeJSON ColumnsJSON Path QueriesNotes
TAGOOJSON columns and functions supported; JSON PK not supported
LOGOOFully supported
LOOKUPOOOrdinary columns supported; JSON path indexes not supported
VOLATILEXXCannot create JSON columns
TRANSACTIONOOFully supported

For details, see JSON Support by Table Type.

Last updated on