DBAtlas · Atlas für Datenbankbetrieb

JSON-Daten abfragen

Oracle

-- Extract a scalar value with JSON_VALUE:
SELECT JSON_VALUE(data, '$.customer.city') AS city FROM orders;
-- Expand into rows with JSON_TABLE:
SELECT t.product, t.quantity
FROM orders s,
     JSON_TABLE(s.data, '$.items[*]'
       COLUMNS (product VARCHAR2(50) PATH '$.product',
                quantity NUMBER PATH '$.quantity')) t;

Oracle (12c+) fragt JSON-Daten mit SQL/JSON-Pfadausdrücken ab. JSON_VALUE liefert einen einzelnen Skalarwert, JSON_QUERY ein Objekt-/Array-Fragment, JSON_TABLE wandelt JSON in eine relationale Zeilen-Spalten-Struktur um. Ab 21c wird ein nativer JSON-Datentyp unterstützt.

PostgreSQL

-- jsonb operators:
SELECT data->'customer'->>'city' AS city FROM orders;
-- Containment (@>) and a GIN index:
CREATE INDEX ix ON orders USING GIN (data);
SELECT * FROM orders
WHERE data @> '{"status": "approved"}';

Der PostgreSQL-Typ jsonb bietet umfangreiche Operatoren: -> (json-Objekt/-Element), ->> (Textwert), #> (Pfad), @> (Enthaltensein), ? (Schlüssel existiert). Ein GIN-Index beschleunigt @>- und ?-Abfragen. Auch SQL/JSON-Pfadabfragen sind mit jsonb_path_query möglich.

MySQL

-- JSON_EXTRACT or shorthand operators:
SELECT JSON_EXTRACT(data, '$.customer.city') AS city FROM orders;
-- ->> returns unquoted text:
SELECT data->>'$.customer.city' AS city FROM orders
WHERE data->>'$.status' = 'approved';

MySQL (5.7+) fragt den nativen JSON-Typ mit JSON_EXTRACT() ab; der Operator -> ist eine Kurzform für JSON_EXTRACT, ->> für JSON_UNQUOTE(JSON_EXTRACT()). Suchen auf JSON-Feldern lassen sich mit Generated Column + Index beschleunigen.

SQL Server

-- Extract a scalar value with JSON_VALUE:
SELECT JSON_VALUE(data, '$.customer.city') AS city FROM orders;
-- Expand into rows with OPENJSON:
SELECT k.product, k.quantity
FROM orders s
CROSS APPLY OPENJSON(s.data, '$.items')
  WITH (product NVARCHAR(50) '$.product', quantity INT '$.quantity') k;

SQL Server (2016+) speichert JSON in einer NVARCHAR-Spalte. JSON_VALUE liefert einen Skalarwert, JSON_QUERY ein Objekt/Array, OPENJSON wandelt JSON in Tabellenform um. ISJSON() validiert es. Zugriffe auf JSON-Pfade lassen sich mit Computed Column + Index beschleunigen.

MongoDB

// MongoDB is natively a document (BSON/JSON) database.
// Access nested fields with dot notation:
db.orders.find({ "customer.city": "Ankara" })
// Array element and projection:
db.orders.find(
  { "items.product": "pen" },
  { "items.$": 1 }
)

MongoDB speichert alle Daten als Dokumente (JSON-ähnliches BSON); die Abfrage ist nativ. Mit der Punktnotation erreichst du verschachtelte Felder und Array-Elemente. Komplexe Transformationen erfolgen mit der Aggregation Pipeline, Indizes ermöglichen schnelle Zugriffe auf verschachtelte Felder.

Redis

# Requires the RedisJSON module (Redis Stack).
# Write a JSON document:
JSON.SET order:1 $ '{"customer":{"city":"Ankara"},"status":"approved"}'
# Read a value with JSONPath:
JSON.GET order:1 $.customer.city

Core Redis hat keinen JSON-Typ; das Modul RedisJSON (Redis Stack) ermöglicht JSONPath-basiertes Lesen und Schreiben mit JSON.SET/JSON.GET. Zusammen mit RediSearch lassen sich JSON-Dokumente indexieren und abfragen. Ohne das Modul wird JSON als String gespeichert und in der Anwendung geparst.

Elasticsearch

# Documents are already JSON; query fields directly:
GET orders/_search
{ "query": { "term": { "customer.city": "Ankara" } } }
# For nested arrays, use the nested type and a nested query:
GET orders/_search
{ "query": { "nested": { "path": "items",
  "query": { "match": { "items.product": "pen" } } } } }

Elasticsearch speichert und indexiert Dokumente nativ als JSON. Um bei Arrays verschachtelter Objekte die Elementgrenzen zu erhalten, nutze den Feldtyp 'nested' und eine nested-Abfrage; andernfalls werden Arrays vom Typ object abgeflacht (Gefahr von Kreuztreffern). Die Unterscheidung object vs. nested ist für korrekte Ergebnisse entscheidend.

In DBAtlas öffnen →