# JSON_ARRAY_INSERT

> JSON_ARRAY_INSERT function in StackQL: insert values into a JSON array at given positions, shifting later elements.

Source: https://stackql.io/language-spec/functions/json/json_array_insert

Inserts one or more values into a JSON array at the given positions.  Existing elements at and after a position move one place to the right; `$[#]` appends to the end.

See also:  
[[` SELECT `]](/language-spec/select) [[` JSON_INSERT `]](/language-spec/functions/json/json_insert) [[` JSON_ARRAY_LENGTH `]](/language-spec/functions/json/json_array_length)

* * * 

## Syntax

```sql
SELECT JSON_ARRAY_INSERT(json_array, path, value [, path, value ...]) FROM <multipartIdentifier>;
```

## Arguments

__*json_array*__  
A JSON value containing the array.

__*path*__  
The JSON path of the insertion position, for example `$[0]`, `$[1]` or `$[#]` (after the last element).

__*value*__  
The value inserted at that position.

## Return Value(s)
Returns the modified JSON string.

* * *

## Examples

### Insert and append

The first call inserts at the second position, the second appends:

```sql
SELECT JSON_ARRAY_INSERT('[1, 2, 3]', '$[1]', 'x') AS a,
JSON_ARRAY_INSERT('[1, 2, 3]', '$[#]', 'end') AS b;
```

```
|---------------|-----------------|
|       a       |        b        |
|---------------|-----------------|
| [1,"x",2,3]   | [1,2,3,"end"]   |
|---------------|-----------------|
```

### Prepend to an array field

The following query adds a value at the start of the `topics` array of each repository in a GitHub organization:

```sql
SELECT name, JSON_ARRAY_INSERT(topics, '$[0]', 'stackql') AS topics
FROM github.repos.repos
WHERE org = 'stackql-labs';
```

For more information, see [https://www.sqlite.org/json1.html#jarrayins](https://www.sqlite.org/json1.html#jarrayins)
