ACTIONS
  • Home
  • My Actions
  • My Projects
  • My Packages
Optimize AI Input
A utility designed to clean and format JSON data, making it ideal for use in prompts for Large Language Models (LLMs) like those from OpenAI, Google AI, Anthropic, and others.

Action summary

Optimize AI Input

Xano Contributors

A utility designed to clean and format JSON data, making it ideal for use in prompts for Large Language Models (LLMs) like those from OpenAI, Google AI, Anthropic, and others.

It processes a JSON object, intelligently removes keys that have empty or null values, and then returns a clean, human-readable, YAML-like string. This process helps reduce the token count sent to the AI service (saving costs) and improves the clarity of the prompt, often leading to better AI-generated responses.

Example Use Cases:

  • Agent and MCP tool outputs
  • Agent Inputs (Prompts and System Prompts)

Inputs

This action accepts two inputs:

Name Type Required Description
payload json Yes The JSON object you want to process and clean.
strip_empty_keys bool Yes A boolean flag to control the cleaning process. If true, keys with empty values will be removed. If false, the payload will be formatted without removing any keys.

Functionality Breakdown

The action follows a clear, step-by-step process to transform the input payload:

  1. Pre-check: First, the action ensures the provided payload is not null to prevent execution errors.
  2. Initialization: It creates a new, empty object (cleaned_payload) to store the filtered results. This is a best practice that avoids modifying the original input object.
  3. Iteration: The action then iterates through each key-value pair (entry) of the input payload.
  4. Empty Value Check: For each entry, it performs a comprehensive check to determine if the value (entry.value) is "empty." This is accomplished using the following Xano expression:
    ($entry.value == null) || (($entry.value|is_text) && ($entry.value|strlen) == 0) || (($entry.value|is_array) && ($entry.value|count) == 0) || (($entry.value|is_object) && ($entry.value|count) == 0)
    
    This expression identifies a value as empty if it meets any of these conditions:
    • The value is null.
    • The value is a text string with a length of zero (e.g., "").
    • The value is an array with zero items (e.g., []).
    • The value is an object with zero key-value pairs (e.g., {}).
  5. Conditional Filtering:
    • If a value is NOT empty (the expression in step 4 evaluates to false), the original key-value pair is added to the cleaned_payload object.
    • If a value IS empty (the expression evaluates to true), it is skipped and not included in the final result.
  6. Final Output Formatting: After the loop has processed all items, the action encodes the cleaned_payload object into a formatted string that approximates the YAML format (e.g., key: value). This string is the final, optimized output.

Example Usage

Example 1: Stripping Empty Keys (Standard Use)

This example demonstrates the primary use case where you want to clean up the data.

Input:

{
  "payload": {
    "name": "Daniel",
    "email": "daniel@example.com",
    "bio": "",
    "last_login": null,
    "preferences": {},
    "tags": []
  },
  "strip_empty_keys": true
}

Output:

name: Daniel
email: daniel@example.com

Explanation: The bio, last_login, preferences, and tags keys were all removed because their values met the criteria for being "empty" as defined in step 4 of the Functionality Breakdown.


Example 2: Retaining All Keys

This example shows what happens if you set strip_empty_keys to false. The action will simply format the original payload without removing anything. (Note: This assumes a conditional branch in your function stack that bypasses the stripping logic based on the boolean input).

Input:

{
  "payload": {
    "name": "Daniel",
    "status": null
  },
  "strip_empty_keys": false
}

Output:

name: Daniel
status: null

Explanation: Because strip_empty_keys was false, the cleaning logic was bypassed, and all key-value pairs from the original payload were included in the formatted output string.


Version notes

Initial version
Current
2025-08-04T01:13:34.000+00:00