Insert (Json function)
Jump to navigation
Jump to search
Insert an item into a JSON array (Json class)
[Introduced in Model 204 7.6]
This function inserts an item into a Json array.
Syntax
[%number =] json:Insert( number, json) Throws InvalidJsonType
Syntax terms
%number | The number of items in the array after the Add. |
---|---|
Json | Json object, which must be an array or an InvalidJsonType exception is thrown. |
number | The number of the item before which the new item is to be inserted. If this is one more than the number of items in the array, the item is added to the end of the array. |
json | Json object that can be Null. |
Usage notes
- The first parameter can also be thought of as the intended item number for the item being added.
- The value returned by Insert is the same value that would be returned by an immediately following Count function.
- Unlike the Arraylist and Stringlist Insert methods, only one item can be added with the Json Insert function.
- Inserting an item into the middle of an array shifts subsequent items up. This means that subsequent items after an insert are accessed by a number one greater than before.
Examples
The following example populates an array with the first 10 prime numbers and then inserts the string "Start" at the beginning and "End" at the end:
b %i float %json is object json %prime is float %json = array for %i from 1 to 10 %prime = %prime:nextPrime %json:add(%prime) end for %json:insert(1, "Start") %json:insert(%json:count + 1, "End") printtext {~=%json} end
It prints:
%json=["Start",2,3,5,7,11,13,17,19,23,29,"End"]
Of course, the insert of "End" above would be done much more sensibly with the Add function.