Skip to main content

JSON Parsing (from process or URL)

Advanced JSON Handling in Web & App Handlers

The web-json and app-json handlers in DaFT allow for powerful JSON extraction and filtering, enabling users to:

  • Extract specific paths from deeply nested JSON.
  • Select only certain fields from the extracted data.
  • Maintain full backward compatibility (default behavior is unchanged).

Configurable JSON Extraction Options

Key Description Example Value
source URL (for web-json) or command (for app-json) https://api.example.com/data.json
type The handler type (web-json or app-json) web-json
json_path (Optional) Extract a specific JSON path data.items
fields (Optional) Select specific fields from results id,name,score

How JSON Path Extraction Works

The json_path option lets you specify a dot-separated path within a JSON response.

For example, given this API response:

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

You can extract only data.items using this configuration:

source = "https://api.example.com/data.json"
type = "web-json"
json_path = "data.items"

Returned Output:

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


Field Filtering: Selecting Only the Data You Need

The fields option allows you to select only certain fields from the extracted data.

For example, with this configuration:

source = "https://api.example.com/data.json"
type = "web-json"
json_path = "data.items"
fields = "id,name"

Returned Output:

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


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

source = "https://api.example.com/data.json"
type = "web-json"
json_path = "metadata.info"

Returned Output:

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


Using App JSON Handler

The app-json handler works exactly the same, but instead of fetching JSON from a web URL, it runs a shell command that outputs JSON.

Example Configuration

source = "/usr/local/bin/my-json-app --option=value"
type = "app-json"
json_path = "response.results"
fields = "id,score"

Expected Output:

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


What Happens If a Path Doesn’t Exist?

If the specified json_path does not exist in the response:

  • Instead of returning an error, it will return an empty array ([]).

If the specified fields don’t exist in the extracted data:

  • Those fields will be ignored without errors.

This ensures consistent behavior and prevents API failures due to missing keys.