Add( ) method (JsonArray)
- Last Updated: January 18, 2024
- 2 minute read
- OpenEdge
- Version 12.8
- Documentation
Creates one or more new elements and adds them at a specified index or to the end of the JsonArray. On successful completion, this method returns the index of the last newly added element.
Return type: INTEGER
Access: PUBLIC
Applies to: Progress.Json.ObjectModel.JsonArray class
Syntax
|
- value
- The value to which the new element is to be set. If value is
the Unknown value (
?), the element is set to the JSON null value.If value is a simple type, the JsonArray:Length property is incremented by one. If value is an ABL array, JsonArray:Length is incremented by the length of the ABL array.
- array-value
- ABL array containing values used to initialize a set of new
elements. Each element of the ABL array is used to initialize one
new element in the JsonArray. Any Unknown value (
?) within the ABL array will result in a JSON null value in the JsonArray. If array-value itself is the Unknown value (?) or indeterminate, a JsonError is raised. - index
- An INTEGER identifying the element after which the new element or
elements are added. Indexing into JsonArrays is 1-based. For example, myArray:Add(5,
"custnum") inserts a String value "custnum" as element 6 into the
JsonArray. All elements starting with the original sixth element are
shifted by one position.
If index is 0, the new element is inserted at the beginning of the array making that new element's index 1. If index is not provided, the default behavior is to add new elements to the end of the array. If index is the Unknown value (
?), less than 0, or is greater than the length of the array, a JsonError is raised.The data type of the added JSON value set by this method call depends upon the ABL data type of the value parameter.
Table 1. Value parameter A value parameter of data type Data type of JSON value CHARACTER, LONGCHAR
string INTEGER, INT64, DECIMAL number LOGICAL boolean MEMPTR, RAW, ROWID string with a value as if you had called BASE64-ENCODE( )on valueDATE, DATETIME, DATETIME-TZ string with a value as if you had called ISO-DATE( )on valueCOM-HANDLE, HANDLE, RECID number with a value as if you had called INTEGER( )on valueThe following example demonstrates how the
Add( )overloaded methods affect a JsonArray:DEFINE VARIABLE a AS INTEGER EXTENT 3 INITIAL [3, 2, 1]. DEFINE VARIABLE b AS CHARACTER EXTENT 2 INITIAL ["Hello", "world"]. DEFINE VARIABLE c AS DATE EXTENT 2 INITIAL ["9/20/10", "10/10/10"]. myArr = NEW JsonArray(a) /* myArr is now [3, 2, 1] */ myArr:Add(TRUE). /* myArr is now [3, 2, 1, true] */ myArr:Add(b). /* myArr is now [3, 2, 1, true, "Hello", "world"] */ myArr:Add(2, c). /* myArr is now [3, 2, "2010-09-20", "2010-10-10", 1, true, "Hello", "world"] */