JSON functions

The following functions are associated with the JSON type used in Robots.

Conversion functions

Conversion functions convert different types to JSON and back. If the JSON value does not match the corresponding target type, the robot will throw a ConversionIssue exception.

Name

Description

Example

json: T -> JSON

Converts values of other types to JSON.

This works for all types except Password.

5.json() evaluates the JSON value to 5.

json: Record -> JSON

Converts a Record value to a JSON object.

Binary fields are base64 encoded, passwords become their hash code value (the same is displayed in the State pane).

myRecord.json()

json: (Record, Boolean) -> JSON

Converts a Record value to a JSON object. The binary fields are left out if the Boolean value is false.

myRecord.json(false)

text: JSON -> Text

Converts a JSON String value to a Text value.

"hello".json().text() evaluates to the Text value "hello".

integer: JSON -> Integer

Converts a JSON Integer value to an Integer value.

5.json().integer() evaluates to 5

number: JSON -> Number

Converts a JSON Number value to a Number value.

5.1.json().number() evaluates to 5.1

boolean: JSON -> Boolean

Converts a JSON Boolean value to a Boolean value.

true.json().boolean() evaluates to true

Test functions

Test functions test the type of a JSON value. They also test a case where an object value has a given key.

Name

Description

Example

isString: JSON -> Boolean

Returns true if the JSON value is a String.

"hello".json().isString() evaluates to true, and 5.json().isString() evaluates to false

isNumber: JSON -> Boolean

Returns true if the JSON value is a Number.

5.1.json().isNumber() evaluates to true

isWhole: JSON -> Boolean

Returns true if the JSON value is a Number that is also an Integer.

5.json().isWhole() evaluates to true

isBoolean: JSON -> Boolean

Returns true if the JSON value is a Boolean.

false.json().isBoolean() evaluates to true

isObject: JSON -> Boolean

Returns true if the JSON value is an Object.

{}.isObject() evaluates to true

isArray: JSON -> Boolean

Returns true if the JSON value is an Array.

[].isArray() evaluates to true

isNull: JSON -> Boolean

Returns true if the JSON value is null.

null.isNull() evaluates to true

hasKey: (JSON, Text) -> Boolean

Returns true if the JSON value is an object with a given key.

{"a": 5}.hasKey("a") evaluates to true

Parse and Stringify functions

Parse and Stringify functions convert a text representation of JSON to a JSON value and back.

Name

Description

Example

parse: Text -> JSON

Parses a text into a JSON value.

This function is different from the function json. For example: "null".json() returns the Text value "null", whereas "null".parse() returns the JSON value null.

"[2, true, {}]".parse() evaluates to a JSON array consisting of the number 2, the Boolean true and the empty object {}

stringify: JSON -> Text

Stringify is the inverse function of Parse. It returns a Text representing the JSON.

[2, true, {}].stringify() evaluates to the text "[2, true, {}]"

Object functions

Object functions modify object values by adding or removing keys.

Name

Description

Example

put: (JSON, Text, T) -> JSON

Creates a new JSON object by adding a new key/value pair to it or overriding an existing one. This is an immutable operation, so it does not change the value of the original object.

{"a": 5, "b": true}.put("a", 7) evaluates to {"a": 7,"b": true}

remove: (JSON, Text) -> JSON

Removes a key/value pair from a JSON array.

{"a": 5, "b": true}.remove("a") evaluates to {"b": true}

keys: JSON -> JSON

Returns a JSON array consisting of keys of a JSON object.

{"a": 5,"b": true}.keys() evaluates to ["a", "b"]

Array functions

Array functions modify or ask questions about arrays.

Name

Description

Example

remove: (JSON, Integer) -> JSON

Removes an element from a JSON array.

[true, 2, null].remove(1) evaluates to [true, null]

add: (JSON, T) -> JSON

Adds an element to the end of a JSON array.

[true, 2].add(null) evaluates to [true, 2, null]

add: (JSON, Integer, T) -> JSON

Adds an element at the given index to a JSON array.

[true, 2].add(0, null) evaluates to [null, true, 2]

length: JSON -> Integer

Returns the length of a JSON array.

[true, 2, null].length() evaluates to 3

isEmpty: JSON -> Boolean

Returns true if a JSON array is empty.

[],isEmpty() evaluates to true

nonEmpty: JSON -> Boolean

Returns false if a JSON array is not empty.

[true, 2, null].nonEmpty() returns true

sort: (JSON (array), Boolean (optional)) -> JSON (array)

Sorts an array of numbers or strings.

To select a sorting order, specify an optional Boolean argument: true is ascending (default) and false is descending.

[1, 3, 2, 6, 5].sort() evaluates to [1, 2, 3, 5, 6]

[1, 3, 2, 6, 5].sort(false) evaluates to [6, 5, 3, 2, 1]

sort: (JSON (array), Text, Boolean (optional)) -> JSON (array)

Sorts an array of objects based on the value of the given key. The Text argument is the key to sort by.

To select a sorting order, specify an optional Boolean argument: true is ascending (default) and false is descending.

[{"a": 0, "b": true},{"a": 2, "c": null},{"a": 1, "b": false}].sort("a") evaluates to [{"a": 0, "b": true},{"a": 1, "b": false},{"a": 2, "c": null}]

subsequence: (JSON (array), Integer) -> JSON (array)

Returns a new JSON array consisting of elements from a given index.

[1, 2, 3, 4, 5].subsequence(2) evaluates to [3, 4, 5]

subsequence: (JSON (array), Integer, Integer) -> JSON (array)

Returns a new JSON array consisting of elements from the first index up to (but not including) the second index.

[1, 2, 3, 4, 5].subsequence(1, 3) evaluates to [2,3]

distinct: JSON (array) -> JSON (array)

Removes duplicates and returns a new JSON array with distinct values.

[1, 4, 2, 3, 1, 4, 4].distinct() evaluates to [1, 4, 2, 3]

reverse: JSON (array) -> JSON (array)

Returns a JSON array with the elements in the reversed order.

[1, 3, 4, 2].reverse() evaluates to [2, 4, 3, 1]

Array creation functions

Array creation functions create arrays dynamically.

The size of an array generated by the range or fill function is limited to 2147483647 elements. However, if the value does not exceed the limit, but is still large, your robot may run out of memory.

Name

Description

Example

range: (Integer, Integer, Integer) -> JSON (array)

range(start, stop, step) returns a JSON array of a sequence of integers. The stop value is included.

range(0, 10, 2) evaluates to [0, 2, 4, 6, 8, 10]

range: (Integer, Integer) -> JSON (array)

range(start, stop)returns a JSON array of a sequence of Integers. The step value is 1 by default. The stop value is included.

range(5, 10) evaluates to [5, 6, 7, 8, 9, 10]

range: (Integer) -> JSON (array)

range(stop) returns a JSON array of a sequence of Integers. The start value is 0. The step value is 1. The stop value is included.

range(5) evaluates to[0, 1, 2, 3, 4, 5]

fill: (Integer, T) -> JSON (array)

Returns a JSON array containing the given value the given number of times.

fill(5, 1) evaluates to [1, 1, 1, 1, 1]

Path functions

Use paths, where a path is a Text value corresponding to a path in a JSON value. For example: ".a[5].b".

Name

Description

Example

updateAtPath: (JSON, Text, JSON) -> JSON

Updates the first JSON value at a given path (second argument) with a value (third argument). The second argument can also be a JSON text.

{"a":[1]}.updateAtPath(".a[0]", null) evaluates to {"a": [null]}

lookupPath: (JSON, Text) -> JSON

Returns the value of JSON at a given JSON path (the second argument)

{"a": [1]}.lookupPath(".a[0]") evaluates to 1

containsPath: (JSON, Text) -> Boolean

Returns true if the given JSON path matched a location in the JSON value, otherwise returns false.

{"a": [1]}.containsPath(".a[0]") evaluates to true

{"a": [1]}.containsPath(".b") returns false