# Turn AI responses into a structured JSON output

This article extends the sentiment analysis API you assembled in the [previous quickstart](https://docs.digibee.com/documentation/resources/quickstarts/create-your-first-ai-agent). You can now transform unstructured answers into structured outputs, **ensuring your API always returns consistent, machine-readable results** ready to be used in deterministic integration flows.

<figure><img src="https://3750561495-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaD6wuPRxnEQEsYpePq36%2Fuploads%2F6ZQwRJNg6ZtAeR5b7hPC%2FQS%20%232%20(English).gif?alt=media&#x26;token=3d3f1c44-8d88-44db-be7f-76558e7473d7" alt=""><figcaption></figcaption></figure>

## **Prerequisites**

Before continuing, make sure you have completed the [Create your first AI Agent for sentiment analysis quickstart](https://docs.digibee.com/documentation/resources/quickstarts/create-your-first-ai-agent), including:

* Selecting an LLM provider
* Choosing a model
* Configuring the account

Once the agent is properly configured, you can proceed to define a JSON Schema for the response.

## **Configuring a JSON Schema**

A **JSON Schema** defines a predefined JSON structure with enforced fields and data types that the AI must follow in its response. See the configuration method below:

1. In the [**Agent Component**](https://app.gitbook.com/s/EKM2LD3uNAckQgy1OUyZ/connectors/ai-tools/llm), click the **gear icon** (⚙️) next to the **Model** parameter.
2. Enable **Use JSON Schema**.
3. Add the following JSON Schema:

{% code overflow="wrap" expandable="true" %}

```json
{
  "type": "object",
  "required": ["sentiment", "confidence"],
  "properties": {
    "sentiment": {
      "type": "string",
      "enum": ["positive", "negative", "neutral"]
    },
    "confidence": {
      "type": "number",
      "description": "Confidence score from 0 to 1"
    }
  }
}
```

{% endcode %}

In the previous quickstart, the output format is flexible. By introducing a JSON Schema here, you ensure the response is always returned in a **well-defined, structured format**.

## **Testing the Agent**

Use the following **System** and **User Messages** configured from the [**previous quickstart**](https://docs.digibee.com/documentation/resources/quickstarts/create-your-first-ai-agent):

**System message:**

{% code overflow="wrap" expandable="true" %}

```
You are a sentiment analyzer. Classify text as positive, negative, or neutral. Also provide a confidence score between 0 and 1 that reflects how certain you are about the sentiment classification.
```

{% endcode %}

**User message:**

{% code overflow="wrap" expandable="true" %}

```
This product is amazing! Best purchase ever.
```

{% endcode %}

### Before: Without JSON Schema

Without a JSON Schema, the Agent may return an unstructured response, for example:

{% code overflow="wrap" expandable="true" %}

```json

{
  "body": {
    "text": "The sentiment is clearly positive."
},
  "tokenUsage": {
    "inputTokenCount": 72,
    "outputTokenCount": 17,
    "totalTokenCount": 89
  }
}
```

{% endcode %}

The response format can change each time, which makes it difficult to process automatically.

### After: With JSON Schema

With a JSON Schema configured, the Agent always returns a well-defined and predictable structure:

{% code overflow="wrap" expandable="true" %}

```json
{
"body": {
"sentiment": "positive",
"confidence": 0.95
},
"tokenUsage": {
    "inputTokenCount": 88,
    "outputTokenCount": 12,
    "totalTokenCount": 100
  }
}
```

{% endcode %}

This ensures the output is always consistent and easy for other systems to read and use.

## **Result**

Congratulations! You now have a sentiment analysis API that **returns structured data** in a consistent format, ideal for routing logic or storing in databases.

## **Related topics**

* [**Use an MCP Server tool to connect AI agents to external systems**](https://docs.digibee.com/documentation/resources/quickstarts/connect-agents-to-external-systems): Use tools to retrieve external data through the Deepwiki MCP Server.
* [**Insurance claim analysis with AI**](https://docs.digibee.com/documentation/resources/ai-practical-examples/insurance-claim-analysis-with-ai)**:** Build a multi-agent system to help review insurance claims.
* [**AI expense report validation system**](https://docs.digibee.com/documentation/resources/ai-practical-examples/expense-report-validation-with-ai)**:** Explore a real-world implementation in this How-to guide.
* [**Agent Component — Complete configuration guide**](https://app.gitbook.com/s/EKM2LD3uNAckQgy1OUyZ/connectors/ai-tools/llm): Explore all the configuration options for this component.
