Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Code Generationbeginner

Interface from JSON

Share

Generate interfaces from JSON samples

Works with OpenClaude

You are a TypeScript developer who generates strongly-typed interfaces from JSON sample data.

What to check first

  • Examine the JSON structure to identify all properties, their types, and nesting levels
  • Check if any values are null or undefined — these indicate optional properties
  • Verify whether arrays contain objects (need to define element types) or primitives

Steps

  1. Parse the JSON sample and identify the root object structure
  2. For each top-level property, determine its type: string, number, boolean, object, or array
  3. For nested objects, create separate interfaces and reference them
  4. For arrays of objects, extract the object schema and mark the array type as ElementType[]
  5. Mark properties as optional using ?: if they appear null, undefined, or missing in the sample
  6. Use Record<string, unknown> for dynamic/unknown object keys
  7. Generate the complete interface hierarchy from innermost to outermost types
  8. Export the root interface for use in consuming code

Code

import * as fs from "fs";

interface JSONValue {
  [key: string]: unknown;
}

function inferType(value: unknown): string {
  if (value === null || value === undefined) return "unknown";
  if (Array.isArray(value)) {
    if (value.length === 0) return "unknown[]";
    const elementType = inferType(value[0]);
    return `${elementType}[]`;
  }
  if (typeof value === "object") return "object";
  return typeof value;
}

function generateInterface(
  json: JSONValue,
  interfaceName: string = "Root"
): string {
  const interfaces: string[] = [];
  const processedTypes = new Set<string>();

  function processObject(obj: JSONValue, name: string): string {
    if (processedTypes.has(name)) return name;
    processedTypes.add(name);

    const properties: string[] = [];

    for (const [key, value] of Object.entries(obj)) {
      let type = "unknown";

      if (Array.isArray(value) && value.length > 0 && typeof value[0] === "object" && value[0] !== null) {
        const elementInterface = `${name}${key.charAt(0).toUpperCase() + key.slice(1)}`;
        processObject(value[0] as JSONValue, elementInterface);
        type = `${elementInterface}[]`;
      } else if (typeof value === "object" && value !== null && !Array.isArray(value)) {
        const nestedInterface = `${name}${key.charAt(0).toUpperCase() + key.slice(1)}`;
        processObject(value as JSONValue, nestedInterface);
        type = nestedInterface;
      } else {
        type = inferType(value);
      }

      const isOptional = value === null || value === undefined ? "?" : "";

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
codegeninterfacesjson

Install command:

curl -o ~/.claude/skills/interface-from-json.md https://claude-skills-hub.vercel.app/skills/code-generation/interface-from-json.md

Related Code Generation Skills

Other Claude Code skills in the same category — free to download.

Want a Code Generation skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.