Back to blog
Guides · Retrieval

Best Way to Get Structured JSON From a Database With an AI Agent

Ask for a boolean, a number, a list or a JSON object. n8n and LangChain branch on the value. They do not parse a paragraph.

Claix · November 2026. Related guides: n8n, LangChain, and call an agent. Product: Claix Retrieve.

An agent workflow often needs a value, not a sentence. Is this item in stock? How many orders are pending? Which customers need follow-up? What is the status of this reservation? This guide shows how to get that structured JSON from the database with typed answers, n8n IF nodes and LangChain.

Why free text is a problem

Ask “Is this item in stock?” and a model may answer “Yes, there are 12 units available.” A person can read that. A workflow cannot rely on it. The wording changes, extra words appear, the value is ambiguous, the branch breaks, or the model invents a number. A typed field avoids that parse.

What structured JSON means

  • A boolean for “is this item in stock?”
  • A number for “how many orders are pending?”
  • A list for “which customers need follow-up?”
  • A JSON object for structured operational data.

Claix Retrieve returns those types so the next step branches on the value.

How Claix Retrieve works

Retrieve connects to a relational database and lets each agent read only the tables and columns you allow. It does not copy your data into a vector store. A route is that boundary: tables, columns and a guide with your vocabulary. An agent is the identity that uses one or more routes, with its own endpoint, prompt and response mode. A support bot and an internal assistant can share the database and see different columns.

You choose raw or reasoned on the agent in the dashboard. The request does not switch the mode.

How to call Claix Retrieve from n8n

POST https://claix.dev/retrieve/{agent_name} with header x-api-key. One call accepts up to five questions. Each question can include a format.

{
  "questions": [
    { "question": "Is this item in stock?", "format": "boolean" },
    { "question": "How many orders are pending?", "format": "int" },
    { "question": "Which customers need follow-up?", "format": "array:string" }
  ]
}

The response is user_ask, ia_response and log_id. ia_response is aligned with user_ask. In reasoned mode each slot is the typed value, or { error } if that question failed. In raw mode each slot is { rows, truncated }. Use log_id in Traces.

What formats you can request

In reasoned mode the formats are string, int, boolean, timestamp, timestamptz, date, json, array:string and array:int. The alias timestampz is accepted and treated as timestamptz. A plain array is also accepted.

How to use IF nodes in n8n

  • If the boolean is true, send one message. If it is false, send another.
  • If the number is over a threshold, take the exception path.
  • If the value is a list, loop over the items.
  • If the field is empty, the route did not contain the answer. Do not treat that as a guess. Ask a person or stop.

How to use LangChain

LangChain calls the same endpoint and the same body. Wrap it as a tool that returns ia_response, and let the graph branch on the typed value instead of on a generated sentence. The integration notes are on the LangChain page.

Why this is better than direct database access

A database login still leaves tables, columns, tenant rows, result size and writes for you to enforce. Retrieve validates each query before it runs and traces the call. Connect it with a read-only role.

How to control writes

A typed read is not a write. If the workflow then inserts, updates or deletes, call POST /check-sql first. Allowed means your code can run the statement. Denied means it should not, and the agent gets the reason. Needs approval waits for a person. Claix never executes the SQL. A denial is still HTTP 200. Read allowed. Docs: check SQL.

What Guard can enforce

ControlWhat it prevents
Operation typeUnwanted inserts, updates or deletes
Target tableChanges to tables outside the agent’s scope
Target columnsChanges to sensitive or protected fields
Record selectionUpdates or deletes that affect too many records
Value limitsDiscounts, quantities, amounts or statuses outside allowed ranges
State transitionsInvalid changes from one status to another
Rate limitsToo many changes in a short period
Human reviewHigh-risk actions that require approval
Audit trailMissing evidence of what was proposed and decided

How to get started

Claix supports any SQL database, including Postgres, MySQL and MariaDB, plus Supabase. Connect with a read-only role, define the routes, set the agent to reasoned mode, and call one endpoint. Guard supports PostgreSQL, MySQL and MariaDB.

Conclusion

Ask for a boolean, a number, a list or a JSON object. The workflow then branches on that value. Retrieve keeps the read inside the tables and columns you allowed, and traces the call. Do not parse the model’s paragraph to find the number.