JSON_QUERY() zieht ein Objekt oder ein Array aus einem JSON-Dokument, und zwar entlang des Pfades, den man angibt. Es ist das Geschwister von JSON_VALUE() mit einem wesentlichen Unterschied: JSON_VALUE() liefert Skalare, JSON_QUERY() die strukturierten Gebilde.
Syntax
JSON_QUERY(json_doc, path)
Ein verschachteltes Objekt greifen
SET @json_document = ' { "_id" : 1, "name" : "Wag", "details" : { "type" : "Dog", "weight" : 20, "awards" : { "Florida Dog Awards" : "Top Dog", "New York Marathon" : "Fastest Dog", "Sumo 2020" : "Biggest Dog" } } } '; SELECT JSON_QUERY(@json_document, '$.details');
Ergebnis:
{ "type" : "Dog", "weight" : 20, "awards" : { "Florida Dog Awards" : "Top Dog", "New York Marathon" : "Fastest Dog", "Sumo 2020" : "Biggest Dog" } }
Verlängert man den Pfad per Punktnotation, geht es eine Ebene tiefer:
SET @json_document = ' { "_id" : 1, "name" : "Wag", "details" : { "type" : "Dog", "weight" : 20, "awards" : { "Florida Dog Awards" : "Top Dog", "New York Marathon" : "Fastest Dog", "Sumo 2020" : "Biggest Dog" } } } '; SELECT JSON_QUERY(@json_document, '$.details.awards');
Ergebnis:
{ "Florida Dog Awards" : "Top Dog", "New York Marathon" : "Fastest Dog", "Sumo 2020" : "Biggest Dog" }
Arrays
SET @json_document = ' { "_id" : 1, "awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ] } '; SELECT JSON_QUERY(@json_document, '$.awards');
Ergebnis:
+------------------------------------------+ | JSON_QUERY(@json_document, '$.awards') | +------------------------------------------+ | [ "Top Dog", "Best Dog", "Biggest Dog" ] | +------------------------------------------+
Um ein einzelnes Element statt des ganzen Arrays zu holen, verwenden Sie JSON_VALUE().
Pfade, die es nicht gibt
SET @json_document = ' { "_id" : 1, "awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ] } '; SELECT JSON_QUERY(@json_document, '$.type');
Ergebnis:
+--------------------------------------+ | JSON_QUERY(@json_document, '$.type') | +--------------------------------------+ | NULL | +--------------------------------------+
Skalare Werte liefern NULL
SELECT JSON_QUERY('{ "weight": 10 }', '$.weight');
Ergebnis:
+--------------------------------------------+ | JSON_QUERY('{ "weight": 10 }', '$.weight') | +--------------------------------------------+ | NULL | +--------------------------------------------+
Ist das Ziel ein Skalar, greifen Sie zu JSON_VALUE().
NULL-Argumente
SELECT JSON_QUERY(null, '$.type'), JSON_QUERY('{"a":1}', null);
Ergebnis:
+----------------------------+-----------------------------+ | JSON_QUERY(null, '$.type') | JSON_QUERY('{"a":1}', null) | +----------------------------+-----------------------------+ | NULL | NULL | +----------------------------+-----------------------------+
Fehler bei der Argumentanzahl
SELECT JSON_QUERY();
Ergebnis:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_QUERY'
Zu wenige Argumente ergeben denselben Fehler:
SELECT JSON_QUERY('{ "a": 1}');
Ergebnis:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_QUERY'