Custom computation

Define your own computed field method using JsonLogic. For example, return the sum of two fields, map arrays, or return a boolean indicating if a field's output is non-null.

Parameters

The following parameters are in the computed field's global Method parameter:

keyvaluedescription
id (required)customComputationThis method has access to the parsed_document object at verbosity = 0.
This method doesn't infer Sensible types. It outputs string, number, boolean, null , or an array of those. For example, adding two currencies results in a number. This method returns null if you attempt to reference a variable that Sensible can't find in the parsed_document.
jsonLogic (required)JsonLogic objectTransforms the output of one or more Field objects using JsonLogic. Supports all JsonLogic operations and extends them with Sensible operations. For more information, see the following section.
Double escape any dots in the field keys (for example, delivery\\.zip\\.code). Use dot notation to access arrays, for example, test_table.columns.3.values to access the 4th column in a table.

Sensible operations

Sensible extends JsonLogic with the following operations:

Exists

{
    "exists": [
        JsonLogic
    ]
}

Most commonly used with the JsonLogic var operation to test that an output value isn't null. The var operation retrieves extracted field values using field id keys.

Match

{
    "match": [
        JsonLogic,
        regex
    ]
}

Where regex is a Javascript-flavored regular expression.

Double escape special regex characters, since the regex is in a JSON object (for example, \\s, not \s , to represent a whitespace character). This operation does not support regular expression flags such as i for case insensitive.

Replace

One of the following syntaxes:

{
    "replace": {
        "source": JsonLogic,
        "find": JsonLogic
        "replace": JsonLogic
    }
}

Or:

{
    "replace": {
        "source": JsonLogic,
        "find_regex": regex
        "replace": JsonLogic,
        "flags": "string" //optional
    }
}

Where regex is a Javascript-flavored regular expression. Double escape special regex characters, since the regex is in a JSON object (for example, \\s, not \s , to represent a whitespace character). This operation supports:

  • regex capturing groups
  • regex flags, such as i for case insensitive.

Examples

The following example shows defining custom computed fields.

Config

{
  "fields": [
    {
      "id": "prop_limit",
      "type": "currency",
      "method": {
        "id": "query",
        "description": "property damage limit in first table (insurer A)"
      }
    },
    {
      "id": "injury_limit",
      "type": "currency",
      "method": {
        "id": "query",
        "description": "bodily injury limit in first table (insurer A)"
      }
    },
    {
      "id": "vehicle_vin",
      "type": "number",
      "method": {
        "id": "query",
        "description": "vehicle vin for insurer A"
      }
    },
    {
      "id": "insurer_b_table",
      "method": {
        "id": "nlpTable",
        "description": "table for insurer B describing insurance limits",
        "columns": [
          {
            "id": "make_and_model",
            "description": "make and model",
            "type": "string"
          },
          {
            "id": "vehicle_vin",
            "description": "vehicle vin",
            "type": "string"
          },
          {
            "id": "policy_effective_date",
            "description": "policy effective date",
            "type": "string"
          },
          {
            "id": "limits",
            "description": "limits",
            "type": "string"
          },
          {
            "id": "dollar_amount_per_limit",
            "description": "dollar amount per limit",
            "type": "string"
          }
        ]
      }
    },
  ],
  "computed_fields": [
    /* check an extracted field is non-null */
    {
      "id": "prop_limit_exists",
      "method": {
        "id": "customComputation",
        "jsonLogic": {
          "exists": [
            {
              "var": "prop_limit.value"
            }
          ]
        }
      }
    },
    /* check a field's value matches a regex */
    {
      "id": "injury_limit_matches_regex",
      "method": {
        "id": "customComputation",
        "jsonLogic": {
          "match": [
            {
              "var": "injury_limit.value"
            },
            "^\\w+$"
          ]
        }
      }
    },
    /* redact the vehicle's VIN using regex replace operation */
    {
      "id": "replace_regex",
      "method": {
        "id": "customComputation",
        "jsonLogic": {
          "replace": {
            "source": {
              "var": "vehicle_vin.value"
            },
            "find_regex": "(\\d{4})(\\d{4})",
            "replace": "xxxx$2",
            "flags": "g"
          }
        }
      }
    },
    /* modify value using JsonLogic in replace operation */
    {
      "id": "replace_var",
      "method": {
        "id": "customComputation",
        "jsonLogic": {
          "replace": {
            "source": {
              "var": "injury_limit.value"
            },
            "find": {
              "var": "injury_limit.value"
            },
            "replace": {
              "+": [
                {
                  "var": "prop_limit.value"
                },
                200
              ]
            }
          }
        }
      }
    },
    /* perform addition on an extracted field's value */
    {
      "id": "prop_limit_addition",
      "method": {
        "id": "customComputation",
        "jsonLogic": {
          "+": [
            {
              "var": "prop_limit.value"
            },
            200
          ]
        }
      }
    },
    {
      /* modify a table's column using map */
      "id": "map_table",
      "method": {
        "id": "customComputation",
        "jsonLogic": {
          /* perform map operation on each limit in the 'limits' column */
          "map": [
            {
              /* the array to be mapped: the fourth column, titled "limits", in a table
              / Note Sensible uses dot notation to access array elements,
                for example, insurer_b_table.columns.3.values
              */
              "var": "insurer_b_table.columns.3.values"
            },
            {
              /* Note it's "cat" for strings, "+" for numbers */
              "cat": [
                {
                  "var": "value"
                },
                // append the word "LIMIT" to each limit description
                " LIMIT"
              ]
            }
          ]
        }
      }
    },
  ]
}

Example document
The following image shows the example document used with this example config:

Click to enlarge

Example documentDownload link

Output

{
  "prop_limit": {
    "source": "$5,000",
    "value": 5000,
    "unit": "$",
    "type": "currency"
  },
  "injury_limit": {
    "source": "$3,000",
    "value": 3000,
    "unit": "$",
    "type": "currency"
  },
  "vehicle_vin": {
    "source": "12345678",
    "value": 12345678,
    "type": "number"
  },
  "insurer_b_table": {
    "columns": [
      {
        "id": "make_and_model",
        "values": [
          {
            "value": "Honda Accord",
            "type": "string"
          },
          {
            "value": "Honda Accord",
            "type": "string"
          },
          {
            "value": "Honda Accord",
            "type": "string"
          }
        ]
      },
      {
        "id": "vehicle_vin",
        "values": [
          {
            "value": "92343156",
            "type": "string"
          },
          {
            "value": "92343156",
            "type": "string"
          },
          {
            "value": "92343156",
            "type": "string"
          }
        ]
      },
      {
        "id": "policy_effective_date",
        "values": [
          {
            "value": "12/19/20",
            "type": "string"
          },
          {
            "value": "12/19/20",
            "type": "string"
          },
          {
            "value": "12/19/20",
            "type": "string"
          }
        ]
      },
      {
        "id": "limits",
        "values": [
          {
            "value": "Property damage*",
            "type": "string"
          },
          {
            "value": "Bodily injury**",
            "type": "string"
          },
          {
            "value": "Auto only*",
            "type": "string"
          }
        ]
      },
      {
        "id": "dollar_amount_per_limit",
        "values": [
          {
            "value": "$6,000",
            "type": "string"
          },
          {
            "value": "$4,000",
            "type": "string"
          },
          {
            "value": "$1,000",
            "type": "string"
          }
        ]
      }
    ],
    "title": {
      "type": "string",
      "value": "Insurer B"
    },
    "footer": {
      "type": "string",
      "value": "* per person ** per accident"
    }
  },
  "prop_limit_exists": {
    "value": true,
    "type": "boolean"
  },
  "injury_limit_matches_regex": {
    "value": true,
    "type": "boolean"
  },
  "replace_regex": {
    "value": "xxxx1234",
    "type": "string"
  },
  "replace_var": {
    "value": "5200",
    "type": "string"
  },
  "prop_limit_addition": {
    "value": 5200,
    "type": "number"
  },
  "map_table": [
    {
      "value": "Property damage* LIMIT",
      "type": "string"
    },
    {
      "value": "Bodily injury** LIMIT",
      "type": "string"
    },
    {
      "value": "Auto only* LIMIT",
      "type": "string"
    }
  ]
}