Using ‘Value Expression’ in OpenBots Documents.

This guide can help you create simple expressions that combine and manipulate data extracted from documents into what you exactly need. The Expressions using Liquid is a powerful templating language that allows you to create dynamic output by combining fields extracted from a document.

What is Liquid templating language?

Liquid is a templating language that allows you to insert dynamic content in your output. In our system, Liquid can be used to process and format data extracted from Documents. For example, you can combine the first name, last name, and gender fields to create a personalized salutation.

Basic Syntax of Liquid

Liquid templates are made up of tags, objects, and filters:
1. Tags: Tags are used to create logic or control flow in your templates
They are enclosed within `{% %}`. – Example: `{% if condition %}…{% endif %}`
2. Objects: Objects are used to display content and are enclosed within `{{ }}`. They represent the data you want to insert into the template.
– Example: `{{ FirstName }}`
3. Filters: Filters are used to modify the output of an object. They are applied using the pipe `|` character.
– Example: `{{ FirstName | capitalize }}`

Step-by-Step Guide to Creating Expressions

1. Combining Fields

Let’s say you have extracted the following fields from a Document:

– `FirstName = “John”`
– `LastName = “Doe”`
– `Gender = “Male”`

You can combine these fields to create a full name with a salutation using the following expression:

{% if Gender == “Male” %}
Mr. {{ FirstName }} {{ LastName }}
{% elsif Gender == “Female” %}
Ms. {{ FirstName }} {{ LastName }}
{% else %}
{{ FirstName }} {{ LastName }}
{% endif %}

This expression checks the gender and adds the appropriate salutation before the name.

2. Using Filters

Filters allow you to modify the extracted data. Here are some common filters:
capitalize: Capitalizes the first letter of a string.
– Example: `{{ FirstName | capitalize }}`
upcase: Converts a string to uppercase.
– Example: `{{ LastName | upcase }}`
downcase: Converts a string to lowercase.
– Example: `{{ LastName | downcase }}`
For instance, if you want to display the full name in uppercase, you can use:

{% if Gender == “Male” %}
MR. {{ FirstName | upcase }}
{{ LastName | upcase }}
{% elsif Gender == “Female” %}
MS. {{ FirstName | upcase
}} {{ LastName | upcase }}
{% else %}
{{ FirstName | upcase }}
{{ LastName | upcase }}
{% endif %}

3. Conditional Logic

Liquid supports conditional statements, allowing you to create expressions based on certain conditions. Here’s a simple example:

{% if Age > 18 %}
Adult
{% else %}
Minor
{% endif %}

This expression checks if the extracted `Age` field is greater than 18 and outputs “Adult” if true, otherwise “Minor”.

4. Iterating Over Collections

If your extracted data includes lists or arrays, you can loop over them using the `for` tag:

{% for item in items %}
{{ item }}
{% endfor %}

This will iterate over each item in the `items` array and output it.

Practical Examples

Example 1: Combining Name and Address
Suppose you have the following fields extracted:

– `FirstName = “Jane”`
– `LastName = “Smith”`
– `Street = “123 Main St”`
– `City = “Anytown”`
– `State = “CA”`
– `ZipCode = “12345”`

You can create an expression to combine these into a formatted address:

{{ FirstName }} {{ LastName }}
{{ Street }}
{{ City }}, {{ State }} {{ ZipCode }}

This will output:

Jane Smith
123 Main St
Anytown, CA 12345

Example 2: Conditional Output Based on Document Type

If your Document extraction identifies the type of document, you can use conditional logic to format the output accordingly:

{% if DocumentType == “Invoice” %}
Invoice Number: {{ InvoiceNumber }}
Date: {{ Date }}
Total: ${{ Total }}
{% elsif DocumentType == “Receipt” %}
Receipt Number: {{ ReceiptNumber }}
Date: {{ Date }} Amount Paid: ${{ AmountPaid }}
{% endif %}

Tips for New Users

Start simple: Begin with basic expressions and gradually introduce more complex logic as you become comfortable with Liquid.
Test frequently: Test your expressions with actual data to ensure they produce the desired output.
Use Liquid documentation:
Refer to the [ Liquid documentation ] for more details on tags, filters, and other features.
Category Function Name Description Sample Usage
Math abs Returns the absolute value of a number. {{ abs(-5) }} → 5
Math ceil Rounds a number up to the nearest integer. {{ ceil(4.3) }} → 5
Math floor Rounds a number down to the nearest integer. {{ floor(4.8) }} → 4
Math round Rounds a number to the nearest integer or specified precision. {{ round(4.56, 1) }} → 4.6
Math max Returns the maximum value from a list of numbers. {{ max(1, 2, 3) }} → 3
Math min Returns the minimum value from a list of numbers. {{ min(1, 2, 3) }} → 1
String lower Converts a string to lowercase. {{ lower("HELLO") }} → "hello"
String upper Converts a string to uppercase. {{ upper("hello") }} → "HELLO"
String replace Replaces all occurrences of a substring with another string. {{ replace("Hello world", "world", "Scriban") }} → "Hello Scriban"
String split Splits a string into an array based on a delimiter. {{ split("a,b,c", ",") }} → ["a", "b", "c"]
String substring Extracts a substring from a string. {{ substring("hello", 1, 3) }} → "ell"
Array size Returns the number of elements in an array. {{ size([1, 2, 3]) }} → 3
Array reverse Reverses the order of elements in an array. {{ reverse([1, 2, 3]) }} → [3, 2, 1]
Array sort Sorts an array in ascending order. {{ sort([3, 1, 2]) }} → [1, 2, 3]
Array first Returns the first element of an array. {{ first([1, 2, 3]) }} → 1
Array last Returns the last element of an array. {{ last([1, 2, 3]) }} → 3
Date date.now Returns the current date and time. {{ date.now }} → "2024-08-13T12:00:00Z"
Date date.format Formats a date according to a specified format. {{ date.format(date.now, "%Y-%m-%d") }} → "2024-08-13"
Date date.add_days Adds a number of days to a date. {{ date.add_days(date.now, 5) }} → "2024-08-18"
Date date.parse Parses a string into a date using a specified format. {{ date.parse("2024-08-13", "%Y-%m-%d") }} → date object
Control Flow if Executes a block of code if a condition is true. {% if condition %} ... {% endif %}
Control Flow for Iterates over each item in a collection or array. {% for item in collection %} ... {% endfor %}
Control Flow while Repeats a block of code while a condition is true. {% while condition %} ... {% endwhile %}
Utility typeof Returns the type of a value. {{ typeof(123) }} → "number"
Utility print Outputs a value to the console or output. {{ print("Hello World") }} → "Hello World"