MariaDBでは、JSON_INSERT() は、JSONドキュメントにデータを挿入し、結果を返す組み込み関数です。
構文
構文は次のようになります:
JSON_INSERT(json_doc, path, val[, path, val] ...)
json_docの場所 JSONドキュメント、path データを挿入する場所のパスであり、val そのパスに挿入される値です。
例
実例を示します。
SET @json_document = '
{
"name": "Wag"
}
';
SELECT JSON_INSERT(@json_document, '$.type', "Dog"); 結果:
+----------------------------------------------+
| JSON_INSERT(@json_document, '$.type', "Dog") |
+----------------------------------------------+
| {"name": "Wag", "type": "Dog"} |
+----------------------------------------------+
ここに"type": "Dog"を挿入しました ドキュメントに。
この場合、$.typeを使用しました パスとして。したがって、type キーであり、Dog は値です。
パスがすでに存在する場合
JSONドキュメントにすでに存在するパスを渡すと、元のドキュメントは変更されずに返されます。
例:
SET @json_document = '
{
"name": "Wag"
}
';
SELECT JSON_INSERT(@json_document, '$.name', "Bark"); 結果:
+-----------------------------------------------+
| JSON_INSERT(@json_document, '$.name', "Bark") |
+-----------------------------------------------+
| {"name": "Wag"} |
+-----------------------------------------------+ 配列の挿入
JSONドキュメントに配列を挿入する例を次に示します。
SET @json_document = '
{
"name" : "Wag"
}
';
SELECT JSON_INSERT(@json_document, '$.scores', '[ 8, 7, 9 ]'); 結果:
+--------------------------------------------------------+
| JSON_INSERT(@json_document, '$.scores', '[ 8, 7, 9 ]') |
+--------------------------------------------------------+
| {"name": "Wag", "scores": "[ 8, 7, 9 ]"} |
+--------------------------------------------------------+ 配列への追加
JSON_INSERT()の使用例を次に示します。 配列にデータを追加するには:
SET @json_document = '
{
"_id" : 1,
"awards" : [ "Top Dog", "Best Dog" ]
}
';
SELECT JSON_INSERT(@json_document, '$.awards[2]', "Biggest Dog"); 結果:
+--------------------------------------------------------------+
| JSON_INSERT(@json_document, '$.awards[2]', "Biggest Dog") |
+--------------------------------------------------------------+
| {"_id": 1, "awards": ["Top Dog", "Best Dog", "Biggest Dog"]} |
+--------------------------------------------------------------+ ただし、この例では正常に機能しましたが、簡単に失敗した可能性があります。たとえば、配列の別の場所に値を挿入しようとしても、機能しません。
SET @json_document = '
{
"_id" : 1,
"awards" : [ "Top Dog", "Best Dog" ]
}
';
SELECT JSON_INSERT(@json_document, '$.awards[1]', "Biggest Dog"); 結果:
+-----------------------------------------------------------+
| JSON_INSERT(@json_document, '$.awards[1]', "Biggest Dog") |
+-----------------------------------------------------------+
| {"_id": 1, "awards": ["Top Dog", "Best Dog"]} |
+-----------------------------------------------------------+
配列に値を挿入するには、JSON_ARRAY_INSERT()を使用します 代わりに機能します。
また、上記の例では配列に値を追加できましたが、JSON_ARRAY_APPEND()を使用した方がよいでしょう。 その目的のために特別に設計されているため、機能します。
ネストされたオブジェクト
別のオブジェクト内にネストされたオブジェクトに値を挿入する例を次に示します。
SET @json_document = '
{
"_id" : 1,
"name" : "Wag",
"details" : {
"type" : "Dog",
"weight" : 20,
"awards" : {
"Florida Dog Awards" : "Top Dog",
"New York Marathon" : "Fastest Dog"
}
}
}
';
SELECT JSON_INSERT(
@json_document,
'$.details.awards.Sumo 2020',
'Biggest Dog'
); 結果:
{"_id": 1, "name": "Wag", "details": {"type": "Dog", "weight": 20, "awards": {"Florida Dog Awards": "Top Dog", "New York Marathon": "Fastest Dog", "Sumo 2020": "Biggest Dog"}}} 結果をきれいにする
JSON_DETAILED()を使用できます 結果を読みやすくする関数:
SET @json_document = '
{
"_id" : 1,
"name" : "Wag",
"details" : {
"type" : "Dog",
"weight" : 20,
"awards" : {
"Florida Dog Awards" : "Top Dog",
"New York Marathon" : "Fastest Dog"
}
}
}
';
SELECT
JSON_DETAILED(
JSON_INSERT(
@json_document,
'$.details.awards.Sumo 2020',
'Biggest Dog'
)
); 結果:
{
"_id": 1,
"name": "Wag",
"details":
{
"type": "Dog",
"weight": 20,
"awards":
{
"Florida Dog Awards": "Top Dog",
"New York Marathon": "Fastest Dog",
"Sumo 2020": "Biggest Dog"
}
}
} ヌル引数
json_documentのいずれか またはpath 引数はNULLです 、結果はNULLです :
SELECT
JSON_INSERT(null, '$.type', 'Dog'),
JSON_INSERT('{"a":1}', null, 'Dog'); 結果:
+------------------------------------+-------------------------------------+
| JSON_INSERT(null, '$.type', 'Dog') | JSON_INSERT('{"a":1}', null, 'Dog') |
+------------------------------------+-------------------------------------+
| NULL | NULL |
+------------------------------------+-------------------------------------+
ただし、value 引数はNULLです 、キーは指定されたパスにnullの値で追加されます :
SELECT JSON_INSERT('{"a":1}', '$.type', null); 結果:
+----------------------------------------+
| JSON_INSERT('{"a":1}', '$.type', null) |
+----------------------------------------+
| {"a": 1, "type": null} |
+----------------------------------------+ パラメータカウントが正しくありません
引数を指定しないとエラーが発生します:
SELECT JSON_INSERT(); 結果:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_INSERT'
提供する引数が少なすぎても多すぎても同じです:
SELECT JSON_INSERT('{ "a": 1}'); 結果:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_INSERT'
同様の機能
JSON_REPLACE() 関数は既存のデータを更新できます。
JSON_SET() 関数は、既存のデータを更新し、新しいデータを挿入できます。したがって、JSON_SET() JSON_INSERT()のようなものです およびJSON_REPLACE() 1つの機能で。