Technologies
JSONata
Advanced functionality in Stubber
JSONata is a powerful and flexible query and transformation language specifically designed for JSON data. It provides a way to effortlessly filter, transform, and extract data from JSON structures, making it an invaluable tool for developers working with APIs, databases, or any system that uses JSON.
Key Features:
Querying JSON Data:
JSONata allows you to select specific parts of a JSON document using a simple and expressive syntax. For example, you can easily retrieve all items in a list that match certain criteria.
Transforming JSON Data:
Beyond just querying, JSONata enables you to transform JSON data. You can restructure the data, create new fields, or modify existing values as needed.
Filtering Data:
JSONata's filtering capabilities let you define complex conditions to include or exclude data based on various criteria. This is particularly useful for extracting relevant information from large JSON datasets.
Function Support:
JSONata includes a variety of built-in functions for performing common tasks like string manipulation, arithmetic operations, and date handling. You can also define your own custom functions to extend its capabilities.
Chaining and Composition:
JSONata expressions can be chained together to perform multiple operations in a single query. This makes it possible to create complex data transformations in a clear and concise manner.
Uses in Stubber
JSONata is used throughout Stubber to allow for more advanced functionality in the some areas:
- value replacement via the JSONata Hashtag Helper
- the savedata task to allow for more advanced data manipulation
- conditions arrays
- validations
Writing JSONata
The best way to learn JSONata is to use the JSONata Playground.
You can copy and paste the context data into the playground and then write your JSONata query in the expression box.
A common mistake with JSONata is to try to do dynamic path lookups eg. stub.data.people[stub.data.current_person].name
This is not possible with JSONata you need to use the $lookup
function to achieve this.
Eg. $lookup(stub.data.people, stub.data.current_person).name
Example: Converting a JSON String to JSON Structure
You should use the Parse JSON Hashtag Helper to achieve this. It is a more direct and easier way to convert a JSON string back to a JSON structure.
This is how you would use the JSONata Helper to convert a String containing JSON into JSON Structured data for use where this is required.
We will use the jsonata expression $eval() to achieve this.
Consider Data
Usage Example
Result
Try it in the JSONata Exerciser