API Response Formatter

Format, beautify, and analyze JSON API responses with syntax highlighting, validation, and debugging tools for developers.

5 min read

API Response Formatter

A comprehensive tool for developers to format, validate, and analyze JSON API responses. Perfect for debugging, documentation, and API development workflows.

Features

🔧 JSON Processing

  • Pretty printing with customizable indentation
  • Syntax highlighting for better readability
  • JSON validation with detailed error reporting
  • Minification for production use
  • Path extraction for nested objects

🔍 Analysis Tools

  • Schema generation from JSON responses
  • Data type analysis and statistics
  • Size optimization suggestions
  • Performance metrics calculation
  • Diff comparison between responses

📋 Developer Utilities

  • Copy formatted JSON to clipboard
  • Export to multiple formats (CSV, XML, YAML)
  • Generate TypeScript interfaces from JSON
  • Create mock data based on schema
  • API documentation generation

Installation

Browser Extension

Available for Chrome, Firefox, and Edge:

Chrome Web Store: API Response Formatter
Firefox Add-ons: API Response Formatter

NPM Package

npm install @api-formatter/core

CLI Tool

npm install -g @api-formatter/cli

Usage

Web Interface

Simply paste your JSON response into the formatter at api-formatter.dev

Command Line

# Format a JSON file
api-format input.json --output formatted.json --indent 2
 
# Validate JSON from URL
api-format --url https://api.example.com/users --validate
 
# Generate TypeScript interfaces
api-format response.json --generate-types --output types.ts
 
# Compare two API responses
api-format --diff response1.json response2.json

JavaScript API

import { JSONFormatter, SchemaGenerator } from '@api-formatter/core';
 
const formatter = new JSONFormatter({
  indent: 2,
  sortKeys: true,
  removeEmpty: false
});
 
// Format JSON response
const apiResponse = `{"users":[{"id":1,"name":"John","email":"john@example.com"}]}`;
const formatted = formatter.format(apiResponse);
 
console.log(formatted);
// Output: Pretty-printed JSON with proper indentation
 
// Generate schema
const schemaGenerator = new SchemaGenerator();
const schema = schemaGenerator.generate(JSON.parse(apiResponse));
 
console.log(schema);
// Output: JSON Schema definition

React Component

import { JSONViewer, SchemaViewer } from '@api-formatter/react';
 
function APIDebugger({ response }) {
  return (
    <div>
      <h3>API Response</h3>
      <JSONViewer 
        data={response}
        theme="dark"
        expandLevel={2}
        showLineNumbers={true}
        enableClipboard={true}
      />
      
      <h3>Generated Schema</h3>
      <SchemaViewer 
        data={response}
        showTypes={true}
        generateExamples={true}
      />
    </div>
  );
}

Advanced Features

Schema Generation

Automatically generate JSON Schema from API responses:

const response = {
  "users": [
    {
      "id": 1,
      "name": "John Doe",
      "email": "john@example.com",
      "active": true,
      "profile": {
        "age": 30,
        "location": "New York"
      }
    }
  ]
};
 
const schema = schemaGenerator.generate(response);
// Generates comprehensive JSON Schema with types, required fields, etc.

TypeScript Interface Generation

Convert JSON responses to TypeScript interfaces:

// Generated from API response
interface User {
  id: number;
  name: string;
  email: string;
  active: boolean;
  profile: {
    age: number;
    location: string;
  };
}
 
interface APIResponse {
  users: User[];
}

Response Comparison

Compare two API responses to identify differences:

const differ = new ResponseDiffer();
const diff = differ.compare(response1, response2);
 
console.log(diff);
// Output: Detailed diff showing added, removed, and modified fields

Performance Analysis

Analyze API response performance and optimization opportunities:

const analyzer = new ResponseAnalyzer();
const analysis = analyzer.analyze(response);
 
console.log(analysis);
// Output: {
//   size: "2.3 KB",
//   fields: 15,
//   depth: 3,
//   suggestions: ["Remove empty fields", "Compress nested objects"]
// }

Configuration Options

Formatting Options

const formatter = new JSONFormatter({
  indent: 2,                    // Indentation spaces
  sortKeys: true,              // Sort object keys alphabetically
  removeEmpty: false,          // Remove empty objects/arrays
  maxDepth: 10,               // Maximum nesting depth
  theme: 'dark',              // Syntax highlighting theme
  showTypes: true,            // Show data types in comments
  compactArrays: false        // Keep arrays on single line if short
});

Validation Options

const validator = new JSONValidator({
  strict: true,               // Strict JSON parsing
  allowComments: false,       // Allow JavaScript-style comments
  allowTrailingCommas: false, // Allow trailing commas
  maxSize: '10MB',           // Maximum file size
  schema: customSchema       // Validate against custom schema
});

Browser Extension Features

Automatic Detection

  • Automatically detects JSON responses in browser
  • Formats responses in DevTools Network tab
  • Adds format button to API testing tools

Context Menu Integration

  • Right-click on any JSON text to format
  • Quick access to validation and analysis
  • Export options directly from context menu

Developer Tools Integration

  • Enhanced JSON viewing in DevTools
  • Performance metrics overlay
  • Schema generation in sidebar

CLI Commands

Basic Commands

# Format JSON file
api-format file.json
 
# Format from stdin
echo '{"name":"John"}' | api-format
 
# Validate JSON
api-format --validate response.json
 
# Minify JSON
api-format --minify large-response.json

Advanced Commands

# Generate TypeScript types
api-format --types response.json --output types.ts
 
# Compare responses
api-format --diff old.json new.json --format html
 
# Analyze performance
api-format --analyze response.json --report
 
# Convert to other formats
api-format --convert yaml response.json

Integration Examples

Express.js Middleware

const { formatMiddleware } = require('@api-formatter/express');
 
app.use('/api', formatMiddleware({
  development: true,
  prettyPrint: true
}));

Webpack Plugin

const APIFormatterPlugin = require('@api-formatter/webpack');
 
module.exports = {
  plugins: [
    new APIFormatterPlugin({
      formatResponses: true,
      generateSchemas: true
    })
  ]
};

Browser Support

  • Chrome 70+
  • Firefox 65+
  • Safari 13+
  • Edge 79+

Contributing

We welcome contributions! Please see our Contributing Guide for details.

License

MIT License - see LICENSE for details.

Related Tools