Removing Properties and Elements

The JSON API provides several functions to remove properties from JSON objects and elements from JSON arrays.

JsonRemoveProperty

Existing properties can be removed from a JSON object.

dcltrans 
	transaction TMain 
	var 
		jsonObject : number; 
		jsonText   : string;
	begin  
		jsonObject := JsonParse(“{ \”Id\”: 1, \”Name\” : \“New Object\” }”);

		JsonToText(jsonArray, jsonText);
		Print(“Before: “ + jsonText);

		JsonRemoveProperty(jsonObject, “Name”);

		JsonToText(jsonArray, jsonText);
		Print(“After: “ + jsonText);

		JsonFree(jsonObject);
	end TMain;

Output
Before: {”Id”:1,”Name”:“New Object”}
After:  {”Id”:1}

JsonArrayRemoveElement

Existing elements can be removed from a JSON array. The index of all following elements decreases by one. Also the size of the JSON array decreases by one.

dcltrans 
	transaction TMain 
	var 
		jsonArray : number; 
		jsonText  : string;
	begin  
		jsonArray := JsonParse(“[ \“StringValue\”, 10, 20.13, true, null ]”);

		JsonToText(jsonArray, jsonText);
		Print(“Before: “ + jsonText);

		JsonArrayRemoveElement(jsonArray, 0);
		
		JsonToText(jsonArray, jsonText);
		Print(“After:  “ + jsonText);

		JsonFree(jsonArray);
	end TMain;

Output
Before: [“StringValue”,10,20.13,true,null]
After:  [10,20.13,true,null]

JsonArrayResize

This function changes the size of a JSON array. If he specified size is lower than the actual size, the last elements of the JSON array are removed. If the specified size is higher than the actual size, empty elements are added to the end of the JSON array.

transaction TMain
var
  jsonText  : string;
  jsonArray : number;
begin
  jsonArray := JsonParse("[0,1,2,3,4]");
  
  JsonArrayResize(jsonArray, 2);

  JsonToText(jsonArray, jsonText);
  Print("resized array: " + jsonText);

  JsonFree(jsonArray);
end TMain;

Output
resized array: [0,1]