Skip to content

prettyJSON

reference
1 min readUpdated

Kind: Function

Source: src/middleware/pretty-json/index.ts

Part of: Middleware

Pretty JSON Middleware for Hono.

prettyJSON creates Hono middleware that formats JSON response bodies with indentation when the request asks for pretty output. Register it before route handlers so it can inspect downstream JSON responses and replace them with formatted JSON when appropriate.

Signature

ts
function prettyJSON(options: PrettyOptions): MiddlewareHandler

Parameters

NameType
optionsPrettyOptions

Returns: MiddlewareHandler

Diagram

mermaid
graph LR
  Request[Incoming request] --> Middleware[prettyJSON middleware]
  Middleware --> Route[Route handler]
  Route --> Response[JSON response]
  Response --> Middleware
  Middleware --> Formatted[Formatted JSON response]

Usage

ts
import { Hono } from 'hono'
import { prettyJSON } from 'hono/pretty-json'

const app = new Hono()

app.use('*', prettyJSON())

app.get('/users', (c) => {
  return c.json({
    users: [{ id: 'user-a', name: 'Ada' }],
  })
})

export default app

Request the route with ?pretty to receive formatted JSON.

AI Coding Instructions

  • Register prettyJSON() before routes whose JSON responses should support formatted output.
  • Return JSON through Hono response helpers such as c.json() so the response has a JSON content type.
  • Preserve the middleware order: prettyJSON must wrap downstream route handling to format the completed response.
  • Avoid applying assumptions about non-JSON response bodies; the middleware should only reformat valid JSON responses.

Was this page helpful?

Download as PDF
prettyJSON — Hono (narrator proof)