Skip to content

Data Types & Literals

Determining type dynamically

  • To determine type of a field, we can item | type.
  • Input JSON


    {
      "name": {
        "first": "Malory",
        "last": "Archer"
      },
      "exes": [
        "Nikolai Jakov",
        "Len Trexler",
        "Burt Reynolds"
      ],
      "lastEx": 2,
      "isIndian" : false,
      "salary" :  35000.002 
    }
    
  • Transformed JSON


    {
        "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"
    }
    
  • Transforms


    [
        {
            "typeOfName": "input.name | type"
        },
        {
            "typeOfExes": "input.exes | type"
        },
        {
            "typeOfLastEx": "input.lastEx | type"
        },
        {
            "typeOfIsIndian": "input.isIndian | type"
        },
        {
            "typeOfSalary": "input.salary | type"
        },
        {
            "typeOfX": "input.x | type"
        }
    ]
    
  • Which will return if it 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 or Array, 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 or false.
    • Internally boolean values represent the values 1 for true and 0 for false. 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
  • 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
        {
            "x": {
                "y": [
                        {
                            "g": "Hello"
                        }
                    ]
            }
        }
        
        in the output JSON.
  • 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]]