npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

scl339-json-to-types

v1.0.0

Published

Convert JSON to TypeScript interfaces/types. CLI tool that generates accurate TS types from JSON — supports nested objects, arrays, optional fields, and .d.ts export.

Readme

JSON to TypeScript Types

npm version npm downloads License: MIT Node.js Version GitHub stars

Convert JSON to TypeScript interfaces/types instantly. A zero-config CLI tool that generates accurate TypeScript type definitions from JSON — supports nested objects, arrays, union types, optional field detection, and .d.ts export.

Stop writing TypeScript types by hand from JSON API responses, config files, or mock data. json-to-types does it in one command.

Features

  • One command — pipe JSON in, get TypeScript out
  • 📦 Nested objects — auto-generates interface names for sub-objects
  • 🔄 Arrays — detects homogeneous arrays, deduplicates object shapes
  • 🎯 Optional fields — auto-detects nullable fields and marks them as optional (?)
  • 📝 .d.ts export — output ready-to-use declaration files
  • 🏷️ Custom root name — control the top-level type name
  • 🧹 No export mode — for inline type definitions
  • Zero config — works out of the box, no TypeScript project required

Installation

# Global install (recommended)
npm install -g json-to-types

# Or run via npx
npx json-to-types data.json

# Or use as a dependency
npm install --save-dev json-to-types

Usage

CLI

# From a JSON file
json-to-types data.json

# From stdin with custom root type name
cat data.json | json-to-types --name ApiResponse

# Output to file
json-to-types data.json --output types.ts

# Generate .d.ts declaration file
json-to-types data.json --dts --output types.d.ts

# Disable optional field detection
json-to-types data.json --no-optional

# Omit export keyword
json-to-types data.json --no-export

# Custom indentation
json-to-types data.json --indent 4

Options

| Option | Alias | Default | Description | |--------|-------|---------|-------------| | --name | -n | RootObject | Root type name | | --output | -o | stdout | Output file path | | --dts | -d | false | Output as .d.ts | | --no-optional | | true | Disable optional field detection | | --no-export | | true | Omit export keyword | | --indent | | 2 | Spaces per indent level |

API (Programmatic Usage)

import { convert, convertString } from 'json-to-types';

// From a parsed object
const json = { name: 'John', age: 30, active: true };
const types = convert(json, { rootName: 'User' });
console.log(types);

// From a JSON string
const types2 = convertString('{"id":1,"title":"Hello"}', { rootName: 'Post' });

Examples

Basic Object

Input:

{ "name": "Alice", "age": 25, "active": true }

Output:

export interface RootObject {
  name: string;
  age: number;
  active: boolean;
}

Nested Object

Input:

{
  "user": {
    "profile": {
      "bio": "Developer",
      "followers": 1200
    }
  }
}

Output:

export interface RootObjectSub1 {
  bio: string;
  followers: number;
}

export interface RootObjectSub2 {
  profile: RootObjectSub1;
}

export interface RootObject {
  user: RootObjectSub2;
}

Arrays of Objects

Input:

{
  "users": [
    { "id": 1, "name": "Alice" },
    { "id": 2, "name": "Bob" }
  ]
}

Output:

export interface RootObjectSub1 {
  id: number;
  name: string;
}

export interface RootObject {
  users: RootObjectSub1[];
}

Optional Fields (nullable values)

Input:

{ "name": "Test", "description": null }

Output:

export interface RootObject {
  name: string;
  description?: null;
}

Use Cases

  • API response types — get TypeScript types from any REST API JSON response
  • Configuration files — generate types for JSON configs (.eslintrc, tsconfig, etc.)
  • Mock data — turn mock JSON objects into proper TypeScript interfaces
  • Legacy migration — quickly type existing JSON blobs in a TypeScript codebase
  • Documentation — generate type stubs for API documentation

Why json-to-types?

| Feature | json-to-types | Other tools | |---------|--------------|-------------| | Nested type names | ✅ Automatic | ❌ Often missing | | Array item dedup | ✅ Structural hash | ❌ Duplicate types | | Optional detection | ✅ Auto from null | ❌ Manual only | | .d.ts output | ✅ Built-in | ⚠️ Partial | | Zero dependencies | ✅ (only commander) | ❌ Heavy | | CLI + API | ✅ Both | ⚠️ Usually one |

Related

License

MIT © SCL339