Generate interfaces from JSON samples
✓Works with OpenClaudeYou 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
nullorundefined— these indicate optional properties - Verify whether arrays contain objects (need to define element types) or primitives
Steps
- Parse the JSON sample and identify the root object structure
- For each top-level property, determine its type:
string,number,boolean,object, orarray - For nested objects, create separate interfaces and reference them
- For arrays of objects, extract the object schema and mark the array type as
ElementType[] - Mark properties as optional using
?:if they appearnull,undefined, or missing in the sample - Use
Record<string, unknown>for dynamic/unknown object keys - Generate the complete interface hierarchy from innermost to outermost types
- 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
Related Code Generation Skills
Other Claude Code skills in the same category — free to download.
Type Generator
Generate TypeScript types from JSON/API responses
Enum Generator
Generate enums from constant values
Boilerplate Reducer
Generate boilerplate code patterns
Regex Builder
Build and test regular expressions
SQL Generator
Generate SQL queries from natural language
Mock Data Generator
Generate realistic mock data (Faker.js)
Type Guard Generator
Generate TypeScript type guards
Regex Lookahead
Write regex with lookaheads, lookbehinds, and named groups
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.