{"id":18258,"date":"2021-07-02T07:18:32","date_gmt":"2021-07-01T21:18:32","guid":{"rendered":"https:\/\/database.guide\/?p=18258"},"modified":"2024-04-28T11:48:43","modified_gmt":"2024-04-28T01:48:43","slug":"mariadb-json_array_insert-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/mariadb-json_array_insert-explained\/","title":{"rendered":"MariaDB JSON_ARRAY_INSERT() Explained"},"content":{"rendered":"\n<p>In MariaDB, <code>JSON_ARRAY_INSERT()<\/code> is a built-in function that inserts a value into a JSON document and returns the result. <\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JSON_ARRAY_INSERT(json_doc, path, value&#91;, path, value] ...)<\/code><\/pre>\n\n\n\n<p>Where <code>json_doc<\/code> is the JSON document, <code>path<\/code> is the path to where you want to insert the value\/s, and <code>value<\/code> is the value to insert.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s an example to demonstrate.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_ARRAY_INSERT('&#91;0, 1, 2]', '$&#91;1]', 3);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-------------------------------------------+\n| JSON_ARRAY_INSERT('[0, 1, 2]', '$[1]', 3) |\n+-------------------------------------------+\n| [0, 3, 1, 2]                              |\n+-------------------------------------------+<\/pre>\n\n\n\n<p>In this case, the value <code>3<\/code> was inserted into the the array at position <code>1<\/code> . Arrays are zero based, so it becomes the second element in the array.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Insert Multiple Values<\/h2>\n\n\n\n<p>You can insert multiple values within a single call to <code>JSON_ARRAY_INSERT()<\/code>. <\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_ARRAY_INSERT('&#91;0, 1, 2]', '$&#91;0]', 4, '$&#91;2]', 5);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+------------------------------------------------------+\n| JSON_ARRAY_INSERT('[0, 1, 2]', '$[0]', 4, '$[2]', 5) |\n+------------------------------------------------------+\n| [4, 0, 5, 1, 2]                                      |\n+------------------------------------------------------+<\/pre>\n\n\n\n<p>Evaluation is performed from left to right. <\/p>\n\n\n\n<p>Here&#8217;s what happens if we switch the <code>path<\/code>\/<code>value<\/code> arguments around:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_ARRAY_INSERT('&#91;0, 1, 2]', '$&#91;2]', 5, '$&#91;0]', 4);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+------------------------------------------------------+\n| JSON_ARRAY_INSERT('[0, 1, 2]', '$[2]', 5, '$[0]', 4) |\n+------------------------------------------------------+\n| [4, 0, 1, 5, 2]                                      |\n+------------------------------------------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Multiple Arrays<\/h2>\n\n\n\n<p>You can insert values into more than one array within the same call to <code>JSON_ARRAY_INSERT()<\/code>.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @json_doc = '{\"a\": &#91;0, 1], \"b\": &#91;2, 3]}';\n\nSELECT JSON_ARRAY_INSERT(@json_doc, '$.a&#91;1]', 4, '$.b&#91;2]', 5);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------------------------------------------------------+\n| JSON_ARRAY_INSERT(@json_doc, '$.a[1]', 4, '$.b[2]', 5) |\n+--------------------------------------------------------+\n| {\"a\": [0, 4, 1], \"b\": [2, 3, 5]}                       |\n+--------------------------------------------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Nested Arrays<\/h2>\n\n\n\n<p>Here&#8217;s an example of inserting a value into a nested array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @json_doc = '&#91;0, 1, &#91;2, 3]]';\n\nSELECT JSON_ARRAY_INSERT(@json_doc, '$&#91;2]&#91;1]', 4);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------------------------------------------+\n| JSON_ARRAY_INSERT(@json_doc, '$[2][1]', 4) |\n+--------------------------------------------+\n| [0, 1, [2, 4, 3]]                          |\n+--------------------------------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Larger JSON Document<\/h2>\n\n\n\n<p>Here&#8217;s an example with a slightly larger JSON document. <\/p>\n\n\n\n<p>I also use <code>JSON_DETAILED()<\/code> to prettify the result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @json_doc = '{  \n    \"pet\": {    \n       \"name\": \"Fluffy\", \n       \"diet\": &#91;\"Fish\", \"Water\"]  \n    }\n }';\nSELECT JSON_DETAILED(\n        JSON_ARRAY_INSERT(\n            @json_doc, \n            '$.pet.diet&#91;1]', \n            'Chicken')\n);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n    \"pet\": \n    {\n        \"name\": \"Fluffy\",\n        \"diet\": \n        [\n            \"Fish\",\n            \"Chicken\",\n            \"Water\"\n        ]\n    }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Null Arguments<\/h2>\n\n\n\n<p>If the first argument is <code>NULL<\/code>, the result is <code>NULL<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_ARRAY_INSERT(null, '$&#91;1]', 4);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+------------------------------------+\n| JSON_ARRAY_INSERT(null, '$[1]', 4) |\n+------------------------------------+\n| NULL                               |\n+------------------------------------+<\/pre>\n\n\n\n<p>The same applies to the <code>path<\/code> argument:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_ARRAY_INSERT('&#91;0, 1, 2, 3]', null, 4);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------------------------------------------+\n| JSON_ARRAY_INSERT('[0, 1, 2, 3]', null, 4) |\n+--------------------------------------------+\n| NULL                                       |\n+--------------------------------------------+<\/pre>\n\n\n\n<p>However, if the <code>value<\/code> argument is <code>NULL<\/code>, then <code>NULL<\/code> is inserted into to the array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_ARRAY_INSERT('&#91;0, 1, 2]', '$&#91;1]', null);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+----------------------------------------------+\n| JSON_ARRAY_INSERT('[0, 1, 2]', '$[1]', null) |\n+----------------------------------------------+\n| [0, null, 1, 2]                              |\n+----------------------------------------------+<\/pre>\n\n\n\n<p>You can also use <code><a href=\"https:\/\/database.guide\/mariadb-json_array_append-explained\/\" data-type=\"post\" data-id=\"18244\">JSON_ARRAY_APPEND()<\/a><\/code> to append values to an array.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MariaDB, JSON_ARRAY_INSERT() is a built-in function that inserts a value into a JSON document and returns the result.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[169],"class_list":["post-18258","post","type-post","status-publish","format-standard","hentry","category-mariadb","tag-arrays"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=18258"}],"version-history":[{"count":11,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18258\/revisions"}],"predecessor-version":[{"id":18275,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18258\/revisions\/18275"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=18258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=18258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=18258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}