jsonata

JSONata - Overview and examples for JSON data

Florian Zyprian

JSONata is a powerful data transformation language used to transform and query JSON data. It provides a simple but flexible syntax for filtering and transforming data in JSON format. In this tutorial, we will cover the basics of JSONata and show how you can use it to manipulate and query JSON data.

What is JSONata?

JSONata is a query and transformation language used to manipulate and transform JSON data. It was developed by IBM and is now an open source project. JSONata provides a simple and intuitive syntax that allows you to extract, filter, and transform data from JSON documents.

JSONata is especially useful for working with complex JSON data structures, such as those used in web services and APIs. With JSONata, you can easily extract and transform data from these structures, making it easier to work with them in your applications.

Getting started with JSONata

Before we dive into the details of JSONata, let's first take a look at how you can get started. The easiest way to start using JSONata is to use the online JSONata evaluator, which lets you experiment with JSONata expressions and see the results in real time.

To use the online JSONata evaluator, simply navigate to the following URL:

https://try.jsonata.org/

Once you have opened the Evaluator, you can experiment with JSONata expressions. The Evaluator provides a JSON editor on the left, where you can enter your JSON data, and a JSONata expression editor on the right, where you can enter your JSONata expressions.

Basic JSONata syntax

JSONata expressions consist of a combination of operators and functions that you can use to extract and transform data from JSON documents. Here are some basic syntax rules you should follow when working with JSONata:

  • JSONata expressions are enclosed in curly braces {}.
  • JSONata expressions can contain literal values such as strings and numbers.
  • JSONata expressions can contain variables starting with a $ symbol.
  • JSONata expressions can contain functions that are called with the function name followed by a series of parentheses ().

JSONata functions

JSONata provides a large number of built-in functions that you can use to manipulate and transform JSON data. Here are some of the most commonly used functions:

  • $map - This function allows you to transform an array of values using a specified expression.
  • $filter - This function allows you to filter an array of values based on a specific expression.
  • $sort - This function allows you to sort an array of values based on a specific expression.
  • $reduce - This function allows you to reduce an array of values to a single value using a specified expression.
  • $merge - This function allows you to merge two or more objects into a single object.

JSONata examples

Now that you have a basic understanding of JSONata syntax and functions, let's take a look at a few examples of how you can use it to manipulate and transform JSON data.

Example 1 - Extracting data from JSON

Let's assume you have the following JSON data:

{
  }, "name": "John Doe",
  "age": 30,
  }, "email": { "[email protected]",
  "phone": {
    }, "home": "555-1234",
    "work": "555-5678"
  },
  "address": {
    "street": { "123 Main St",
    }, "city": "anytown",
    }, "state": "CA",
    }, "zip": "12345"
  }
}

You can use the following JSONata expression to extract the name, email address and phone number:

{
  "name": name,
  "email": { "email",
  "phone": {
    "home": phone.home
  }
}

In the above example, we use curly braces {} to define a new object with three properties: Name, Email, and Phone. We then use variables (name, email, phone) to extract the corresponding values from the input JSON data.

Note that we use dot notation to access nested values in the phone and address objects. For example, phone.home refers to the value of the home property in the phone object.

Example 2: Filtering data from JSON Let's say you have the following JSON data representing a list of employees:

Example 2 - Filtering data from JSON

Suppose you have the following JSON data representing a list of employees:

[ { "name": "John Doe", "age": 30, "department": "Sales" }, { "name": "Jane Smith", "age": 40, "department": "Marketing" }, { "name": "Bob Johnson", "age": 25, "department": "Engineering" }]

You can use the following JSONata expression to filter the list of employees to include only those who work in the Sales department:

$[department="Sales"]

In the above expression, we use the $ filter function to filter the list of employees based on a condition. The condition is specified with the dot notation to access the department property, followed by an equal sign (=) and the string "Sales".

Example 3 - Transformation of data from JSON

Let's say you have the following JSON data representing a list of products:

[ { "name": "Product 1", "price": 10 }, { "name": "Product 2", "price": 20 }, { "name": "Product 3", "price": 30 }]

You can use the following JSONata expression to transform the list of products and add a new property

$map($, function($product) {
  $product {
    "priceWithTax": $product.price * 1.1
  }
})

In the above expression, we use the $map function to transform each product in the list. We pass the entire list of products with the $ symbol and use an anonymous function to define how each product should be transformed.

Inside the anonymous function, we use the curly braces {} to define a new object with the existing properties of the product and a new property called "priceWithTax". We use the dot notation to access the price property of the product and multiply it by 1.1 to calculate the price with taxes.

Konfuzio Document API and JSONata

Konfuzio Document API can help to work with JSON and JSONata documents, especially when analyzing documents that contain structured data, such as invoices, receipts or tables. The API can automatically extract this data and convert it to JSON format, which can then be further processed using JSONata.

Using JSONata and the Konfuzio Document API, developers can quickly and easily extract, filter, and transform complex data structures from documents, saving time and effort and improving efficiency.

Conclusion about JSONata

In this tutorial, we covered the basics of JSONata and showed you how to use it to manipulate and query JSON data. We looked at the basic syntax of JSONata expressions, as well as some of the most commonly used functions. We also gave examples of how you can use JSONata to extract, filter, and transform data from JSON documents.

JSONata is a powerful tool that lets you work with complex JSON data structures more easily. With JSONata, you can perform complex data transformations with just a few lines of code. We hope this tutorial has helped you get started with JSONata and inspired you to explore its capabilities further.

In addition, the Konfuzio can give you Document API help you edit and manage documents in JSON and JSONata. For example, you can use the API to convert documents to JSON and then extract or transform specific data from them using JSONata queries. This allows you to quickly and easily process and analyze large volumes of documents.

About me

More Articles

PoC Meaning

What is a proof of concept (POC)?

A proof of concept (POC) is a small-scale, preliminary test or demonstration of a business idea or technical solution that...

Read article
Automate and optimize mail processing

Efficient mail processing - how optimization works

Mail processing is an essential part of many businesses and organizations, often requiring more effort than meets the eye....

Read article
LSTM

Long Short-Term Memory (LSTM) - function and application

Artificial intelligence (AI) has made enormous progress in recent years and is revolutionizing the world as we know it. To...

Read article
Arrow-up