JsonArraySetStringElement Function

Action

Replaces the value of a specified element from a JSON array. If the array element does not exist in the JSON array, a new element with the specified value will be appended to the array. If the array element does not contain a string value, nothing happens.

Include file

Json.bdh

Syntax

JsonArraySetStringElement( in handle : number,
                           in index  : number,
                           in value  : string ): boolean;
Parameter Description
handle Valid handle to a JSON array
index The index position in the JSON array. Lower bound is zero (0).
value The new value of the element

Return value

  • true if value successfully written to element
  • false otherwise

Example

transaction TMain
var
  jsonText               : string;
  jsonArray, i           : number;
  newValue, currentValue : string;
begin
  WebParseDataBound(jsonText);
  WebPageUrl("http://mycompany.com/api/jsondata");
  jsonArray := JsonParse(jsonText);
  
  //every JSON array element which contains the string "old string value" is set to "new string value"
  newValue := "new string value";
  for i := 0 to (JsonGetArrayLength(jsonArray) - 1) do
    JsonArrayGetStringElement(jsonArray, i, currentValue);
    if currentValue = "old string value" then
      JsonArraySetStringElement(jsonArray, i, newValue);
    end;
  end;
  
  /* add new value
   * index of the new value := size
   * size := size + 1 */
  JsonArraySetStringElement(jsonArray, 999999, newValue);

  JsonFree(jsonArray);
end TMain;