Working with JSON data across all six database dialects (MySQL, MariaDB, PostgreSQL, SQLite, MSSQL, Oracle).
PDOdb provides a unified API for JSON operations that works seamlessly across:
- MySQL: Native JSON type
- PostgreSQL: JSONB type
- SQLite: TEXT type with JSON functions
- MSSQL: NVARCHAR(MAX) or JSON type
- Oracle: VARCHAR2(4000) IS JSON or JSON type (12c+)
All examples work identically on all six databases.
Creating and storing JSON data.
Topics covered:
- Creating tables with JSON columns
- Storing JSON data with
json_encode() - Inserting records with JSON fields
- Basic JSON column queries
- JSON data types across dialects
- Auto-detection of JSON columns
Querying and filtering JSON data.
Topics covered:
- Extracting JSON values with
jsonExtract() - Filtering by JSON values with
jsonContains() - Checking JSON key existence with
jsonExists() - Array operations with
jsonArrayContains() - Complex JSON path queries
- Nested JSON navigation
- WHERE conditions with JSON
Modifying JSON data using helper methods.
Topics covered:
- Setting JSON values with
Db::jsonSet() - Removing JSON paths with
Db::jsonRemove() - Replacing JSON values with
Db::jsonReplace() - Creating nested paths
- Removing array elements
- Comparison between
jsonSetandjsonReplace - Complex nested JSON operations
PDOdb provides these JSON helpers (from Db class):
jsonExtract(column, path)- Extract value from JSONjsonUnquote(column, path)- Extract and unquote value
jsonContains(column, value, path?)- Check if JSON contains valuejsonExists(column, path)- Check if path existsjsonArrayContains(column, value)- Check if array contains value
jsonLength(column, path?)- Get JSON array lengthjsonType(column, path?)- Get JSON typejsonKeys(column, path?)- Get object keys
Db::jsonSet(column, path, value)- Set JSON value (creates path if missing)Db::jsonRemove(column, path)- Remove JSON pathDb::jsonReplace(column, path, value)- Replace JSON value (only if path exists)
php 01-json-basics.phpPDODB_DRIVER=mysql php 01-json-basics.phpPDODB_DRIVER=pgsql php 01-json-basics.phpPDODB_DRIVER=sqlsrv php 01-json-basics.phpPDODB_DRIVER=oci php 01-json-basics.phpWhile the API is unified, there are some internal differences:
| Feature | MySQL | PostgreSQL | SQLite | MSSQL | Oracle |
|---|---|---|---|---|---|
| Native type | JSON | JSONB | TEXT | NVARCHAR(MAX)/JSON | VARCHAR2(4000) IS JSON |
| Path syntax | $.path |
Array notation | $.path |
$.path |
$.path |
| Performance | Good | Excellent | Good | Good | Good |
| Indexing | Supported | Supported | Limited | Supported | Limited |
For more JSON operations, see: