JSON

JSON_ARRAY_INSERT

MySQLMySQL

Inserts a value at a specified position within a JSON array. Elements after the insertion point shift to the right.

Tipe hasil: JSONDiperbarui: 13 Jun 2026

Syntax

SQL
JSON_ARRAY_INSERT(json_doc, path, val[, path, val] ...)

Parameter

json_docJSONwajib

The source JSON document

pathVARCHARwajib

Path with array index ($.arr[index])

valanywajib

The value to insert

Contoh Penggunaan

Insert at Index

SQL
1SELECT JSON_ARRAY_INSERT('["a", "b", "c"]', '$[1]', 'x') AS result;

Inserts 'x' at index 1.

Hasil
["a", "x", "b", "c"]

Insert at Beginning

SQL
1SELECT JSON_ARRAY_INSERT('[1, 2, 3]', '$[0]', 0) AS result;

Inserts at the beginning of the array (unshift).

Hasil
[0, 1, 2, 3]

Insert in Nested Array

SQL
1SELECT JSON_ARRAY_INSERT('{"items": [1, 2, 3]}', '$.items[1]', 1.5) AS result;

Inserts into a nested array.

Hasil
{"items": [1, 1.5, 2, 3]}

Pertanyaan Umum tentang JSON_ARRAY_INSERT

Apa itu fungsi JSON_ARRAY_INSERT di MySQL?
Inserts a value at a specified position within a JSON array. Elements after the insertion point shift to the right. Di MySQL, fungsi JSON_ARRAY_INSERT termasuk dalam kelompok fungsi yang sering digunakan untuk mengolah dan menganalisis data secara efisien.
Bagaimana cara menggunakan JSON_ARRAY_INSERT di MySQL?
Gunakan sintaks berikut: JSON_ARRAY_INSERT(json_doc, path, val[, path, val] ...). Pastikan argumen yang dimasukkan sudah sesuai dengan tipe data yang diharapkan.
Apa nilai yang dikembalikan oleh fungsi JSON_ARRAY_INSERT?
Fungsi JSON_ARRAY_INSERT mengembalikan nilai bertipe JSON. Pastikan tipe data hasil sudah sesuai dengan kebutuhan query atau formula kamu. Jika input mengandung nilai NULL, perilaku fungsi dapat berbeda — selalu periksa dokumentasi untuk memastikan hasilnya sesuai ekspektasi.
Apa saja parameter fungsi JSON_ARRAY_INSERT?
Fungsi ini memiliki 3 parameter wajib. Parameter yang digunakan: json_doc (JSON, wajib): The source JSON document; path (VARCHAR, wajib): Path with array index ($.arr[index]); val (any, wajib): The value to insert.
Ngulik Langsung

Sudah paham JSON_ARRAY_INSERT? Latih langsung di browser

Latihan interaktif, langsung di browser.

Latihan Langsung →