Previous Topic: checkCalendarDateNext Topic: convertValueToXml


convertJson

The convertJson function converts a valid JSON string into ValueMap. Use this method to convert a JSON response from a REST service to a ValueMap object. The Valuemap object can then be traversed and accessed using the standard expressions.

Syntax

vmResult = convertJson(jsonString)

Arguments

jsonString (String)

Specifies the JSON string that needs to be parsed.

Return Value

ValueMap

ValueMap representation of the data contained in the JSON string which can be traversed or referenced with expressions.

If you pass a null value, it returns null value without an exception. If you pass an invalid JSON string, the method returns a Null value and the exceptions are logged in the logs in the server.

Example

vmResult = convertJson(Process.jsonString)

Consider an example where you invoked a REST service. The following response that is received is stored in a variable name restResponse in a process dataset:

{
    "UserName": "pamadmin",
    "age": 25,
    "address": {
        "streetAddress": "CA Technologies, 115, IT Park Area",
        "city": "Hyderabad",
        "state": "AP",
        "postalCode": "500084"
    },
    "phoneNumber": [
        {
            "type": "Office",
            "number": "04066812345"
        },
        {
            "type": "Home",
            "number": "04066854321"
        }
    ]
}

To access the values of the UserName, streetAddress, and phoneNumber, write the following code:

// Code starts
// Parse the REST response using convertJson() method and store it in a Process dataset variable named as “resultData”
Process.resultData=convertJson(Process.restResponse);
// Access “UserName” from the resultData varaible 
Process.userName = Process.resultData.UserName;
// Street Address is inside address object hence it will be accessed using the following syntax
Process.streetAddress = Process.resultData.address.streetAddress;
// Phone Number is an array and Office number is stored in the first element, hence index [0] is used.
Process.officePhoneNumber = Process.resultData.phoneNumber[0].number; 
// Code ends