Query and extract JSON data using PDOdb's JSON API.
use tommyknocker\pdodb\helpers\Db;
$users = $db->find()
->from('users')
->select([
'id',
'name',
'city' => Db::jsonGet('meta', ['city']),
'age' => Db::jsonGet('meta', ['age'])
])
->get();$products = $db->find()
->from('products')
->select([
'id',
'name',
'specs' => 'specs'
])
->get();
foreach ($products as $product) {
$specs = json_decode($product['specs'], true);
// Process parsed JSON
}// Find users older than 25 (from JSON metadata)
$adults = $db->find()
->from('users')
->where(Db::jsonPath('meta', ['age'], '>', 25))
->get();$active = $db->find()
->from('users')
->where(Db::jsonPath('meta', ['age'], '>', 25))
->andWhere(Db::jsonContains('tags', 'php'))
->andWhere(Db::jsonExists('meta', ['verified']))
->get();// Find users with 'php' in tags array
$phpDevs = $db->find()
->from('users')
->where(Db::jsonContains('tags', 'php'))
->get();// Find users with both 'php' and 'mysql' in tags
$fullStack = $db->find()
->from('users')
->where(Db::jsonContains('tags', ['php', 'mysql']))
->get();// Check if JSON object has specific key
$withCity = $db->find()
->from('users')
->where(Db::jsonContains('meta', 'city', ['location']))
->get();use tommyknocker\pdodb\helpers\Db;
$users = $db->find()
->from('users')
->orderBy(Db::jsonGet('meta', ['age']), 'DESC')
->get();$users = $db->find()
->from('users')
->orderBy(Db::jsonLength('tags'), 'DESC')
->get();$users = $db->find()
->from('users')
->select([
'id',
'name',
'tag_count' => Db::jsonLength('tags')
])
->get();$users = $db->find()
->from('users')
->select([
'id',
'meta_keys' => Db::jsonKeys('meta')
])
->get();$users = $db->find()
->from('users')
->select([
'id',
'tags_type' => Db::jsonType('tags')
])
->get();$users = $db->find()
->from('users')
->where(Db::jsonPath('profile', ['address', 'city'], '=', 'NYC'))
->get();
// JSON structure:
// { profile: { address: { city: "NYC" } } }// Users who have ALL of these skills
$experts = $db->find()
->from('users')
->where(Db::jsonContains('skills', ['php', 'mysql', 'docker']))
->get();$users = $db->find()
->from('users')
->select([
'id',
'name',
'skill_count' => Db::jsonLength('skills')
])
->orderBy('skill_count', 'DESC')
->get();MySQL:
SELECT JSON_EXTRACT(meta, '$.city') as city FROM usersPostgreSQL:
SELECT meta->>'city' as city FROM usersSQLite:
SELECT json_extract(meta, '$.city') as city FROM usersPDOdb handles this automatically.
- JSON Basics - Creating JSON data
- JSON Filtering - Advanced JSON queries
- JSON Modification - Update JSON values