Laravel 4 JSON Output formatting

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering JSON Output Formatting in Laravel: From Compact to Pretty

As developers working with APIs, one of the most common friction points is the difference between functionally correct data transmission and aesthetically pleasing output. When you return JSON from a Laravel application, the default method often results in a single, compact line of text. However, many external APIs provide nicely formatted, human-readable JSON, which significantly improves debugging, testing, and client-side consumption.

This post will dive into how you can take your standard Laravel JSON output and format it beautifully, giving you complete control over the response structure. We'll move beyond the basic Response::json() call to achieve that polished result.

Understanding Default JSON Serialization

When you use Laravel’s built-in methods like return Response::json(...), the framework leverages PHP’s native functions (specifically json_encode()) to serialize your PHP array or object into a JSON string. By default, json_encode() produces the most compact representation possible, minimizing whitespace and line breaks to reduce payload size—which is excellent for performance in high-throughput systems.

Your example demonstrates this standard behavior:

return Response::json(array(
    'status' => 200,
    'posts' => $post->toArray()
), 200);
// Output (compact): {"status":200,"posts":[...]}

While this is technically valid and efficient, it lacks the readability you see in complex API documentation or third-party services.

The Solution: Pretty-Printing JSON with PHP

The key to achieving formatted JSON lies not in changing how Laravel sends the response, but in manipulating the data before it gets encoded into a string. Since Laravel relies on underlying PHP functionality, we can inject specific formatting flags directly into the encoding process.

To get human-readable JSON, you must use the JSON_PRETTY_PRINT flag within the json_encode() function. This flag instructs PHP to insert newlines and indentation between objects and array elements, making the resulting string easy for humans to read.

Here is how you apply this technique in a Laravel controller context:

use Illuminate\Http\Response;

class PostController extends Controller
{
    public function getFormattedPosts()
    {
        $data = [
            'status' => 200,
            'posts' => $this->post->toArray()
        ];

        // Use json_encode with the JSON_PRETTY_PRINT flag
        $formattedJson = json_encode($data, JSON_PRETTY_PRINT);

        return response($formattedJson, 200, [
            'Content-Type' => 'application/json'
        ]);
    }
}

Explanation of the Technique

In the example above, instead of passing the array directly to Response::json(), we manually encode the data:

  1. Prepare Data: We gather our results into a standard PHP array ($data).
  2. Encode with Formatting: We call json_encode($data, JSON_PRETTY_PRINT). This function takes our structured data and outputs it as a string with indentation.
  3. Return Response: Finally, we use the standard response() helper to send this newly formatted string back to the client, ensuring the correct Content-Type header is set.

This approach decouples the formatting logic from Laravel's default response mechanism, giving you granular control over the output structure—a hallmark of robust API design, much like the principles discussed on the Laravel documentation.

Best Practices for API Formatting

While JSON_PRETTY_PRINT is a great starting point, consider these best practices when dealing with JSON formatting:

  1. Client Context: If your primary consumers are other machines or services (machine-to-machine communication), stick to the compact format (json_encode($data) without flags) for maximum efficiency and minimal bandwidth usage.
  2. Human Readability: Use pretty printing only when serving data to human users (e.g., a web application interface, admin dashboards, or documentation endpoints).
  3. Custom Serializers: For highly complex applications, instead of manually formatting the output in every controller, consider creating custom JSON Serializers or using dedicated packages that handle view-like rendering before serialization.

By understanding how PHP's native functions interact with Laravel’s response layer, you gain the power to tailor your API responses exactly as your consumers need them, moving from simple data delivery to sophisticated presentation.