Map to JSON

Maps data from a JSONPath file and stores it in variables, returning a boolean if the execution was successful.

Command availability: IBM RPA SaaS and IBM RPA on premises

Description

You can map the data types of your business model into JSON by using this command. It is also possible to select different parts of a JSON based on a JSONPath.

JavaScript Object Notation (JSON) is a template for storing and transmitting information in text format.

Script syntax

IBM RPA's proprietary script language has a syntax similar to other programming languages. The script syntax defines the command's syntax in the script file. You can work with this syntax in IBM RPA Studio's Script mode.

mapJson [--handleError(Boolean)] --json(String) --mappings(String) (Boolean)=value

Input Parameter

The following table displays the list of input parameters available in this command. In the table, you can see the parameter name when working in IBM RPA Studio's Script mode and its Designer mode equivalent label.

Designer mode label Script mode name Required Accepted variable types Description
Handle Error handleError Optional Boolean Option for the script not to stop if there is an error during execution.
JSON json Required Text JSON file to be mapped.
Mappings mappings Required Text Name of the field (parameter) to map and the variable that should receive the value found when searching for that field.
For more options see the mappings parameter options.

mappings parameter options

The following table displays the options available for the mappings input parameter. The table shows the options available when working in Script mode and the equivalent label in the Designer mode.

Designer mode label Script mode name Description
Parameter mappings Enter the parameter name or the JSONPath corresponding to the parameter to map.
Value mappings Enter the variable to be receive the value.

Output parameters

Designer mode label Script mode name Accepted variable types Description
Success value Boolean Returns "True" if the command runs successfully, otherwise returns "False".

Example

The command maps JSON text data, returning name, status, and city.

defVar --name success --type Boolean
defVar --name name --type String
defVar --name status --type String
defVar --name city --type String
mapJson --json "{\"name\":\"Ana\", \"status\": \"Approved\", \"city\": \"New York\"}" --mappings "name=${name},status=${status},city=${city}" success=value
logMessage --message "Success: ${success};\r\nName: ${name};\r\nStatus:${status};\r\nCity: ${city}." --type "Info"
// Executing the command returns:
// Success: True; 
// Name: Ana; 
// Status: Approved; 
// City: New York.

Example 2

The command maps JSON text data, returning the first phone number type using a specific JSONPath.

defVar --name phonenumberType --type String
mapJson --json "{\n  \"firstName\": \"John\",\n  \"lastName\" : \"doe\",\n  \"age\"      : 26,\n  \"address\"  : {\n    \"streetAddress\": \"naist street\",\n    \"city\"         : \"Nara\",\n    \"postalCode\"   : \"630-0192\"\n  },\n  \"phoneNumbers\": [\n    {\n      \"type\"  : \"iPhone\",\n      \"number\": \"0123-4567-8888\"\n    },\n    {\n      \"type\"  : \"home\",\n      \"number\": \"0123-4567-8910\"\n    }\n  ]\n}" --mappings "$.phoneNumbers[:1].type=${phonenumberType}"
logMessage --message "Phone number type: ${phonenumberType}" --type "Info"
// Executing the command returns:
// Phone number type: iPhone;

Limitations

JSONPath with multiple tokens

This command does not accept JSONPath that returns multiple tokens.

Arrays as input

The Map to JSON (mapJson) command does not support JSON arrays as input, for example:

[{"one":"two"},{"three":3}] 

You can add the array to a parent JSON object, as a workaround:

{"array":[{"one":"two"},{"three":3}]}