Implicit and Explicit Type Conversion for Json

A JSON can read and contain the following data types:

  • String

  • Boolean

  • Numeric

JSON addresses and treats every element within as a JSON. This specification means that any data type it contains is treated as JSON. The same applies to JSON lists.

Due to this specification, JSON can hold heterogeneous lists. i.e., lists that are made up of multiple data types. However, from the perspective of JSON, it is a homogenous list (as all data types are of one type i.e., JSON.)

This design of JSON may term Json as a loose Type data structure. However, Langstack, as a visual, no code programming language, is strongly typed. To work with JSON in Langstack applications, users should be aware that implicit type conversions for JSON are per the rules of Langstack implicit conversions. This ensures that users are protected from unprecedented type changes that may impact any information negatively.

To prevent users from making accidental mistakes assigning different types, Langstack applications keep track of the data types boolean, numeric, string, and Lists types. Therefore, a boolean JSON key can only take a value of a boolean data Type, a string JSON key can only take a value of a string data Type, and so on. The same applies to lists. Hence, a JSON list of strings can only add strings to its list and can only be assigned to another list of strings.

If a user wants to assign data of the type different from the data type defined for the JSON key, the user will be prompted with an alert. Suppose the user still wants to continue with the assignment. In that case, Langstack applications allow it through the explicit conversion functions as in the examples below:

  • GUID and DateTime: can be assigned to Strings after explicit conversion to the type Strings using toString() function.

  • TimeStamp: can be assigned to Numerical after explicit conversion to the type Numerical using toNumerical() function.

  • All other, complex data types such as, JXPResponse can be assigned to a JSON by calling the function toJson().

Example

Consider a JSON variable "JS1" with the key "customers", which is a list of JSONs:

{
  "customers" :[
       {
           "firstname": "string",
           "isActive": true,
           "lastLoginInDays" : 10
       }
   ]
}

The key customers is a list of JSONs that has three keys:

- "firstname" with the data type String

- "isActive" with the data type Boolean

- "lastLoginInDays" with the data type Numerical

  • Create a new variable as follows: Variable name: "JSCustomer" of JSON data type. Click [Sample] to add the sample.

  • Type in the following JSON sample and click [Save]:

{
  "firstname": "string",
  "isActive": true,
  "lastLoginInDays" : 10
}
  • Update the JSON keys with the values of the same type as of the keys:

    • "firstname": "John Doe"

    • "isActive": true

    • "lastLoginInDays" : 5

It immediately accepts updating the "JSCustomer" variable of JSON data type with string, boolean and numeric values as follows:

{"firstname":"John Doe","isActive":true,"lastLoginInDays":5.0}

  • If the user tries to assign a GUID value to “firstname,” which is of the String data type, the user will be prompted regarding the required data type.

If the user still wants to assign the GUID to the firstname field, the user must convert the GUID explicitly to a String data type using the toString() function: GUID.newGUID().toString()

  • The same applies to DateTime. If the user tries to assign a DateTime value to “firstname”, which is of the String data type, the user will be prompted with an alert regarding the required data type:

If the user still wants to assign the DateTime to "firstname", the user must convert the DateTime explicitly to the String data type using the toString() function:

DateTime.Now().toStriSuppose the user tries to assign a TimeStamp value to “lastLoginInDays”, which is of the Numerical data type. In that case,

  • If the user tries to assign a TimeStamp value to “lastLoginInDays”, which is of the Numerical data type, the user will be prompted with an alert regarding the required data type:

If the user still wants to assign the TimeStamp value "lastLoginInDays" field, the user must convert it to Numerical data type using the toNumerical() function:

TimeStamp.Now().toNumerical()

  • Now, add the variable “JSCustomer” (Json) to another variable “JSLstCustomers” (of data type List i.e. list of JSONs) through the Action “Add item to List”.

Here, if the user wants to assign a variable “lstGUID” (containing a list of GUID values) to the variable “JSLstCustomers”, the user will be prompted with an alert.

If the user still wants to assign the “lstGUID” to the variable “JSLstCustomers”, the user must convert it to JSON list data type using the toJsonList() function: lstGUID.toJsonList()

Last updated