Response Processing

This document explains how Sling processes API responses, including format handling, record extraction, and data transformations.

Response Flow Overview

Response Formats

Sling can automatically handle multiple response formats based on the API's Content-Type header or explicit configuration.

Automatic Format Detection

By default, Sling detects the format from the Content-Type response header:

Content-Type Header
Format Detected
Processing

application/json

JSON

Direct JSON parsing

application/xml or text/xml

XML

Converted to JSON structure

text/csv

CSV

Converted to JSON records

Others

JSON (default)

Attempts JSON parsing

Explicit Format Configuration

You can override automatic detection by specifying the format explicitly:

response:
  format: json  # Force format interpretation
  records:
    jmespath: "data[]"

Supported format values:

  • json - Standard JSON response

  • csv - Comma-separated values

  • xml - XML response

  • jsonl or jsonlines - JSON Lines (one JSON object per line)

Format-Specific Processing

JSON Responses

The most common API response format. Sling parses JSON and extracts records using JMESPath:

Example JSON response:

CSV Responses

CSV responses are automatically converted to JSON records:

CSV Processing Rules:

  1. First row is treated as the header row (column names)

  2. Subsequent rows become records

  3. Minimum 2 rows required (header + at least one data row)

  4. Each row is converted to a JSON object with header names as keys

Example CSV response:

Becomes:

📝 Note: CSV values are always strings. Use processors to convert them to other types if needed.

XML Responses

XML responses are automatically converted to JSON before record extraction:

Example XML response:

Becomes JSON:

⚠️ Warning: XML to JSON conversion follows standard rules: attributes become fields with @ prefix, text content becomes #text field.

JSON Lines (JSONL)

For streaming JSON responses where each line is a complete JSON object:

Example JSONL response:

Record Extraction

After format conversion, records are extracted using JMESPath expressions.

Basic Extraction

Nested Extraction

Conditional Extraction

Projection and Transformation

Deduplication

When primary_key is defined, Sling automatically deduplicates records:

Deduplication Strategies

1. In-Memory Deduplication (Default)

For datasets with reasonable record counts:

Characteristics:

  • Fast and accurate

  • Memory usage grows with unique record count

  • Suitable for datasets up to ~1 million records

2. Bloom Filter Deduplication

For very large datasets where memory is constrained:

Characteristics:

  • Probabilistic deduplication (small false positive rate)

  • Fixed memory footprint

  • Suitable for datasets with millions of records

Format: "capacity,error_rate"

  • capacity: Expected number of unique records

  • error_rate: Acceptable false positive rate (e.g., 0.001 = 0.1%)

💡 Tip: Use Bloom filter for datasets over 1 million records or when memory is limited. The error rate determines memory usage - lower rates use more memory.

Response State

All response data is accessible in the response state variable for use in expressions:

Response Property
Description
Example Usage

response.status

HTTP status code

response.status == 200

response.headers

Response headers

response.headers.link

response.text

Raw response body

length(response.text) > 0

response.json

Parsed JSON response

response.json.has_more

response.records

Extracted records array

length(response.records)

Using Response State in Pagination

Using Response State in Rules

Using Response State in Processors

Conditional Processing with IF Conditions

Processors support an optional if field to conditionally execute based on runtime conditions.

Basic Syntax

How It Works

  • Evaluation: The if condition is evaluated before the expression

  • Skip on False: If false, the entire processor is skipped for that record

  • Access: Has access to record, state, response, env, secrets

Common Patterns

💡 Tip: Always check for null before accessing field properties to avoid errors.

⚠️ Warning: IF conditions are evaluated for every record. Avoid expensive operations.

Overwriting Records with output: "record"

Setting output: "record" completely replaces the entire record with the result of the expression. All existing fields are discarded unless explicitly included.

Common Use Cases

1. Select Specific Fields

Keep only essential fields from large API responses:

2. Rename Fields

Transform field names to match your schema:

3. Flatten Nested Data

Convert nested structures into flat records using JMESPath:

4. Add Computed Fields

Create records with derived values:

Important Warnings

⚠️ All previous fields are discarded - Must explicitly include every field you want to keep

⚠️ Order matters - If you overwrite the record, then add fields afterward:

⚠️ Include primary keys - For deduplication to work, primary key fields must be in the new record

💡 Tip: Use JMESPath projection syntax for cleaner nested data transformations.

Error Handling

Invalid Response Format

When Sling cannot parse the response in the expected format:

Empty or Missing Records

Handle cases where no records are found:

Partial Responses

Some APIs return partial data on errors:

Complete Example

Here's a comprehensive example showing all response processing features:

Best Practices

1. Always Define Primary Keys

Even if the API doesn't explicitly require deduplication, defining primary keys helps ensure data quality:

2. Use Appropriate Deduplication

Choose the right strategy based on your dataset size:

3. Handle Multiple Content Types

If your API might return different formats:

4. Validate Records Structure

Use processors to validate critical fields:

5. Log Response Details for Debugging

During development, use processors to log response information:

Troubleshooting

No Records Extracted

If you're not getting any records:

  1. Check your JMESPath expression:

  1. Look at the raw response in trace output

  2. Verify the path to your records array

  3. Test JMESPath expressions using online tools

CSV Parsing Errors

Common CSV issues:

Deduplication Not Working

Verify your primary key fields exist:

💡 Tip: Use --trace flag to see detailed response processing including format detection, record extraction, and deduplication results.

Last updated

Was this helpful?