Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Start by opening a new browser window and navigating to https://try.jsonata.org. The page is broken down into 3 main sections. The left-side panel contains the JSON being inspected. The right top panel is the JSONata expression. The right bottom panel is the JSON output, based on the expression and JSON provided.

Basic Usage

The default page setup includes some JSON data to get started, and a sample expression as well.

...

Product

Price

Quantity

Bowler Hat

34.45

2

Trilby Hat

21.67

1

Bowler Hat

34.45

4

Cloak

107.99

1

Deconstruct expressions to see their behaviors

It can be helpful to deconstruct expressions to better understand how they work.

  1. In the example above, remove “$sum(“ from the start of the expression, and remove the last “)” from the end of it. You will notice the output changes significantly from a single value to an array of values.

    image-20240328-174637.pngImage Removed

    .

    image-20240328-175041.pngImage Added
  2. We now see expressed output for each product element.

  3. To understand specifically which items are being considered in the mathematical formula, remove that part of the expression. Remove “.(Price * Quantity)” from the expression and observe the output.

  4. It should now be apparent which elements the (Price * Quantity) operation is working against, and further, the values being calculated by the original $sum operation.

Square 9 Sample Setup

The default example provided is great for exploring basic functionality, but it’s not consistent with the JSON format provided by FTE. Let try working with sample output that matches the FTE response syntax.

  1. Clear the expression in the top right panel.

  2. Clear the JSON in the left panel.

Info

To clear the panels, simply select all (Ctrl+A) and hit the delete key.

Browse to the expression exercise sample data, and copy / paste it into the JSONata Exerciser’s JSON input panel (left side panel). You can now write expressions that should help you better understand Square 9’s specific implementation of this technology.

Parsing Headers Versus Tables

Data is returned in two discrete sections of a singular JSON response. Header / Footer data (also known as Key / Value pairs) is returned in an element aptly name “header_footer”. This section represents and array of identified keys and values on a page. When you compare the data in this section to the source image, you can quickly identify matching keys and values on the source image to better understand what is found and where it exists.

  • In the JSONata expression panel (top right), type the text “header_footer”. You will immediately notice the JSON output changes, and you now see an array of objects that is reflective of the header_footer section alone.

Tables are more deeply nested due to their complex nature, but targeting the tables section is no different.

  • In the JSONata expression panel (top right), type the text “tables”. You will notice the JSON output changes, this time showing you only an array of table objects.

  • Tables are more complex in that a single page may contain multiple tables. Each table is assigned its own index, starting with zero.

You can target a specific table in a number of ways:

  1. Specify the index of the table in square brackets: tables[0]
    Your output JSON will present only the first table.

  2. Specify the tab_index value of the table: tables[table.tab_index=0]
    The tables object in the response is an array of table objects. Each table object has an element for tab_index, that corresponds to it’s numerical ID. The list of tables and associated id’s always starts at zero.

  3. Target a specific table object by altering the expression: tables.table[tab_index=0].
    With

...

  1. this expression, you again get only one table, but pay careful attention to the JSON output. The way this example is expressed will result in slightly different output. It’s important to understand completely how your expressed output is formed, as it will impact how you write expressions to map any data points.

Find A Specific Header Value

Write an expression that will return the Due Date.

  1. Filter by header/footer values with the expression: header_footer

  2. Further filter the result by expressing only the key Due Date: header_footer[key='Due Date']

  3. Further filter to get the value: header_footer[key='Due Date'].value

To summarize, the complete expressions finds the header_footer section, filters for a key of Due Date, and outputs the value of that key.

It can be useful to “generalize” your key targets with partial matches and case sensitivity. This is handled with regular expressions. If for example you wanted your lookup to be case insensitive, you would provide a case insensitive pattern. Regex is expressed as key ~> pattern.

  • header_footer[key~>/due date/i].value would match “Due Date”, “due date”, or “dUe dAtE”.