JSON Handlers in DaFT

[DaFT] - [How] - [Install] - [Config] - [JSON & XML] - [Extend] - [Implement]

DaFT includes powerful JSON handlers that allow data extraction from various sources such as applications, web APIs, and local files. This guide explains the available JSON handlers and how optional search fields can be used to refine data selection.

JSON Handler Types

DaFT supports three types of JSON handlers:

1. App JSON (app-json)

2. Web JSON (web-json)

3. File JSON (file-json)


Using JSON Handlers

Each JSON handler requires configuration via config.ini. The following example demonstrates how to define each type:

[example_web_json]
source       = https://api.example.com/data.json
type         = web-json
identifier   = example-api
json_path    = data.items
fields       = id,name,score
[example_app_json]
source       = /usr/local/bin/my-json-script --option=value
type         = app-json
identifier   = example-app
json_path    = response.results
fields       = id,timestamp
[example_file_json]
source       = /var/log/data.json
type         = file-json
identifier   = example-file
json_path    = logs.entries
fields       = user,action,status

Understanding json_path

The json_path setting allows users to extract specific parts of a JSON response using dot notation.

Example API Response

{
  "status": "success",
  "data": {
    "items": [
      { "id": 1, "name": "Alice", "score": 95 },
      { "id": 2, "name": "Bob", "score": 88 }
    ]
  }
}

To extract only data.items, set:

json_path = data.items

The output would be:

[
  { "id": 1, "name": "Alice", "score": 95 },
  { "id": 2, "name": "Bob", "score": 88 }
]

Using fields to Filter Data

The fields setting lets you specify which fields should be extracted from the JSON structure.

Example Usage

Given the previous data.items structure, setting:

fields = id,name

Would produce:

[
  { "id": 1, "name": "Alice" },
  { "id": 2, "name": "Bob" }
]

If a specified field does not exist, it is simply ignored without causing errors.


Handling Single JSON Objects

If the extracted json_path points to a single JSON object, it will be returned as an object instead of an array.

Example JSON Response

{
  "metadata": {
    "info": {
      "version": "1.0",
      "author": "Admin"
    }
  }
}

Configuration

json_path = metadata.info

Output

{
  "version": "1.0",
  "author": "Admin"
}

What Happens If a Path Doesn’t Exist?

This ensures consistent behaviour and prevents failures due to missing data.


By utilising these JSON handlers, DaFT allows precise and flexible extraction of JSON data from multiple sources, making integration with external services efficient and reliable.