JsonArray Zig Reference Documentation

JsonArray

Current Version: 11.6.1

Chilkat.JsonArray

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

Work with JSON arrays as ordered lists containing strings, numbers, booleans, nulls, objects, arrays, and mixed value types.

Index-based access

Retrieve, insert, replace, delete, or swap values by array index when processing JSON list data.

Nested objects and arrays

Access child JsonObject and JsonArray values to navigate and modify deeper JSON structures.

Search helpers

Search arrays of strings or arrays of objects to locate matching values without manually scanning every element.

Date and numeric handling

Parse date/time values and preserve exact numeric strings when JSON data contains values that must not be rounded or reformatted.

Emit JSON output

Produce compact or pretty JSON output after array data has been loaded, edited, searched, or built programmatically.

Common pattern: Use 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

// Add the package once:
//     zig fetch --save https://chilkatdownload.com/11.6.1/chilkat-zig-11.6.1.tar.gz
// and in build.zig:
//     const chilkat = b.dependency("chilkat", .{ .target = target, .optimize = optimize });
//     exe.root_module.addImport("chilkat", chilkat.module("chilkat"));
const chilkat = @import("chilkat");

// Once per process, before any other Chilkat call:
try chilkat.unlockBundle("Anything for 30-day trial");

const json_array = try chilkat.JsonArray.init();
defer json_array.deinit();
pub fn init() Allocator.Error!JsonArray

Creates the underlying native Chilkat object. JsonArray is a one-pointer struct passed by value; copying it copies the handle (two names for one object). Returns error.OutOfMemory if the library could not allocate the object. Use an object from one thread at a time; it may be handed from one thread to another.

pub fn deinit(self: JsonArray) void

Releases the native object. Call it exactly once per object (usually with defer); the handle is invalid afterwards. Objects returned by methods are owned by the caller too and are released the same way.

pub fn fromHandle(h: *chilkat.c.CkJsonArray.HCkJsonArray) JsonArray

Wraps a handle obtained from the C API (chilkat.c.CkJsonArray), taking ownership of it. The struct's handle field goes the other way, for anything the Zig API does not cover.

Errors and memory

Methods that can fail return an error union: a method whose only outcome is success or failure returns Error!void; a method producing a string returns (Error || Allocator.Error)![:0]u8; a method producing an object returns Error!T. chilkat.Error is error{ChilkatFailed}; the reason for a failure is in getLastErrorText, and the object remains usable. Properties never fail, and methods that answer a question (hasMember, isUnlocked, ...) return a plain bool.

String arguments are [:0]const u8 (UTF-8; string literals can be passed as is). String results are copies allocated with the allocator argument and owned by the caller, so they stay valid across later calls on the same object.

const text = json_array.someMethod(allocator, ...) catch |err| {
    const why = try json_array.getLastErrorText(allocator);
    defer allocator.free(why);
    std.debug.print("{s}\n", .{why});
    return err;
};
defer allocator.free(text);

Properties

DebugLogFilePath
// read/write
pub fn getDebugLogFilePath(self: JsonArray, allocator: Allocator) Allocator.Error![:0]u8
pub fn setDebugLogFilePath(self: JsonArray, value: [:0]const u8) void

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.

More Information and Examples
top
EmitCompact
// read/write
pub fn getEmitCompact(self: JsonArray) bool
pub fn setEmitCompact(self: JsonArray, value: bool) void
Introduced in version 9.5.0.64

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.

top
EmitCrlf
// read/write
pub fn getEmitCrlf(self: JsonArray) bool
pub fn setEmitCrlf(self: JsonArray, value: bool) void
Introduced in version 9.5.0.64

Controls line endings when pretty-formatted JSON is emitted. If true, line breaks use CRLF ( ); if false, they use LF ( ). The default is true.

Applies only to pretty output: When EmitCompact is true, the JSON is emitted on one line and this setting has no effect.

top
FindStartIndex
// read/write
pub fn getFindStartIndex(self: JsonArray) i32
pub fn setFindStartIndex(self: JsonArray, value: i32) void
Introduced in version 11.6.0

Specifies the zero-based array index at which FindString and FindObject begin searching. The default value is 0.

Iterating matches: After finding a match at index n, set this property to n + 1 before the next search to continue with later elements.

top
LastErrorHtml
// read-only
pub fn getLastErrorHtml(self: JsonArray, allocator: Allocator) Allocator.Error![:0]u8

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.

top
LastErrorText
// read-only
pub fn getLastErrorText(self: JsonArray, allocator: Allocator) Allocator.Error![:0]u8

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.

top
LastErrorXml
// read-only
pub fn getLastErrorXml(self: JsonArray, allocator: Allocator) Allocator.Error![:0]u8

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.

top
LastMethodSuccess
// read/write
pub fn getLastMethodSuccess(self: JsonArray) bool
pub fn setLastMethodSuccess(self: JsonArray, value: bool) void

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.

top
Size
// read-only
pub fn getSize(self: JsonArray) i32
Introduced in version 9.5.0.56

Returns the number of values currently contained in the JSON array. Array indexes therefore range from 0 through Size - 1.

top
VerboseLogging
// read/write
pub fn getVerboseLogging(self: JsonArray) bool
pub fn setVerboseLogging(self: JsonArray, value: bool) void

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.

top
Version
// read-only
pub fn getVersion(self: JsonArray, allocator: Allocator) Allocator.Error![:0]u8

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

AddArrayAt
pub fn addArrayAt(self: JsonArray, index: i32) Error!void
Introduced in version 9.5.0.56

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.

0Insert as the first array element.
-1Append as the last array element.
nInsert at zero-based position n.
Insert vs. replace: The Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
AddArrayAt2
pub fn addArrayAt2(self: JsonArray, index: i32, jarr: JsonArray) Error!void
Introduced in version 11.1.0

Inserts a new, empty JSON array at zero-based position index and updates jarr to reference the newly inserted array.

0Insert as the first array element.
-1Append as the last array element.
nInsert at zero-based position n.
Immediate editing: Because jarr references the inserted array, it can be populated directly without a separate ArrayAt lookup.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
AddBoolAt
pub fn addBoolAt(self: JsonArray, index: i32, value: bool) Error!void
Introduced in version 9.5.0.56

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.

0Insert as the first array element.
-1Append as the last array element.
nInsert at zero-based position n.
Insert vs. replace: The Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
AddIntAt
pub fn addIntAt(self: JsonArray, index: i32, value: i32) Error!void
Introduced in version 9.5.0.56

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.

0Insert as the first array element.
-1Append as the last array element.
nInsert at zero-based position n.
Insert vs. replace: The Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
AddNullAt
pub fn addNullAt(self: JsonArray, index: i32) Error!void
Introduced in version 9.5.0.56

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.

0Insert as the first array element.
-1Append as the last array element.
nInsert at zero-based position n.
Insert vs. replace: The Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
AddNumberAt
pub fn addNumberAt(self: JsonArray, index: i32, numeric_str: [:0]const u8) Error!void
Introduced in version 9.5.0.56

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.

0Insert as the first array element.
-1Append as the last array element.
nInsert at zero-based position n.
Exact numeric text: Use this method when lexical form or precision matters, for example 12345678901234567890, 1.2500, or scientific notation. The supplied text must be valid JSON number syntax; JSON does not permit NaN or infinity.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
AddObjectAt
pub fn addObjectAt(self: JsonArray, index: i32) Error!void
Introduced in version 9.5.0.56

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.

0Insert as the first array element.
-1Append as the last array element.
nInsert at zero-based position n.
Insert vs. replace: The Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
AddObjectAt2
pub fn addObjectAt2(self: JsonArray, index: i32, json: chilkat.JsonObject) Error!void
Introduced in version 11.1.0

Inserts a new, empty JSON object at zero-based position index and updates json to reference the newly inserted object.

0Insert as the first array element.
-1Append as the last array element.
nInsert at zero-based position n.
Immediate editing: Because json references the inserted object, it can be populated directly without a separate ObjectAt lookup.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
AddObjectCopyAt
pub fn addObjectCopyAt(self: JsonArray, index: i32, json_obj: chilkat.JsonObject) Error!void
Introduced in version 9.5.0.82

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.

0Insert as the first array element.
-1Append as the last array element.
nInsert at zero-based position n.
Copy semantics: The inserted object is copied rather than referenced. Later changes to the source jsonObj do not serve as edits to the inserted array value.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
AddStringAt
pub fn addStringAt(self: JsonArray, index: i32, value: [:0]const u8) Error!void
Introduced in version 9.5.0.56

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.

0Insert as the first array element.
-1Append as the last array element.
nInsert at zero-based position n.
Insert vs. replace: The Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
AddUIntAt
pub fn addUIntAt(self: JsonArray, index: i32, value: u32) Error!void
Introduced in version 9.5.0.96

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.

0Insert as the first array element.
-1Append as the last array element.
nInsert at zero-based position n.
Insert vs. replace: The Add*At methods insert a new array element. Use the corresponding Set*At method when replacing an existing element.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
AppendArrayItems
pub fn appendArrayItems(self: JsonArray, jarr: JsonArray) Error!void
Introduced in version 9.5.0.82

Appends copies of all values from jarr to the end of this array, preserving their order.

Source is unchanged: The source array is not emptied or moved. This method is useful for concatenating JSON arrays, including arrays belonging to different JSON documents.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
ArrayAt
pub fn arrayAt(self: JsonArray, index: i32) Error!JsonArray
Introduced in version 9.5.0.56

Returns the nested JSON array stored at zero-based position index.

Reference, not copy: The returned JsonArray refers to the existing nested array in the same JSON document. Changes made through the returned object modify that nested array.
Type requirement: The value at index must be an array. Use TypeAt when the value type is not already known.

Returns error.ChilkatFailed on failure; getLastErrorText explains why. The returned object is owned by the caller: release it with deinit.

More Information and Examples
top
ArrayAt2
pub fn arrayAt2(self: JsonArray, index: i32, jarr: JsonArray) Error!void
Introduced in version 11.0.0

Updates jarr so that it references the nested JSON array stored at zero-based position index.

Reference, not copy: Changes made through jarr modify the existing nested array in the same JSON document.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
BoolAt
pub fn boolAt(self: JsonArray, index: i32) bool
Introduced in version 9.5.0.56

Returns the boolean value stored at zero-based position index.

Type-aware access: Use TypeAt when the JSON type at index is not already known.
top
Clear
pub fn clear(self: JsonArray) void
Introduced in version 9.5.0.76

Removes all values from the array, leaving an empty JSON array ([]).

top
DateAt
pub fn dateAt(self: JsonArray, index: i32, dt: chilkat.DateTime) Error!void
Introduced in version 9.5.0.73

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.

JSON has no native date type: Dates are application conventions represented as strings or numbers. These methods recognize common representations and convert them to Chilkat date/time objects.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
DeleteAt
pub fn deleteAt(self: JsonArray, index: i32) Error!void
Introduced in version 9.5.0.56

Deletes the value at zero-based position index. Elements following the deleted value shift down by one position.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
DtAt
pub fn dtAt(self: JsonArray, index: i32, b_local: bool, dt: chilkat.DtObj) Error!void
Introduced in version 9.5.0.73

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.

JSON has no native date type: Dates are application conventions represented as strings or numbers. These methods recognize common representations and convert them to Chilkat date/time objects.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
Emit
pub fn emit(self: JsonArray, allocator: Allocator) (Error || Allocator.Error)![:0]u8
Introduced in version 9.5.0.58

Serializes this JSON array and returns the resulting JSON text.

Formatting: Set EmitCompact to choose compact versus pretty output. When pretty output is enabled, EmitCrlf selects CRLF or LF line endings.

Returns error.ChilkatFailed on failure; getLastErrorText explains why. The string is allocated with allocator and owned by the caller (defer allocator.free(s)).

top
EmitSb
pub fn emitSb(self: JsonArray, sb: chilkat.StringBuilder) Error!void
Introduced in version 9.5.0.65

Serializes this JSON array into the supplied StringBuilder.

Formatting: Set EmitCompact to choose compact versus pretty output. When pretty output is enabled, EmitCrlf selects CRLF or LF line endings.
Large JSON: Using a 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 error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
FindObject
pub fn findObject(self: JsonArray, name: [:0]const u8, value: [:0]const u8, case_sensitive: bool) i32
Introduced in version 9.5.0.73

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.

Member name, not JSON path: name identifies a direct member of each object being tested; it is not a JSON path.
Search start: Searching begins at FindStartIndex, whose default is 0. To continue searching after a match, set FindStartIndex to the previous returned index plus 1.
More Information and Examples
top
FindString
pub fn findString(self: JsonArray, value: [:0]const u8, case_sensitive: bool) i32
Introduced in version 9.5.0.73

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.

Search start: Searching begins at FindStartIndex, whose default is 0. To continue searching after a match, set FindStartIndex to the previous returned index plus 1.
top
IntAt
pub fn intAt(self: JsonArray, index: i32) i32
Introduced in version 9.5.0.56

Returns the integer value stored at zero-based position index.

Type-aware access: Use TypeAt when the JSON type at index is not already known.
More Information and Examples
top
IsNullAt
pub fn isNullAt(self: JsonArray, index: i32) bool
Introduced in version 9.5.0.56

Returns true if the value at zero-based position index is the JSON value null; otherwise returns false.

JSON null: null is a distinct JSON value. It is not the same as an empty string, zero, false, an empty object, or an empty array.
More Information and Examples
top
Load
pub fn load(self: JsonArray, json_array: [:0]const u8) Error!void
Introduced in version 9.5.0.64

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 ].

Important — loading detaches the array: Calling this method makes the 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 error.ChilkatFailed on failure; getLastErrorText explains why.

top
LoadSb
pub fn loadSb(self: JsonArray, sb: chilkat.StringBuilder) Error!void
Introduced in version 9.5.0.64

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 ].

Important — loading detaches the array: Calling this method makes the 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 error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
ObjectAt
pub fn objectAt(self: JsonArray, index: i32) Error!chilkat.JsonObject
Introduced in version 9.5.0.56

Returns the JSON object stored at zero-based position index.

Reference, not copy: The returned JsonObject refers to the existing object in the same JSON document. Changes made through the returned object modify that nested object.
Type requirement: The value at index must be an object. Use TypeAt when the value type is not already known.

Returns error.ChilkatFailed on failure; getLastErrorText explains why. The returned object is owned by the caller: release it with deinit.

top
ObjectAt2
pub fn objectAt2(self: JsonArray, index: i32, json_obj: chilkat.JsonObject) Error!void
Introduced in version 11.0.0

Updates jsonObj so that it references the JSON object stored at zero-based position index.

Reference, not copy: Changes made through jsonObj modify the existing object in the same JSON document.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
SetBoolAt
pub fn setBoolAt(self: JsonArray, index: i32, value: bool) Error!void
Introduced in version 9.5.0.56

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 error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
SetIntAt
pub fn setIntAt(self: JsonArray, index: i32, value: i32) Error!void
Introduced in version 9.5.0.56

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 error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
SetNullAt
pub fn setNullAt(self: JsonArray, index: i32) Error!void
Introduced in version 9.5.0.56

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 error.ChilkatFailed on failure; getLastErrorText explains why.

top
SetNumberAt
pub fn setNumberAt(self: JsonArray, index: i32, value: [:0]const u8) Error!void
Introduced in version 9.5.0.56

Replaces the value at zero-based position index with a JSON number. The value argument supplies the exact JSON numeric text to store.

Exact numeric text: Use this method when lexical form or precision matters. The supplied text must be valid JSON number syntax; JSON does not permit NaN or infinity.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
SetStringAt
pub fn setStringAt(self: JsonArray, index: i32, value: [:0]const u8) Error!void
Introduced in version 9.5.0.56

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 error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
SetUIntAt
pub fn setUIntAt(self: JsonArray, index: i32, value: u32) Error!void
Introduced in version 9.5.0.96

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 error.ChilkatFailed on failure; getLastErrorText explains why.

top
StringAt
pub fn stringAt(self: JsonArray, allocator: Allocator, index: i32) (Error || Allocator.Error)![:0]u8
Introduced in version 9.5.0.56

Returns the string value stored at zero-based position index.

Type-aware access: Use TypeAt when the JSON type at index is not already known.

Returns error.ChilkatFailed on failure; getLastErrorText explains why. The string is allocated with allocator and owned by the caller (defer allocator.free(s)).

More Information and Examples
top
Swap
pub fn swap(self: JsonArray, index1: i32, index2: i32) Error!void
Introduced in version 9.5.0.76

Exchanges the values at zero-based positions index1 and index2. The array length does not change.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
TypeAt
pub fn typeAt(self: JsonArray, index: i32) i32
Introduced in version 9.5.0.58

Returns an integer identifying the JSON type of the value at zero-based position index. Returns -1 if no value exists at that index.

1string
2number
3object
4array
5boolean
6null
Why use TypeAt: JSON arrays may contain mixed types. Checking the type first avoids choosing a type-specific accessor such as ObjectAt, ArrayAt, or IntAt for the wrong kind of value.
top
UIntAt
pub fn uIntAt(self: JsonArray, index: i32) u32
Introduced in version 9.5.0.96

Returns the unsigned integer value stored at zero-based position index.

Type-aware access: Use TypeAt when the JSON type at index is not already known.
top