core

append(array arr, any val)

Given arr and val, appends val to the end of arr.

Example

Payload
[
    2,
    3,
    5,
    7
]
DataSonnet map:
ds.append(payload, 11)
Result
[
  2,
  3,
  5,
  7,
  11
]

combine(any first, any second)

Combines first and second. Some values will auto-coerce, e.g. the number 5 will auto coerce to the string "5."

Example

DataSonnet map:
{
    string: ds.combine("Hello ", "World"),
    number: ds.combine(5, 7),
    auto1: ds.combine("Hello ", 5),
    auto2: ds.combine(5, "10"),
    array: ds.combine([1,2], [3,4]),
    obj: ds.combine({a:1}, {b:2})
}
Result
{
  "string": "Hello World",
  "number": "57",
  "auto1": "Hello 5",
  "auto2": "510",
  "array": [
    1,
    2,
    3,
    4
  ],
  "obj": {
    "a": 1,
    "b": 2
  }
}

contains(array|string item, any val)

If item is an array, returns true if item contains val.

If item is a string, returns true if item contains the sub string val.

Example

Payload
[
    2,
    3,
    5,
    7
]
DataSonnet map:
ds.contains(payload, 2)
Result
true

distinctBy(array|object item, function discriminator)

Returns a new object containing only the items that are a unique result from discriminator.

The function discriminator is expected to take the value as the first parameter (required) and the index as the second (optional).

Example

Payload
{
  "array": [
    1,
    1,
    2,
    2,
    3,
    3,
    4,
    4,
    5
  ],
  "obj": {
    "a": 1,
    "b": 2,
    "c": 1
  }
}
DataSonnet map:
{
    array: ds.distinctBy(payload.array, function(item,index) item),
    obj: ds.distinctBy(payload.obj, function(value,key) value)
}
Result
{
  "array": [
    1,
    2,
    3,
    4,
    5
  ],
  "obj": {
    "a": 1,
    "b": 2
  }
}

endsWith(string str, string subStr)

Returns true if str ends with subStr. Ignores casing.

Example

Payload
{
    "name": "Scala",
    "version": "1.0"
}
DataSonnet map:
ds.endsWith(payload.version, ".0")
Result
true

entriesOf(object obj)

Returns an array of objects describing each key value pair of obj.

Example

Payload
{
    "name": "Scala",
    "version": "1.0"
}
DataSonnet map:
ds.entriesOf(payload)
Result
[
  {
    "value": "Scala",
    "key": "name"
  },
  {
    "value": "1.0",
    "key": "version"
  }
]

filter(array arr, function func)

Filters arr depending on the result of func.

The function func is expected to take the value as the first parameter (required) and the index as the second (optional).

Example

Payload
[
    1,
    2,
    3,
    4
]
DataSonnet map:
ds.filter(payload, function(value, index) value < 3)
Result
[
  1,
  2
]

filterObject(object obj, function func)

Filters obj depending on the result of func.

The function func is expected to take the property value as the first parameter (required), the property key as the second (optional) and the index as the third (optional).

Example

Payload
{
    "version": 1.7
}
DataSonnet map:
ds.filterObject(payload, function(value, key, index) value > 1.5)
Result
{
  "version": 1.7
}

find(string|array item, any val)

Returns an array containing the location where val occurs in item.

Example

Payload
{
    "string": "Hello World",
    "array": [1,2,3,4]
}
DataSonnet map:
{
    string: ds.find(payload.string, "World"),
    array: ds.find(payload.array, 3)
}
Result
{
  "string": [6],
  "array": [2]
}

flatten(array arr)

Given arr, which contains one level arrays, creates a flat array.

Example

Payload
[
  [
    1,
    2
  ],
  [
    3,
    4
  ]
]
DataSonnet map:
ds.flatten(payload)
Result
[
  1,
  2,
  3,
  4
]

flatMap(array arr, function func)

Given an array of arrays arr, creates a flat array using the outcome of func.

The function func is expected to take the value as the first parameter (required) and the index as the second (optional).

Example

Payload
[
    [
        2,
        3,
        5,
        7
    ],
    [
        11,
        13,
        17,
        19
    ]
]
DataSonnet map:
ds.flatMap(payload, function(value, index) value)
Result
[
  2,
  3,
  5,
  7,
  11,
  13,
  17,
  19
]

foldLeft(array arr, any initVal, function func)

Iterates over arr, applying func to the previous result. Starts with the value provided in initVal.

The function func is expected to take the current value as the first parameter (required) and the previous value as the second parameter (required).

Example

Payload
[
    1,
    2,
    3,
    4
]
DataSonnet map:
ds.foldLeft(payload, 1, function(curr, prev) curr * prev)
Result
24
/*
  1 * 1 = 1
  2 * 1 = 2
  3 * 2 = 6
  4 * 6 = 24
*/

foldRight(array arr, any initVal, function func)

Iterates backwards over an array, applying func to the previous result. Starts with the value provided in initVal.

The function func is expected to take the current value as the first parameter (required) and the previous value as the second parameter (required).

Example

Payload
[
    1,
    2,
    3,
    4
]
DataSonnet map:
ds.foldRight(payload, 1, function(curr, prev) curr * prev)
Result
24
/*
  4 * 1 = 4 // 1 in this case is the initial value
  3 * 4 = 12
  2 * 12 = 24
  1 * 24 = 24
*/

groupBy(array|object items, function discriminator)

Groups the provided items into an object based on the result of discriminator.

The function discriminator is expected to take the value as the first parameter (required) and the index as the second (optional).

Example

Payload
{
    "array": [
        "a",
        "b",
        "a"
    ],
    "obj": {
        "a":"Alpha",
        "b":"Bravo",
        "c": "Alpha"
    }
}
DataSonnet map:
{
    array: ds.groupBy(payload.array, function(item,index) item ),
    obj: ds.groupBy(payload.obj, function(value,key) value)
}
Result
{
  "array": {
    "a": [
      "a",
      "a"
    ],
    "b": [
      "b"
    ]
  },
  "obj": {
    "Alpha": {
      "a": "Alpha",
      "c": "Alpha"
    },
    "Bravo": {
      "b": "Bravo"
    }
  }
}

isArray(any valToCheck)

Accepts any given value as valToCheck and checks if it is of type array.

Example

Payload
[
    1,
    2,
    3,
    4
]
DataSonnet map:
ds.isArray(payload)
Result
true

isBlank(string strToCheck)

Checks if strToCheck is blank. Also returns true if null.

Example

DataSonnet map:
{
    str1: ds.isBlank("     "),
    str2: ds.isBlank(""),
    'null': ds.isBlank(null)
}
Result
{
  "str1": true,
  "str2": true,
  "null": true
}

isBoolean(any valToCheck)

Accepts any given value as valToCheck and checks if it is of type bool.

Example

Payload
{
  "name": "Java",
  "isObjectOriented": true
}
DataSonnet map:
ds.isBoolean(payload.isObjectOriented)
Result
true

isDecimal(num numToCheck)

Checks that the input number numToCheck is a decimal number. Trailing zeros are ignored.

Example

DataSonnet map:
{
    a: ds.isDecimal(2),
    b: ds.isDecimal(2.0),
    c: ds.isDecimal(2.1),
}
Result
{
  "a": false,
  "b": false,
  "c": true
}

isEmpty(any valToCheck)

Checks if valToCheck is empty. Does not ignore white space if string. Returns true if null.

Example

DataSonnet map:
{
    "null": ds.isEmpty(null),
    str: ds.isEmpty("    "),
    array: ds.isEmpty([]),
    obj: ds.isEmpty({})
}
Result
{
  "null": true,
  "str": false,
  "array": true,
  "obj": true
}

isEven(num numToCheck)

Checks that the input number numToCheck is an even number.

Example

Payload
{
    "version": 2.0
}
DataSonnet map:
ds.isEven(payload.version)
Result
true

isFunction(any valToCheck)

Accepts any given value valToCheck and checks if it is of type function.

Example

DataSonnet map:
ds.isFunction(function() "5")
Result
true

isInteger(num numToCheck)

Checks that the input number numToCheck is an integer. Trailing zeros are ignored.

Example

Payload
{
    "version": 2.0
}
DataSonnet map:
ds.isInteger(payload.version)
Result
true

isNumber(any valToCheck)

Accepts any given value valToCheck and checks if it is of type number.

Example

Payload
{
    "age": 5
}
DataSonnet map:
ds.isNumber(payload.age)
Result
true

isObject(any valToCheck)

Accepts any given value valToCheck and checks if it is of type object.

Example

Payload
{
    "language": "Java"
}
DataSonnet map:
ds.isObject(payload)
Result
true

isOdd(num numToCheck)

Checks that numToCheck is an odd number.

Example

Payload
{
    "age": 5
}
DataSonnet map:
ds.isOdd(payload.age)
Result
true

isString(any valToCheck)

Accepts any given value valToCheck and checks if it is of type string.

Example

Payload
{
    "language":"Java"
}
DataSonnet map:
ds.isString(payload.language)
Result
true

joinBy(array arr, string separator)

Joins arr into a string with the provided separator.

Example

Payload
{
    "versions": [1.0, 1.2, 1.7, 1.8]
}
DataSonnet map:
ds.joinBy(payload.versions, ", ")
Result
"1, 1.2, 1.7, 1.8"

keysOf(object obj)

Returns an array of all the key names in obj.

Example

Payload
{
   "departureDate": "01/20/2019",
   "origin": "PHX",
   "destination": "SEA"
 }
DataSonnet map:
ds.keysOf(payload)
Result
[
  "departureDate",
  "origin",
  "destination"
]

lower(string str)

Converts str to all lower case characters.

Example

Payload
{
   "origin": "PHX",
   "destination": "SEA"
 }
DataSonnet map:
ds.lower(payload.origin)
Result
"phx"

map(array arr, function func)

Loops through all items in arr, applies func to each, and returns a new array containing each result. Returns null if arr is null.

The function func is expected to take the value as the first parameter (required) and the index as the second (optional).

Example

Payload
{
    "versions": [1.0, 1.2, 1.7, 1.8]
}
DataSonnet map:
ds.map(payload.versions, function(value, index) value > 1.2)
Result
[
  false,
  false,
  true,
  true
]

mapEntries(object obj, function func)

Loops through all properties in obj, applies func to each, and returns a new array containing each result.

The function func is expected to take the property value as the first parameter (required), the property key as the second (optional) and the index as the third (optional).

Example

Payload
{
   "origin": "PHX",
   "destination": "SEA"
}
DataSonnet map:
ds.mapEntries(payload, function(value, key, index) value)
Result
[
  "PHX",
  "SEA"
]

mapObject(object obj, function func)

Loops through all properties in obj, applies func to each, and returns a new object containing each result.

The function func is expected to take the property value as the first parameter (required), the property key as the second (optional) and the index as the third (optional).

Example

Payload
{
   "origin": "PHX",
   "destination": "SEA"
}
DataSonnet map:
ds.mapObject(payload, function(value, key, index) {[key]:value})
Result
{
  "origin": "PHX",
  "destination": "SEA"
}

match(string str, string regex)

Executes the regex expression regex against str and returns an array with the match groups.

Example

Payload
{
    "email": "test@server.com"
}
DataSonnet map:
ds.match(payload.email, "(.*)@(.*)(.com)")
Result
[
  "test@server.com",
  "test",
  "server",
  ".com"
]

matches(string str, string regex)

Executes the regex expression regex against str and returns true or false if the expression matches the input.

Example

Payload
{
    "email": "test@server.com"
}
DataSonnet map:
ds.matches(payload.email, "(.*)@(.*)(.com)")
Result
true

max(array arr)

Returns the max value in arr.

Example

Payload
[
    5,
    2,
    7,
    3
]
DataSonnet map:
ds.max(payload)
Result
7

maxBy(array arr, function func)

Returns the max result of func in arr.

The function func is expected to take the value as the first parameter (required).

Example

Payload
[
    {"age": 5},
    {"age": 7},
    {"age": 3}
]
DataSonnet map:
ds.maxBy(payload, function(value) value.age)
Result
{
  "age": 7
}

min(array arr)

Returns the min value in arr.

Example

Payload
[
    5,
    2,
    7,
    3
]
DataSonnet map:
ds.min(payload)
Result
2

minBy(array arr, function func)

Returns the max result of func in arr.

The function func is expected to take the value as the first parameter (required).

Example

Payload
[
    {"age": 5},
    {"age": 7},
    {"age": 3}
]
DataSonnet map:
ds.minBy(payload, function(value) value.age)
Result
{
  "age": 3
}

orderBy(array|object items, function func)

Reorders the array items by the result of func.

If items is an array: the function func is expected to take the value as the first parameter (required).

If items is an object: the function func is expected to take the value as the first parameter (required) and the key as the second parameter (optional).

Example

Payload
[
    {"age": 5},
    {"age": 7},
    {"age": 3}
]
DataSonnet map:
ds.orderBy(payload, function(value) value.age)
Result
[
  {
    "age": 3
  },
  {
    "age": 5
  },
  {
    "age": 7
  }
]

parseDouble(string str)

Parses a string str containing a number and returns its decimal value. Trailing zeros are ignored.

Example

Payload
{
    "version":"1.5"
}
DataSonnet map:
ds.parseDouble(payload.version)
Result
1.5

parseHex(string str)

Parses a hex value given as a string str and returns its decimal value.

Example

Payload
{
    "hex":"F"
}
DataSonnet map:
ds.parseHex(payload.hex)
Result
15

parseInt(string str)

Parses an int value given as a string str and returns its integer value.

Example

Payload
{
    "number":"50"
}
DataSonnet map:
ds.parseInt(payload.number)
Result
50

parseOctal(string str)

Parses an octal value given as a string str and returns its integer value.

Example

Payload
{
    "octal":"107136"
}
DataSonnet map:
ds.parseOctal(payload.octal)
Result
36446

prepend(array arr, any val)

Given arr and val, inserts val at the beginning of arr.

Example

Payload
[
    2,
    3,
    4
]
DataSonnet map:
ds.prepend(payload, 1)
Result
[
  1,
  2,
  3,
  4
]

range(number start, number end)

Returns an array with the numbers from the start to the end of the range, inclusive.

Example

Payload
{
    "start": 0,
    "end": 3
}
DataSonnet map:
ds.range(payload.start, payload.end)
Result
[
  0,
  1,
  2,
  3
]

read(string data, string mimeType, object params)

Reads a string data as the given mimetype.

Example

DataSonnet map:
ds.read("{\"price\": 8.95}", "application/json", {})
Result
{
  "price": 8.95
}

readUrl(string url)

Reads url and returns the content of the url, if it’s JSON.

Example

DataSonnet map:
ds.readUrl("http://httpbin.org/get")
Result
{
  "args": {},
  "headers": {
    "Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
    "Host": "httpbin.org",
    "User-Agent": "Java/14.0.1",
    "X-Amzn-Trace-Id": "Root=1-5f7f568d-481e623471c21cc2686e53e8"
  },
  "origin": "69.250.49.68",
  "url": "http://httpbin.org/get"
}

remove(array|object item, string|array value)

Removes value from item and returns the remaining array or object. All properties of the object can be removed using a value in the array format.

Example

Payload
{
    "array": [
        1,
        2,
        3,
        4
    ],
    "obj": {
        "a": 1,
        "b": 2
    }
}
DataSonnet map:
{
  array: ds.remove(payload.array, 3),
  obj: ds.remove(payload.obj, "b"),
  emptyObj: ds.remove(payload.obj, ["a","b"])
}
Result
{
  "array": [
    1,
    2,
    4
  ],
  "obj": {
    "a": 1
  },
  "emptyObj": {}
}

removeMatch(array|object items, any val)

Given an array or an object items and val of the same type, removes the matching values. If items is an object, both key and value must match.

Example

Payload
{
    "array": [1,2,3,4],
    "obj": {"a":1,"b":2}
}
DataSonnet map:
{
  array: ds.removeMatch(payload.array, [1,4]),
  obj: ds.removeMatch(payload.obj, {a:1,b:3})
}
Result
{
  "array": [
    2,
    3
  ],
  "obj": {
    "b": 2
  }
}

replace(string phrase, string regex, string replacement)

Replaces the matching regex with the replacement in the phrase.

Example

Payload
{
    "regex": "Hello",
    "replacement": "Goodbye"
}
DataSonnet map:
ds.replace("Hello World", payload.regex, payload.replacement)
Result
"Goodbye World"

reverse(array|object items)

Given an array or object as items, reverses the order of the elements.

Example

Payload
{
    "array": [
        1,
        2,
        3,
        4
    ],
    "obj": {
        "a":1,
        "b":2
    }
}
DataSonnet map:
{
  array: ds.reverse(payload.array),
  obj: ds.reverse(payload.obj)
}
Result
{
  "array": [
    4,
    3,
    2,
    1
  ],
  "obj": {
    "b": 2,
    "a": 1
  }
}

scan(string str, string regex)

Executes the regex expression regex against str and returns an array with each match as an array.

Example

Payload
{
    "email": "test@server.com"
}
DataSonnet map:
ds.scan(payload.email, "(.*)@(.*)(.com)")
Result
[
  [
    "test@server.com",
    "test",
    "server",
    ".com"
  ]
]

select(object obj, string path)

Returns a value inside obj by the provided path. For nested objects, the path is separated by a dot ('.').

Example

Payload
{
  "language": {
      "name": "Java",
      "version": "1.8"
  }
}
DataSonnet map:
{
  language: ds.select(payload, 'language.name')
}
Result
{
  "language": "Java"
}

sizeOf(any val)

Returns the size of val.

Example

Payload
{
    "array": [1, 2],
    "obj": {"prop": 2},
    "string": "x"
}
DataSonnet map:
{
    array: ds.sizeOf(payload.array),
    object: ds.sizeOf(payload.obj),
    'null': ds.sizeOf(null),
    'function': ds.sizeOf(function(a,b,c) 1),
    string: ds.sizeOf(payload.string)
}
Result
{
  "array": 2,
  "object": 1,
  "null": 0,
  "function": 3,
  "string": 1
}

splitBy(string strToSplit, string regex)

Splits strToSplit into an array based on the matching regex.

Example

Payload
{
    "string": "Hello World"
}
DataSonnet map:
ds.splitBy(payload.string, " ")
Result
[
  "Hello",
  "World"
]

startsWith(string str, string subStr)

Checks if str starts with subStr. Ignores casing.

Example

Payload
{
    "string": "Hello World"
}
DataSonnet map:
ds.startsWith(payload.string, "hello")
Result
true

toString(any val)

Returns val to a string.

Example

Payload
{
    "num": 5
}
DataSonnet map:
ds.toString(payload.num)
Result
"5"

trim(string str)

Removes leading and trailing spaces in str.

Example

Payload
{
    "string": "      Hello World       "
}
DataSonnet map:
ds.trim(payload.string)
Result
"Hello World"

typeOf(any val)

Returns a string describing the type of object val is.

Example

DataSonnet map:
{
    string: ds.typeOf(""),
    bool: ds.typeOf(true),
    "null": ds.typeOf(null),
    number: ds.typeOf(0),
    "function": ds.typeOf(function() 1),
    array: ds.typeOf([]),
    object: ds.typeOf({})
}
Result
{
  "string": "string",
  "bool": "boolean",
  "null": "null",
  "number": "number",
  "function": "function",
  "array": "array",
  "object": "object"
}

unzip(array arr)

Unzips an array of arrays arr and creates a new array of arrays based on their index in arr.

Example

Payload
[
  [
    1,
    2
  ],
  [
    1,
    2
  ]
]
DataSonnet map:
ds.unzip(payload)
Result
[
  [
    1,
    1
  ],
  [
    2,
    2
  ]
]

upper(string str)

Converts a string to all uppercase characters.

Example

Payload
{
    "string": "HeLlO wOrLd"
}
DataSonnet map:
ds.upper(payload.string)
Result
"HELLO WORLD"

uuid

Generates random alphanumeric uuid.

Example

DataSonnet map:
ds.uuid
Result
"cj36alpm-8mlt-fm43-8vth-mbd961259lqh"

valuesOf(object obj)

Given an object obj, returns an array of the values inside obj.

Example

Payload
{
   "origin": "PHX",
   "destination": "SEA"
}
DataSonnet map:
ds.valuesOf(payload)
Result
[
  "PHX",
  "SEA"
]

write(array|object item, string mimeType, object params)

Converts item to a string.

Example

Payload
{
    "price": 8.95
}
DataSonnet map:
ds.write(payload, "application/json", {})
Result
"{\"price\":8.95}"

zip(array array1, array array2)

Accepts array1 and array2 and combines them into one using elements with matching indexes.

Example

Payload
{
    "firstNames": ["Evelyn", "Herman"],
    "lastNames": ["Waugh" , "Melville", "Tolkien"]
}
DataSonnet map:
ds.zip(payload.firstNames, payload.lastNames)
Result
[
  [
    "Evelyn",
    "Waugh"
  ],
  [
    "Herman",
    "Melville"
  ]
]