JsonArray Lua Reference Documentation
chilkat.JsonArray
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.JsonArray
Work with JSON arrays as ordered lists containing strings, numbers,
booleans, nulls, objects, arrays, and mixed value types.
Retrieve, insert, replace, delete, or swap values by array index when
processing JSON list data.
Access child
Search arrays of strings or arrays of objects to locate matching values
without manually scanning every element.
Parse date/time values and preserve exact numeric strings when JSON data
contains values that must not be rounded or reformatted.
Produce compact or pretty JSON output after array data has been loaded,
edited, searched, or built programmatically.
For an extended overview, see
JsonArray Class Overview.
Read, modify, search, and emit ordered JSON arrays.
Chilkat.JsonArray is the JSON array companion to
Chilkat.JsonObject. It represents an ordered list of JSON values
and provides index-based methods for reading, inserting, replacing, deleting,
swapping, searching, and emitting array data. It supports scalar values,
nested objects, nested arrays, date/time parsing, exact numeric string
handling, compact or pretty output, and search helpers for arrays of strings
or arrays of objects.
Ordered JSON values
Index-based access
Nested objects and arrays
JsonObject and JsonArray values to
navigate and modify deeper JSON structures.
Search helpers
Date and numeric handling
Emit JSON output
JsonObject to navigate named members and JSON paths, then
use JsonArray to iterate or modify ordered lists found within
the object tree. Retrieve nested objects or arrays as needed, update values
by index, and emit the final JSON in compact or pretty form.
Object Creation
local chilkat = require("chilkat")
local jarr = chilkat.JsonArray.new()Creates the object (chilkat.JsonArray() does the same). Lua's garbage collector frees it when it is no longer referenced; jarr:dispose() frees it at once, and in Lua 5.4 and later local jarr <close> = chilkat.JsonArray.new() frees it when the block ends. Calling a method of a disposed object raises an error.
Errors
A method that fails returns nil followed by the error text, as io.open does: local body, err = jarr:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.
Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.
Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (jarr.verbose_logging = true) and methods use the colon (jarr:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.
Properties
DebugLogFilePath
local strVal = jarr.debug_log_file_path
jarr.debug_log_file_path = strVal
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
EmitCompact
local boolVal = jarr.emit_compact
jarr.emit_compact = boolVal
Controls whether emitted JSON is compact or pretty-formatted. If true, output contains no unnecessary whitespace and is emitted on a single line. If false, indentation and line breaks are added for readability. The default is true.
EmitCrlf
local boolVal = jarr.emit_crlf
jarr.emit_crlf = boolVal
Controls line endings when pretty-formatted JSON is emitted. If true, line breaks use CRLF (
); if false, they use LF (
). The default is true.
EmitCompact is true, the JSON is emitted on one line and this setting has no effect.FindStartIndex
local intVal = jarr.find_start_index
jarr.find_start_index = intVal
Specifies the zero-based array index at which FindString and FindObject begin searching. The default value is 0.
n, set this property to n + 1 before the next search to continue with later elements.LastErrorHtml
local strVal = jarr.last_error_html
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
local strVal = jarr.last_error_text
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
local strVal = jarr.last_error_xml
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
local boolVal = jarr.last_method_success
jarr.last_method_success = boolVal
Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
Size
local intVal = jarr.size
Returns the number of values currently contained in the JSON array. Array indexes therefore range from 0 through Size - 1.
VerboseLogging
local boolVal = jarr.verbose_logging
jarr.verbose_logging = boolVal
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
local strVal = jarr.version
Methods
AddArrayAt
-- ok is true, or nil and err on failure
local ok, err = jarr:add_array_at(index)
Inserts a new, empty JSON array at zero-based position index. Existing elements at that position and after it are shifted one position toward the end of the array.
0 | Insert as the first array element. |
-1 | Append as the last array element. |
n | Insert at zero-based position n. |
Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddArrayAt2
-- jarr is a chilkat.JsonArray
-- ok is true, or nil and err on failure
local ok, err = jarr:add_array_at2(index, jarr)
Inserts a new, empty JSON array at zero-based position index and updates jarr to reference the newly inserted array.
0 | Insert as the first array element. |
-1 | Append as the last array element. |
n | Insert at zero-based position n. |
jarr references the inserted array, it can be populated directly without a separate ArrayAt lookup.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddBoolAt
-- value is a boolean
-- ok is true, or nil and err on failure
local ok, err = jarr:add_bool_at(index, value)
Inserts a new JSON boolean value at zero-based position index. Existing elements at that position and after it are shifted one position toward the end of the array.
0 | Insert as the first array element. |
-1 | Append as the last array element. |
n | Insert at zero-based position n. |
Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddIntAt
-- value is an integer
-- ok is true, or nil and err on failure
local ok, err = jarr:add_int_at(index, value)
Inserts a new JSON integer value at zero-based position index. Existing elements at that position and after it are shifted one position toward the end of the array.
0 | Insert as the first array element. |
-1 | Append as the last array element. |
n | Insert at zero-based position n. |
Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddNullAt
-- ok is true, or nil and err on failure
local ok, err = jarr:add_null_at(index)
Inserts a JSON null value at zero-based position index. Existing elements at that position and after it are shifted one position toward the end of the array.
0 | Insert as the first array element. |
-1 | Append as the last array element. |
n | Insert at zero-based position n. |
Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddNumberAt
-- numeric_str is a string
-- ok is true, or nil and err on failure
local ok, err = jarr:add_number_at(index, numeric_str)
Inserts a JSON number at zero-based position index. The numericStr argument supplies the exact JSON numeric text to store. Existing elements at that position and after it are shifted one position toward the end of the array.
0 | Insert as the first array element. |
-1 | Append as the last array element. |
n | Insert at zero-based position n. |
12345678901234567890, 1.2500, or scientific notation. The supplied text must be valid JSON number syntax; JSON does not permit NaN or infinity.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddObjectAt
-- ok is true, or nil and err on failure
local ok, err = jarr:add_object_at(index)
Inserts a new, empty JSON object at zero-based position index. Existing elements at that position and after it are shifted one position toward the end of the array.
0 | Insert as the first array element. |
-1 | Append as the last array element. |
n | Insert at zero-based position n. |
Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddObjectAt2
-- json is a chilkat.JsonObject
-- ok is true, or nil and err on failure
local ok, err = jarr:add_object_at2(index, json)
Inserts a new, empty JSON object at zero-based position index and updates json to reference the newly inserted object.
0 | Insert as the first array element. |
-1 | Append as the last array element. |
n | Insert at zero-based position n. |
json references the inserted object, it can be populated directly without a separate ObjectAt lookup.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddObjectCopyAt
-- json_obj is a chilkat.JsonObject
-- ok is true, or nil and err on failure
local ok, err = jarr:add_object_copy_at(index, json_obj)
Inserts an independent copy of jsonObj at zero-based position index. Existing elements at that position and after it are shifted one position toward the end of the array.
0 | Insert as the first array element. |
-1 | Append as the last array element. |
n | Insert at zero-based position n. |
jsonObj do not serve as edits to the inserted array value.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddStringAt
-- value is a string
-- ok is true, or nil and err on failure
local ok, err = jarr:add_string_at(index, value)
Inserts a new JSON string value at zero-based position index. Existing elements at that position and after it are shifted one position toward the end of the array.
0 | Insert as the first array element. |
-1 | Append as the last array element. |
n | Insert at zero-based position n. |
Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddUIntAt
-- value is an integer
-- ok is true, or nil and err on failure
local ok, err = jarr:add_uint_at(index, value)
Inserts a new JSON unsigned integer value at zero-based position index. Existing elements at that position and after it are shifted one position toward the end of the array.
0 | Insert as the first array element. |
-1 | Append as the last array element. |
n | Insert at zero-based position n. |
Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AppendArrayItems
-- ok is true, or nil and err on failure
local ok, err = jarr:append_array_items(jarr)
Appends copies of all values from jarr to the end of this array, preserving their order.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
ArrayAt
-- jarr2 is a chilkat.JsonArray, or nil and err on failure
local jarr2, err = jarr:array_at(index)
Returns the nested JSON array stored at zero-based position index.
JsonArray refers to the existing nested array in the same JSON document. Changes made through the returned object modify that nested array.index must be an array. Use TypeAt when the value type is not already known.Returns a new object, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead. The returned object is independent of this one.
ArrayAt2
-- jarr is a chilkat.JsonArray
-- ok is true, or nil and err on failure
local ok, err = jarr:array_at2(index, jarr)
Updates jarr so that it references the nested JSON array stored at zero-based position index.
jarr modify the existing nested array in the same JSON document.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
BoolAt
-- flag is true or false
local flag = jarr:bool_at(index)
Returns the boolean value stored at zero-based position index.
TypeAt when the JSON type at index is not already known.Returns true or false: an answer, not a success flag.
Clear
Removes all values from the array, leaving an empty JSON array ([]).
DateAt
-- dt is a chilkat.DateTime
-- ok is true, or nil and err on failure
local ok, err = jarr:date_at(index, dt)
Parses the date/time value at zero-based position index and loads the result into dt. Recognized forms include ISO 8601 timestamps (for example, 2009-11-04T19:55:41Z), RFC 822-style date/time strings, and Unix timestamp integers.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
DeleteAt
-- ok is true, or nil and err on failure
local ok, err = jarr:delete_at(index)
Deletes the value at zero-based position index. Elements following the deleted value shift down by one position.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
DtAt
-- b_local is a boolean
-- dt is a chilkat.DtObj
-- ok is true, or nil and err on failure
local ok, err = jarr:dt_at(index, b_local, dt)
Parses the date/time value at zero-based position index and loads the result into dt. If bLocal is true, the populated fields represent local time; if false, they represent UTC/GMT. Recognized forms include ISO 8601 timestamps, RFC 822-style date/time strings, and Unix timestamp integers.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Emit
local str, err = jarr:emit()
Serializes this JSON array and returns the resulting JSON text.
EmitCompact to choose compact versus pretty output. When pretty output is enabled, EmitCrlf selects CRLF or LF line endings.Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
EmitSb
-- ok is true, or nil and err on failure
local ok, err = jarr:emit_sb(sb)
Serializes this JSON array into the supplied StringBuilder.
EmitCompact to choose compact versus pretty output. When pretty output is enabled, EmitCrlf selects CRLF or LF line endings.StringBuilder can avoid returning a very large JSON document as a language-native string before passing it to another Chilkat method or writing it to a file.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
FindObject
-- value is a string
-- case_sensitive is a boolean
-- n is an integer
local n = jarr:find_object(name, value, case_sensitive)
Searches array elements that are JSON objects and returns the zero-based index of the first object whose direct member name has a value matching value. Returns -1 if no match is found. The value pattern may contain *, which matches zero or more characters. Set caseSensitive to control whether letter case must match.
name identifies a direct member of each object being tested; it is not a JSON path.FindStartIndex, whose default is 0. To continue searching after a match, set FindStartIndex to the previous returned index plus 1.FindString
-- case_sensitive is a boolean
-- n is an integer
local n = jarr:find_string(value, case_sensitive)
Searches for the first string array element matching value and returns its zero-based index, or -1 if no match is found. The pattern may contain *, which matches zero or more characters. Set caseSensitive to control whether letter case must match.
FindStartIndex, whose default is 0. To continue searching after a match, set FindStartIndex to the previous returned index plus 1.IntAt
-- n is an integer
local n = jarr:int_at(index)
Returns the integer value stored at zero-based position index.
TypeAt when the JSON type at index is not already known.IsNullAt
-- flag is true or false
local flag = jarr:is_null_at(index)
Returns true if the value at zero-based position index is the JSON value null; otherwise returns false.
null is a distinct JSON value. It is not the same as an empty string, zero, false, an empty object, or an empty array.Returns true or false: an answer, not a success flag.
Load
-- ok is true, or nil and err on failure
local ok, err = jarr:load(json_array)
Parses the JSON array text in jsonArray and loads it into this object. The input must represent a JSON array, beginning with [ and ending with ].
JsonArray its own independent JSON document. If the object previously referenced an array nested inside another JSON document, that relationship is discarded. For this reason, call Load/LoadSb on a new JsonArray instance.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
LoadSb
-- ok is true, or nil and err on failure
local ok, err = jarr:load_sb(sb)
Parses the JSON array contained in sb and loads it into this object. The input must represent a JSON array, beginning with [ and ending with ].
JsonArray its own independent JSON document. If the object previously referenced an array nested inside another JSON document, that relationship is discarded. For this reason, call Load/LoadSb on a new JsonArray instance.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
ObjectAt
-- json is a chilkat.JsonObject, or nil and err on failure
local json, err = jarr:object_at(index)
Returns the JSON object stored at zero-based position index.
JsonObject refers to the existing object in the same JSON document. Changes made through the returned object modify that nested object.index must be an object. Use TypeAt when the value type is not already known.Returns a new object, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead. The returned object is independent of this one.
ObjectAt2
-- json_obj is a chilkat.JsonObject
-- ok is true, or nil and err on failure
local ok, err = jarr:object_at2(index, json_obj)
Updates jsonObj so that it references the JSON object stored at zero-based position index.
jsonObj modify the existing object in the same JSON document.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SetBoolAt
-- value is a boolean
-- ok is true, or nil and err on failure
local ok, err = jarr:set_bool_at(index, value)
Replaces the value at zero-based position index with the specified JSON boolean value. The array length and the positions of other elements are unchanged.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SetIntAt
-- value is an integer
-- ok is true, or nil and err on failure
local ok, err = jarr:set_int_at(index, value)
Replaces the value at zero-based position index with the specified JSON integer value. The array length and the positions of other elements are unchanged.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SetNullAt
-- ok is true, or nil and err on failure
local ok, err = jarr:set_null_at(index)
Replaces the value at zero-based position index with the JSON value null. The array length and the positions of other elements are unchanged.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SetNumberAt
-- value is a string
-- ok is true, or nil and err on failure
local ok, err = jarr:set_number_at(index, value)
Replaces the value at zero-based position index with a JSON number. The value argument supplies the exact JSON numeric text to store.
NaN or infinity.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SetStringAt
-- value is a string
-- ok is true, or nil and err on failure
local ok, err = jarr:set_string_at(index, value)
Replaces the value at zero-based position index with the specified JSON string value. The array length and the positions of other elements are unchanged.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SetUIntAt
-- value is an integer
-- ok is true, or nil and err on failure
local ok, err = jarr:set_uint_at(index, value)
Replaces the value at zero-based position index with the specified JSON unsigned integer value. The array length and the positions of other elements are unchanged.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
StringAt
-- str is a string, or nil and err on failure
local str, err = jarr:string_at(index)
Returns the string value stored at zero-based position index.
TypeAt when the JSON type at index is not already known.Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Swap
-- index2 is an integer
-- ok is true, or nil and err on failure
local ok, err = jarr:swap(index1, index2)
Exchanges the values at zero-based positions index1 and index2. The array length does not change.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
TypeAt
-- n is an integer
local n = jarr:type_at(index)
Returns an integer identifying the JSON type of the value at zero-based position index. Returns -1 if no value exists at that index.
1 | string |
2 | number |
3 | object |
4 | array |
5 | boolean |
6 | null |
ObjectAt, ArrayAt, or IntAt for the wrong kind of value.UIntAt
-- n is an integer
local n = jarr:uint_at(index)
Returns the unsigned integer value stored at zero-based position index.
TypeAt when the JSON type at index is not already known.