Class JsonArray
- All Implemented Interfaces:
AutoCloseable
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.
For an extended overview, see JsonArray Class Overview.
</section>
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddArrayAt(int index)Inserts a new, empty JSON array at zero-based positionindex.voidaddArrayAt2(int index, JsonArray jarr)Inserts a new, empty JSON array at zero-based positionindexand updatesjarrto reference the newly inserted array.voidaddBoolAt(int index, boolean value)Inserts a new JSON boolean value at zero-based positionindex.voidaddIntAt(int index, int value)Inserts a new JSON integer value at zero-based positionindex.voidaddNullAt(int index)Inserts a JSONnullvalue at zero-based positionindex.voidaddNumberAt(int index, String numericStr)Inserts a JSON number at zero-based positionindex.voidaddObjectAt(int index)Inserts a new, empty JSON object at zero-based positionindex.voidaddObjectAt2(int index, JsonObject json)Inserts a new, empty JSON object at zero-based positionindexand updatesjsonto reference the newly inserted object.voidaddObjectCopyAt(int index, JsonObject jsonObj)Inserts an independent copy ofjsonObjat zero-based positionindex.voidaddStringAt(int index, String value)Inserts a new JSON string value at zero-based positionindex.voidaddUIntAt(int index, long value)Inserts a new JSON unsigned integer value at zero-based positionindex.voidappendArrayItems(JsonArray jarr)Appends copies of all values fromjarrto the end of this array, preserving their order.arrayAt(int index)Returns the nested JSON array stored at zero-based positionindex.voidUpdatesjarrso that it references the nested JSON array stored at zero-based positionindex.booleanboolAt(int index)Returns the boolean value stored at zero-based positionindex.voidclear()Removes all values from the array, leaving an empty JSON array ([]).voidParses the date/time value at zero-based positionindexand loads the result intodt.voiddeleteAt(int index)Deletes the value at zero-based positionindex.voidParses the date/time value at zero-based positionindexand loads the result intodt.emit()Serializes this JSON array and returns the resulting JSON text.voidemitSb(StringBuilder sb)Serializes this JSON array into the suppliedStringBuilder.intfindObject(String name, String value, boolean caseSensitive)Searches array elements that are JSON objects and returns the zero-based index of the first object whose direct membernamehas a value matchingvalue.intfindString(String value, boolean caseSensitive)Searches for the first string array element matchingvalueand returns its zero-based index, or-1if no match is found.booleanControls whether emitted JSON is compact or pretty-formatted.booleanControls line endings when pretty-formatted JSON is emitted.intSpecifies the zero-based array index at whichFindStringandFindObjectbegin searching.intgetSize()Returns the number of values currently contained in the JSON array.intintAt(int index)Returns the integer value stored at zero-based positionindex.booleanisNullAt(int index)Returnstrueif the value at zero-based positionindexis the JSON valuenull; otherwise returnsfalse.voidParses the JSON array text injsonArrayand loads it into this object.voidloadSb(StringBuilder sb)Parses the JSON array contained insband loads it into this object.objectAt(int index)Returns the JSON object stored at zero-based positionindex.voidobjectAt2(int index, JsonObject jsonObj)UpdatesjsonObjso that it references the JSON object stored at zero-based positionindex.voidsetBoolAt(int index, boolean value)Replaces the value at zero-based positionindexwith the specified JSON boolean value.voidsetEmitCompact(boolean value)Sets theEmitCompactproperty; seegetEmitCompact().voidsetEmitCrlf(boolean value)Sets theEmitCrlfproperty; seegetEmitCrlf().voidsetFindStartIndex(int value)Sets theFindStartIndexproperty; seegetFindStartIndex().voidsetIntAt(int index, int value)Replaces the value at zero-based positionindexwith the specified JSON integer value.voidsetNullAt(int index)Replaces the value at zero-based positionindexwith the JSON valuenull.voidsetNumberAt(int index, String value)Replaces the value at zero-based positionindexwith a JSON number.voidsetStringAt(int index, String value)Replaces the value at zero-based positionindexwith the specified JSON string value.voidsetUIntAt(int index, long value)Replaces the value at zero-based positionindexwith the specified JSON unsigned integer value.stringAt(int index)Returns the string value stored at zero-based positionindex.voidswap(int index1, int index2)Exchanges the values at zero-based positionsindex1andindex2.inttypeAt(int index)Returns an integer identifying the JSON type of the value at zero-based positionindex.longuIntAt(int index)Returns the unsigned integer value stored at zero-based positionindex.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
JsonArray
public JsonArray()Creates a newJsonArrayobject.
-
-
Method Details
-
getEmitCompact
public boolean getEmitCompact()Controls whether emitted JSON is compact or pretty-formatted. Iftrue, output contains no unnecessary whitespace and is emitted on a single line. Iffalse, indentation and line breaks are added for readability. The default istrue.- Returns:
- the value of the
EmitCompactproperty
-
setEmitCompact
public void setEmitCompact(boolean value)Sets theEmitCompactproperty; seegetEmitCompact().- Parameters:
value- the new value
-
getEmitCrlf
public boolean getEmitCrlf()Controls line endings when pretty-formatted JSON is emitted. Iftrue, line breaks use CRLF (); iffalse, they use LF (). The default istrue.Applies only to pretty output: When
EmitCompactistrue, the JSON is emitted on one line and this setting has no effect.- Returns:
- the value of the
EmitCrlfproperty
-
setEmitCrlf
public void setEmitCrlf(boolean value)Sets theEmitCrlfproperty; seegetEmitCrlf().- Parameters:
value- the new value
-
getFindStartIndex
public int getFindStartIndex()Specifies the zero-based array index at whichFindStringandFindObjectbegin searching. The default value is0.Iterating matches: After finding a match at index
n, set this property ton + 1before the next search to continue with later elements.- Returns:
- the value of the
FindStartIndexproperty
-
setFindStartIndex
public void setFindStartIndex(int value)Sets theFindStartIndexproperty; seegetFindStartIndex().- Parameters:
value- the new value
-
getSize
public int getSize()Returns the number of values currently contained in the JSON array. Array indexes therefore range from0throughSize - 1.- Returns:
- the value of the
Sizeproperty (read-only)
-
addArrayAt
public void addArrayAt(int index)Inserts a new, empty JSON array at zero-based positionindex. 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*Atmethods insert a new array element. Use the correspondingSet*Atmethod when replacing an existing element.- Parameters:
index- theindexargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addArrayAt2
Inserts a new, empty JSON array at zero-based positionindexand updatesjarrto 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
jarrreferences the inserted array, it can be populated directly without a separateArrayAtlookup.- Parameters:
index- theindexargumentjarr- thejarrargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addBoolAt
public void addBoolAt(int index, boolean value)Inserts a new JSON boolean value at zero-based positionindex. 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*Atmethods insert a new array element. Use the correspondingSet*Atmethod when replacing an existing element.- Parameters:
index- theindexargumentvalue- thevalueargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addIntAt
public void addIntAt(int index, int value)Inserts a new JSON integer value at zero-based positionindex. 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*Atmethods insert a new array element. Use the correspondingSet*Atmethod when replacing an existing element.- Parameters:
index- theindexargumentvalue- thevalueargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addNullAt
public void addNullAt(int index)Inserts a JSONnullvalue at zero-based positionindex. 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*Atmethods insert a new array element. Use the correspondingSet*Atmethod when replacing an existing element.- Parameters:
index- theindexargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addNumberAt
Inserts a JSON number at zero-based positionindex. ThenumericStrargument 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 permitNaNor infinity.- Parameters:
index- theindexargumentnumericStr- thenumericStrargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addObjectAt
public void addObjectAt(int index)Inserts a new, empty JSON object at zero-based positionindex. 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*Atmethods insert a new array element. Use the correspondingSet*Atmethod when replacing an existing element.- Parameters:
index- theindexargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addObjectAt2
Inserts a new, empty JSON object at zero-based positionindexand updatesjsonto 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
jsonreferences the inserted object, it can be populated directly without a separateObjectAtlookup.- Parameters:
index- theindexargumentjson- thejsonargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addObjectCopyAt
Inserts an independent copy ofjsonObjat zero-based positionindex. 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
jsonObjdo not serve as edits to the inserted array value.- Parameters:
index- theindexargumentjsonObj- thejsonObjargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addStringAt
Inserts a new JSON string value at zero-based positionindex. 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*Atmethods insert a new array element. Use the correspondingSet*Atmethod when replacing an existing element.- Parameters:
index- theindexargumentvalue- thevalueargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addUIntAt
public void addUIntAt(int index, long value)Inserts a new JSON unsigned integer value at zero-based positionindex. 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*Atmethods insert a new array element. Use the correspondingSet*Atmethod when replacing an existing element.- Parameters:
index- theindexargumentvalue- thevalueargument (0 .. 4294967295)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendArrayItems
Appends copies of all values fromjarrto 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.
- Parameters:
jarr- thejarrargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
arrayAt
Returns the nested JSON array stored at zero-based positionindex.Reference, not copy: The returned
JsonArrayrefers to the existing nested array in the same JSON document. Changes made through the returned object modify that nested array.Type requirement: The value atindexmust be an array. UseTypeAtwhen the value type is not already known.- Parameters:
index- theindexargument- Returns:
- a new
JsonArrayowned by the caller (nevernull); close it when done - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
arrayAt2
Updatesjarrso that it references the nested JSON array stored at zero-based positionindex.Reference, not copy: Changes made through
jarrmodify the existing nested array in the same JSON document.- Parameters:
index- theindexargumentjarr- thejarrargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
boolAt
public boolean boolAt(int index)Returns the boolean value stored at zero-based positionindex.Type-aware access: Use
TypeAtwhen the JSON type atindexis not already known.- Parameters:
index- theindexargument- Returns:
- the result; this method never throws
ChilkatException
-
clear
public void clear()Removes all values from the array, leaving an empty JSON array ([]). -
dateAt
Parses the date/time value at zero-based positionindexand loads the result intodt. 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.
- Parameters:
index- theindexargumentdt- thedtargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
deleteAt
public void deleteAt(int index)Deletes the value at zero-based positionindex. Elements following the deleted value shift down by one position.- Parameters:
index- theindexargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
dtAt
Parses the date/time value at zero-based positionindexand loads the result intodt. IfbLocalistrue, the populated fields represent local time; iffalse, 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.
- Parameters:
index- theindexargumentbLocal- thebLocalargumentdt- thedtargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
emit
Serializes this JSON array and returns the resulting JSON text.Formatting: Set
EmitCompactto choose compact versus pretty output. When pretty output is enabled,EmitCrlfselects CRLF or LF line endings.- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
emitSb
Serializes this JSON array into the suppliedStringBuilder.Formatting: Set
EmitCompactto choose compact versus pretty output. When pretty output is enabled,EmitCrlfselects CRLF or LF line endings.Large JSON: Using aStringBuildercan 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.- Parameters:
sb- thesbargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
findObject
Searches array elements that are JSON objects and returns the zero-based index of the first object whose direct membernamehas a value matchingvalue. Returns-1if no match is found. The value pattern may contain*, which matches zero or more characters. SetcaseSensitiveto control whether letter case must match.Member name, not JSON path:
nameidentifies a direct member of each object being tested; it is not a JSON path.Search start: Searching begins atFindStartIndex, whose default is0. To continue searching after a match, setFindStartIndexto the previous returned index plus 1.- Parameters:
name- thenameargument (notnull)value- thevalueargument (notnull)caseSensitive- thecaseSensitiveargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
findString
Searches for the first string array element matchingvalueand returns its zero-based index, or-1if no match is found. The pattern may contain*, which matches zero or more characters. SetcaseSensitiveto control whether letter case must match.Search start: Searching begins at
FindStartIndex, whose default is0. To continue searching after a match, setFindStartIndexto the previous returned index plus 1.- Parameters:
value- thevalueargument (notnull)caseSensitive- thecaseSensitiveargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
intAt
public int intAt(int index)Returns the integer value stored at zero-based positionindex.Type-aware access: Use
TypeAtwhen the JSON type atindexis not already known.- Parameters:
index- theindexargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
isNullAt
public boolean isNullAt(int index)Returnstrueif the value at zero-based positionindexis the JSON valuenull; otherwise returnsfalse.JSON null:
nullis a distinct JSON value. It is not the same as an empty string, zero,false, an empty object, or an empty array.- Parameters:
index- theindexargument- Returns:
- the result; this method never throws
ChilkatException
-
load
Parses the JSON array text injsonArrayand 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
JsonArrayits own independent JSON document. If the object previously referenced an array nested inside another JSON document, that relationship is discarded. For this reason, callLoad/LoadSbon a newJsonArrayinstance.- Parameters:
jsonArray- thejsonArrayargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
loadSb
Parses the JSON array contained insband 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
JsonArrayits own independent JSON document. If the object previously referenced an array nested inside another JSON document, that relationship is discarded. For this reason, callLoad/LoadSbon a newJsonArrayinstance.- Parameters:
sb- thesbargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
objectAt
Returns the JSON object stored at zero-based positionindex.Reference, not copy: The returned
JsonObjectrefers to the existing object in the same JSON document. Changes made through the returned object modify that nested object.Type requirement: The value atindexmust be an object. UseTypeAtwhen the value type is not already known.- Parameters:
index- theindexargument- Returns:
- a new
JsonObjectowned by the caller (nevernull); close it when done - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
objectAt2
UpdatesjsonObjso that it references the JSON object stored at zero-based positionindex.Reference, not copy: Changes made through
jsonObjmodify the existing object in the same JSON document.- Parameters:
index- theindexargumentjsonObj- thejsonObjargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setBoolAt
public void setBoolAt(int index, boolean value)Replaces the value at zero-based positionindexwith the specified JSON boolean value. The array length and the positions of other elements are unchanged.- Parameters:
index- theindexargumentvalue- thevalueargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setIntAt
public void setIntAt(int index, int value)Replaces the value at zero-based positionindexwith the specified JSON integer value. The array length and the positions of other elements are unchanged.- Parameters:
index- theindexargumentvalue- thevalueargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setNullAt
public void setNullAt(int index)Replaces the value at zero-based positionindexwith the JSON valuenull. The array length and the positions of other elements are unchanged.- Parameters:
index- theindexargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setNumberAt
Replaces the value at zero-based positionindexwith a JSON number. Thevalueargument 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
NaNor infinity.- Parameters:
index- theindexargumentvalue- thevalueargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setStringAt
Replaces the value at zero-based positionindexwith the specified JSON string value. The array length and the positions of other elements are unchanged.- Parameters:
index- theindexargumentvalue- thevalueargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setUIntAt
public void setUIntAt(int index, long value)Replaces the value at zero-based positionindexwith the specified JSON unsigned integer value. The array length and the positions of other elements are unchanged.- Parameters:
index- theindexargumentvalue- thevalueargument (0 .. 4294967295)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
stringAt
Returns the string value stored at zero-based positionindex.Type-aware access: Use
TypeAtwhen the JSON type atindexis not already known.- Parameters:
index- theindexargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
swap
public void swap(int index1, int index2)Exchanges the values at zero-based positionsindex1andindex2. The array length does not change.- Parameters:
index1- theindex1argumentindex2- theindex2argument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
typeAt
public int typeAt(int index)Returns an integer identifying the JSON type of the value at zero-based positionindex. Returns-1if no value exists at that index.1string2number3object4array5boolean6nullWhy use TypeAt: JSON arrays may contain mixed types. Checking the type first avoids choosing a type-specific accessor such as
ObjectAt,ArrayAt, orIntAtfor the wrong kind of value.- Parameters:
index- theindexargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
uIntAt
public long uIntAt(int index)Returns the unsigned integer value stored at zero-based positionindex.Type-aware access: Use
TypeAtwhen the JSON type atindexis not already known.- Parameters:
index- theindexargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-