Object functions | Scriban
- Home
- Docs
- Built-in functions
- Object
Object functions available through the builtin object 'object'.
object.defaultobject.evalobject.eval_templateobject.formatobject.has_keyobject.has_valueobject.keysobject.sizeobject.typeofobject.kindobject.valuesobject.from_jsonobject.to_json
object.default
object.default <value> <default>
Description
The default value is returned if the input value is null or an empty string "". A string containing whitespace characters will not resolve to the default value.
Arguments
value: The input value to check if it is null or an empty string.default: The default value to return if the inputvalueis null or an empty string.
Returns
The default value is returned if the input value is null or an empty string "", otherwise it returns value
Examples
input Try out
{{ undefined_var | object.default "Yo" }}
output
Yo
object.eval
object.eval <value>
Description
The evaluates a string as a scriban expression or evaluate the passed function or return the passed value.
Arguments
value: The input value, either a scriban template in a string, or an alias function or directly a value.
Returns
The evaluation of the input value.
Examples
input Try out
{{ "1 + 2" | object.eval }}
output
3
object.eval_template
object.eval_template <value>
Description
The evaluates a string as a scriban template or evaluate the passed function or return the passed value.
Arguments
value: The input value, either a scriban template in a string, or an alias function or directly a value.
Returns
The evaluation of the input value.
Examples
input Try out
{{ "This is a template text {{ 1 + 2 }}" | object.eval_template }}
output
This is a template text 3
object.format
object.format <value> <format> <culture>?
Description
Formats an object using specified format.
Arguments
value: The input valueformat: The format string.culture: The culture as a string (e.gen-US). By default the culture from is used
Returns
Examples
input Try out
{{ 255 | object.format "X4" }}
{{ 1523 | object.format "N2" "en-US" }}
output
00FF
1,523.00
object.has_key
object.has_key <value> <key>
Description
Checks if the specified object as the member key
Arguments
value: The input object.key: The member name to check its existence.
Returns
true if the input object contains the member key; otherwise false
Examples
input Try out
{{ product | object.has_key "title" }}
output
true
object.has_value
object.has_value <value> <key>
Description
Checks if the specified object as a value for the member key
Arguments
value: The input object.key: The member name to check the existence of its value.
Returns
true if the input object contains the member key and has a value; otherwise false
Examples
input Try out
{{ product | object.has_value "title" }}
output
true
object.keys
object.keys <value>
Description
Gets the members/keys of the specified value object.
Arguments
value: The input object.
Returns
A list with the member names/key of the input object
Examples
input Try out
{{ product | object.keys | array.sort }}
output
["title", "type"]
object.size
object.size <value>
Description
Returns the size of the input object.
- If the input object is a string, it will return the length
- If the input is a list, it will return the number of elements
- If the input is an object, it will return the number of members
Arguments
value: The input object.
Returns
The size of the input object.
Examples
input Try out
{{ [1, 2, 3] | object.size }}
output
3
object.typeof
object.typeof <value>
Description
Returns string representing the type of the input object. The type can be string, boolean, number, array, iterator and object
Arguments
value: The input object.
Returns
Examples
input Try out
{{ null | object.typeof }}
{{ true | object.typeof }}
{{ 1 | object.typeof }}
{{ 1.0 | object.typeof }}
{{ "text" | object.typeof }}
{{ 1..5 | object.typeof }}
{{ [1,2,3,4,5] | object.typeof }}
{{ {} | object.typeof }}
{{ object | object.typeof }}
output
boolean
number
number
string
iterator
array
object
object
object.kind
object.kind <value>
Description
Returns string representing the type of the input object. The type can be string, bool, byte, sbyte, ushort, short, uint, int,
ulong, long, float, double, decimal, bigint, enum, range, array, function and object
Arguments
value: The input object.
Returns
Examples
This function is newer than object.typeof and returns more detailed results about the types (e.g instead of number, returns int or double)
input Try out
{{ null | object.kind }}
{{ true | object.kind }}
{{ 1 | object.kind }}
{{ 1.0 | object.kind }}
{{ "text" | object.kind }}
{{ 1..5 | object.kind }}
{{ [1,2,3,4,5] | object.kind }}
{{ {} | object.kind }}
{{ object | object.kind }}
output
bool
int
double
string
range
array
object
object
object.values
object.values <value>
Description
Gets the member's values of the specified value object.
Arguments
value: The input object.
Returns
A list with the member values of the input object
Examples
input Try out
{{ product | object.values | array.sort }}
output
["fruit", "Orange"]
object.from_json
object.from_json <json>
Description
Converts the json to a scriban value. Object, Array, string, etc.
Arguments
json: The json to deserialize.
Returns
Returns the scriban value
Examples
input Try out
{{
obj = `{ "foo": 123 }` | object.from_json
obj.foo
}}
output
123
object.to_json
object.to_json <value>
Description
Converts the scriban value to JSON.
Arguments
value: The input object.
Returns
A JSON representation of the value
Examples
input Try out
{{ { foo: "bar", baz: [1, 2, 3] } | object.to_json }}
{{ true | object.to_json }}
{{ null | object.to_json }}
output
{"foo":"bar","baz":[1,2,3]}
true
null