← Back to blog
ai

Transform OpenAI API Response into Clean JSON

Transform an OpenAI API response into clean production JSON by extracting message content, token usage, model, and finish status.

2026-06-018 min read

Transforming an OpenAI API response means extracting the model output and usage fields your application actually needs, then removing the transport wrapper around them.

OpenAI responses are useful, but they are not usually the shape you want to store or send downstream. The message content lives inside choices[0].message.content, token usage is nested under usage, and fields like object, created, and system_fingerprint are often noisy for product workflows.

Use this OpenAI response cleaner when you need a stable JSON shape for logs, analytics, support tools, workflow automation, or a second processing step.

Need multiple transformations? Try ForgeJSON Pipeline →

Transform OpenAI API response (live tool)

{
"id": "chatcmpl_9x7openai1049",
"object": "chat.completion",
"created": 1764508800,
"model": "gpt-4.1-mini",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "{\"status\":\"paid\",\"summary\":\"Customer paid invoice INV-1049.\",\"next_action\":\"send_receipt\"}"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 817,
"completion_tokens": 42,
"total_tokens": 859
},
"system_fingerprint": "fp_demo_2026"
}
Output
1{
2 "model": "gpt-4.1-mini",
3 "response_id": "chatcmpl_9x7openai1049",
4 "message_text": "{\"status\":\"paid\",\"summary\":\"Customer paid invoice INV-1049.\",\"next_action\":\"send_receipt\"}",
5 "finish_reason": "stop",
6 "prompt_tokens": 817,
7 "completion_tokens": 42,
8 "total_tokens": 859
9}

Love the result?

Use this exact pipeline in your app, backend, or LLM workflow.

No setup needed. Works with curl, Node, Python.

Uses example data. For edited input, copy from the playground.

Read integration guide

Result: clean OpenAI response JSON

The example pipeline turns a nested OpenAI chat completion response into a smaller production-ready object.

Input:

{
  "id": "chatcmpl_9x7openai1049",
  "model": "gpt-4.1-mini",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "{\"status\":\"paid\",\"summary\":\"Customer paid invoice INV-1049.\",\"next_action\":\"send_receipt\"}"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 817,
    "completion_tokens": 42,
    "total_tokens": 859
  }
}

Output:

{
  "model": "gpt-4.1-mini",
  "response_id": "chatcmpl_9x7openai1049",
  "message_text": "{\"status\":\"paid\",\"summary\":\"Customer paid invoice INV-1049.\",\"next_action\":\"send_receipt\"}",
  "finish_reason": "stop",
  "prompt_tokens": 817,
  "completion_tokens": 42,
  "total_tokens": 859
}

Open the matching OpenAI Response Cleaner template if you want to start from this workflow directly.

Why OpenAI responses need cleanup

An OpenAI API response is designed to describe the API transaction, not just the useful answer. That is why a single response can include:

  • request identifiers
  • model names
  • one or more choices
  • assistant messages
  • finish reasons
  • token usage
  • system fingerprints
  • timestamps

That metadata can be valuable, but most downstream systems need a smaller contract. A product database, analytics table, support log, or webhook step usually wants the answer text, the model, and the usage numbers.

Pipeline steps

This workflow uses two transformation steps:

  1. Extract Response Fields: creates top-level fields for response_id, message_text, finish_reason, and token usage.
  2. Keep Production Fields: removes the wrapper fields and keeps only the clean response object.

The result is easier to validate, diff, store, and review.

When to use this workflow

Use this workflow when you need to:

  • extract content from an OpenAI response
  • log token usage without storing the whole payload
  • compare model outputs across runs
  • send the answer into another automation step
  • normalize OpenAI responses before analytics

If you are cleaning unpredictable model-generated JSON rather than the OpenAI API envelope, use the related LLM cleanup workflow next.

Why use a visual pipeline instead of a script?

A small script works when the shape never changes. A visual pipeline is better when you want the transformation to be inspectable, reusable, and safe for repeated API payloads.

ForgeJSON Pipeline lets you keep the cleanup steps visible:

  • what gets extracted
  • what gets removed
  • what the final JSON looks like
  • which fields downstream systems can rely on

That matters when AI responses are part of product workflows, not just one-off experiments.

FAQ

How do I extract content from an OpenAI API response?

The answer text is usually inside choices[0].message.content. This workflow extracts that content into message_text and keeps it next to the model, finish reason, and token usage.

Can I clean OpenAI responses without writing code?

Yes. Paste an OpenAI response into the pipeline, run the extraction steps, and copy the clean JSON output.

Does this parse JSON inside the message content?

This template extracts the message content as text. If the content itself contains JSON, you can add a follow-up cleanup or parsing step in ForgeJSON Pipeline.

What should I store from an OpenAI response?

For many product workflows, store the response id, model, message text, finish reason, and token counts. Keep the full raw response only when you need audit-level debugging.

Support material

Practical example and product context

Use these examples to understand the transformation and apply the same workflow in your own JSON tasks.

Before & After

Example Transformation

See how this workflow reshapes the sample material into clean output.

Input / Output
Input
{
"id": "chatcmpl_9x7openai1049",
"object": "chat.completion",
"created": 1764508800,
"model": "gpt-4.1-mini",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "{\"status\":\"paid\",\"summary\":\"Customer paid invoice INV-1049.\",\"next_action\":\"send_receipt\"}"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 817,
"completion_tokens": 42,
"total_tokens": 859
},
"system_fingerprint": "fp_demo_2026"
}
Output
{
"model": "gpt-4.1-mini",
"response_id": "chatcmpl_9x7openai1049",
"message_text": "{\"status\":\"paid\",\"summary\":\"Customer paid invoice INV-1049.\",\"next_action\":\"send_receipt\"}",
"finish_reason": "stop",
"prompt_tokens": 817,
"completion_tokens": 42,
"total_tokens": 859
}
Why this output is ready to use
  • The example keeps the fields application code usually needs from an OpenAI response.
  • The pipeline removes transport metadata such as object, created, choices wrapper details, and system fingerprint.
  • The final JSON can be stored, logged, analyzed, or passed into another API step.
Built with ai pipeline
Open the sample input and generated pipeline in the editor.

Related Articles

Continue with another practical guide in the same workflow area.