Add (Json function)

From m204wiki
Jump to navigation Jump to search

Add an item to a JSON array (Json class)

[Introduced in Model 204 7.6]

This functions adds a Json object to a Json array.


[%number =] json:Add( json) Throws InvalidJsonType

Syntax terms

%numberThe number of items in the array after the Add.
Json Json object, which must be an array or an InvalidJsonType exception is thrown.
json Json object that can be Null.

Usage notes

  • The added Json object becomes the last item in the array.
  • The value returned by Add is the same value that would be returned by an immediately following Count function.
  • Unlike the Arraylist and Stringlist Add methods, only one item can be added with the Json Add function.


The following request adds the string "Start", the first 10 prime numbers, and then the string "End" to a Json object that is an array. This illustrates how different Json object types can be added to the same array. In fact, it would also possible to add a Boolean, Object, or even another Array type to the array.

b %i float %json is object json %prime is float %json = array %json:add("Start") for %i from 1 to 10 %prime = %prime:nextPrime %json:add(%prime) end for %json:add("End") printtext {~=%json} end

This program prints:


See also