JSON.ARRINSERT
Since 1.1.0JSONChanged in 1.2.1
Syntax
JSON.ARRINSERT key path index value [value ...]History
- 1.2.1: arity: -2 -> -5
Inserts one or more values into each array path matches, before the element at index. The values go in together, in the order given, and the elements from index on move up to make room.
index0inserts at the front. The array's length inserts at the end, asJSON.ARRAPPENDdoes.- A negative
indexcounts from the end:-1inserts before the last element. - An
indexoutside-lengthtolengthis out of bounds. That array is left alone: a JSONPath reportsnilfor it, and a legacy path fails with an error.
Each value is JSON text, so a string needs its quotes ('"first"'). A JSONPath inserts into every array it matches; a legacy path into the first match only. See JSON paths.
Reply
- A JSONPath: an array with the new length of each matched array,
nilwhere a match is not an array orindexis out of bounds for it; an empty array when nothing matches. - A legacy path: the new length, as an integer;
nilwhen the path matches nothing. nilif the key does not exist.
Errors
ERR index out of boundsandERR path is not an array, for a legacy path.ERR index must be an integer.ERR invalid JSON value: a value is not JSON text.
Examples
> JSON.SET d $ '{"tags":["math","code"]}'
OK
> JSON.ARRINSERT d $.tags 0 '"first"'
1) (integer) 3
> JSON.ARRINSERT d $.tags -1 '"beforelast"'
1) (integer) 4
> JSON.ARRINSERT d $.tags 4 '"end"'
1) (integer) 5
> JSON.GET d .tags
"[\"first\",\"math\",\"beforelast\",\"code\",\"end\"]"
> JSON.ARRINSERT d $.tags 6 '"x"'
1) (nil)
> JSON.ARRINSERT d .tags 6 '"x"'
(error) ERR index out of bounds
> JSON.ARRINSERT d .tags 1 '"a"' '"b"'
(integer) 7