Data Types & Literals
Determining type dynamically¶
- To determine type of a field, we can
item | type
.
{
"name": {
"first": "Malory",
"last": "Archer"
},
"exes": [
"Nikolai Jakov",
"Len Trexler",
"Burt Reynolds"
],
"lastEx": 2,
"isIndian": false,
"salary": 35000.002,
"typeOfName": "Object",
"typeOfExes": "Array",
"typeOfLastEx": "Number",
"typeOfIsIndian": "Boolean",
"typeOfSalary": "Number",
"typeOfX": "undefined"
}
- Which will return if it is
String
,Number
,Boolean
,Object
,Array
, orundefined
. - Currently we only support the mentioned 6 data types.
Literals¶
- You can also define literals anywhere in the transforms for the supported data types.
- String literals:
'Hello World'
- You can simply concat two different string literals using
+
operator, for example :'Hello' + 'World'
- Additionally, whenever you concat an empty string literal with other fields of data type
Number
,Boolean
orArray
, the result is implicitly type-casted to string.true + ''
->'true'
2 + ''
->'2'
['Jam','Your','JSON'] + ''
->'Jam, Your, JSON'
- It is not advisable to perform the above same operation on fields with
Object
type, as you would get an output like{} + ''
->[object Object]
. - Instead the right way to convert an object to string would be to use in-built
stringify
.{x:2} | stringify
->'{\"x\":2}'
, The output would be escaped, as datadance deals with JSON input, output and transform specification.
- Number literals:
- You can simply define your number directly as a value to a field in the transforms and that will be automatically reflected in the output.
2.0
->2
, Any decimals with zero fractional part will be converted to non-decimal numbers by default.
- Boolean literals:
- You can represent boolean values by
true
orfalse
. - Internally boolean values represent the values
1
fortrue
and0
forfalse
. So the below operations are valid in that sense.true + true
->2
- You can also add a boolean and a number :
1 + true
->2
. false + 1 == true
->true
- You can represent boolean values by
- Object literals:
- You can simply define an empty object literal by using
{}
.
In Object literals, the keys are not string literals
Let us say you want to define object literal there would be an error if you supply
{'x':2}
, Instead you can simply give{x:2}
or{x:'Hello World'}
. There is no need to provide the keys in the object as string literals, the object is parsed and evaluated internally.- You can define other data type fields inside object literals.
- You can define such expression
{x:{y:[{g:'Hello'}]}}
, and this will be evaluated to in the output JSON.
- You can define such expression
- You can simply define an empty object literal by using
- Array literals:
- You can define an array literal simply by using
[]
. - Also a matrix can be represented using the same notation :
[[1,2,3], [3,4,5]]
- You can define an array literal simply by using