• Stars
    star
    202
  • Rank 192,967 (Top 4 %)
  • Language
    JavaScript
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatically generate Typescript Definition files or Flow types from JSON input.

Build Status

npm install -g json-ts

Automatically generate Typescript Definition files or Flow types from JSON input.

Use it via the API, CLI, or Website

json-ts

How does json-ts stack up against the competition?

Feature json-ts (this library) json-to-ts json2ts
simple literal types (number, string etc) YES YES YES
array type, all elements of same kind YES YES YES
merge multiple json files YES (cli, v1.6 & above) NO NO
optional members YES YES NO
array union types YES YES NO
correct handling of top-level values (strings, arrays, arrays of objects, numbers etc) YES NO NO
recursive data structures (see here) YES NO NO
nested type literals (to account for invalid interface names) YES YES NO
output @flow types YES NO NO
Website YES YES YES
CLI YES NO NO
API YES YES NO

Quick-start

# install
npm install -g json-ts

# run against a single JSON file
json-ts dir/myfile.json

# run against multiple single JSON files (interfaces will be merged)
json-ts api/resp1.json api/resp2.json

Usage (CLI)

Note: only stdin (which requires the --stdin flag) & filepaths are supported right now. Later I will add support for Windows, reading data from network requests etc.

## piping via stdin
curl https://jsonplaceholder.typicode.com/posts/1 | json-ts --stdin

## reading single json file from disk
json-ts my-file.json

## reading multiple json files from disk
json-ts my-file.json my-other-file.json

... produces the following:

interface IRootObject {
  userId: number;
  id: number;
  title: string;
  body: string;
}

Usage (API)

npm install json-ts --save-dev
const { json2ts } = require('json-ts');
const json = `
{
    "name": "Shane"
}
`;
console.log(json2ts(json))

... produces the following:

interface IRootObject {
  name: string;
}

For more examples, see the Tests

Options

  • namespace: string - if provided, interfaces will be wrapped in a namespace (see below)
    # usage
    json-ts <filename> --namespace <namespace_name> 
    
    # example
    json-ts data/my-file.json --namespace API
  • flow: boolean - output types in Flow format.
    # usage
    json-ts <filename> --flow 
    
    # example
    json-ts data/my-file.json --flow
  • prefix: string - override the I prefix on interface names
    # usage
    json-ts <filename> --prefix <prefix_string> 
    
    # example (remove prefix)
    json-ts data/my-file.json --prefix ""
  • rootName: string - override the RootObject name of the top-level interface
    # usage
    json-ts <filename> --rootName <rootname_string> 
    
    # example
    json-ts data/my-file.json --rootName "Product"

TODO:

options

  • Allow choice of I prefix on interface names
  • Allow naming of RootObject
  • Allow choice of spaces/tabs

Core

  • support array types
  • support boolean types
  • support null types
  • output types for Flow via --flow
  • PascalCase as default for all interface names
  • de-dupe interfaces (it's dumb atm, POC)
  • de-dupe interfaces where propname differs, but members are the same
  • merge interfaces by creating union types for members
  • union types for array that contain mixed literal types: nums: [1, "2"] -> nums: number|string[] (already works for complex objects)
  • quoted member names when needed
  • handle invalid name for interface
  • support type alias declarations
  • Use Typescript factory methods/printer for output
  • Allow wrapping in namespace: eg:
        declare namespace Projects {
            export interface ILoc {
               lat: number;
               lng: number;
            }
            ...
        }

CLI

  • CLI tool to accept stdin (with --stdin flag)
  • CLI tool to accept json file as input
  • CLI tool to accept URL as input (for validating against remote API)
  • configurable output (filename/stdout etc)

More Repositories

1

jekyll-gulp-sass-browser-sync

A starter project including full setup for Jekyll, GulpJS, SASS & BrowserSync
CSS
728
star
2

gulp-svg-sprites

Create SVG sprites or compile to <symbols>
JavaScript
332
star
3

laravel-docker

repo for upcoming blog post
PHP
123
star
4

dev-ip

Find a suitable IP host to view local websites on
JavaScript
118
star
5

html-injector

BrowserSync html injector
JavaScript
78
star
6

grunt-cache-breaker

Simple cache-breaker - appends a timestamp to an asset url
JavaScript
74
star
7

next-no-js

TypeScript
53
star
8

cra-docker

Create React App + Docker - multi-stage build example
JavaScript
50
star
9

serde-zod

Rust
45
star
10

arewereadyformobileyet

Using Lighthouse to provide automated analysis of what frameworks give you 'out of the box'
HTML
41
star
11

nginx-cors-plus

A simple nginx proxy that you can put in front of any domain to enable CORS.
Shell
31
star
12

actor-js

Exploring the potential benefits of implementing APIs in the style of the Actor Model, even in single-threaded programming environments such as Javascript.
TypeScript
30
star
13

egghead-redux-obs

JavaScript
29
star
14

browser-sync-spa

Better Single Page App support for BrowserSync
JavaScript
29
star
15

laravel4-backbone-marionette

A starter project complete with a build step
JavaScript
27
star
16

bs-fullscreen-message

Fullscreen, in-browser error/success messages
JavaScript
19
star
17

foxy

Proxy with response moddin'
JavaScript
18
star
18

bem-validator

JavaScript
16
star
19

eazy-logger

easy cli logger
JavaScript
14
star
20

tfunk

Multi-colour console output from Chalk with added awesome.
JavaScript
13
star
21

js-blade

Javascript port of Laravel's Blade Templating language for Node
JavaScript
13
star
22

resp-modifier

middleware for modifying a html response
JavaScript
12
star
23

gulp-contribs

Give your open-source contributors some credit - automatically list them in your readme.md, or anywhere else.
JavaScript
10
star
24

egghead-preact-up-and-running

10
star
25

m2run

The zero-config way to contribute to Magento 2.
Rust
9
star
26

action-typed

Better type safety from less typing - making Redux work more like Elm
TypeScript
9
star
27

redux-obs-v1

JavaScript
9
star
28

bs-rewrite-rules

UI interface for adding/removing/editing Browsersync's rewrite rules
JavaScript
8
star
29

bs-snippet-injector

JavaScript
6
star
30

staunch

Redux-style state management for large-scale apps. Powered by ImmutableJS & RxJS. Async built-in.
JavaScript
6
star
31

easy-extender

plugin/hooks interface
JavaScript
5
star
32

entry-wrap-webpack-plugin

Wrap entry points with any code
JavaScript
5
star
33

egghead-gulp-browserify

JavaScript
5
star
34

bs-flask

Debugging repo for BrowserSync + Flask
Python
5
star
35

laravel-backbone-boilerplate

Boiler-plate app for rapidly creating a project using Laravel & Backbone.
PHP
5
star
36

wptuts-map-directions

This is the source code used in this tutorial - http://wp.tutsplus.com/tutorials/creative-coding/give-your-customers-driving-directions-with-the-google-maps-api/
JavaScript
5
star
37

cache-breaker

Cache Breaking tasks
JavaScript
4
star
38

from_file

A simple convenience to deserialize a rust Struct or Enum directly from a file path.
Rust
4
star
39

img-compare

Stand alone image comparisons for Node JS
JavaScript
4
star
40

redux-observable

JavaScript
3
star
41

module-federation-first-framework

TypeScript
3
star
42

svg-sprite-data

JavaScript
3
star
43

wordpress-dev-helper

Helper Class that allows you to minify and concatenate your JS when working with Wordpress.
PHP
3
star
44

blog

TypeScript
3
star
45

shakyshane-blog

Source code for shakyshane.com
CSS
3
star
46

swag2ts

TypeScript
3
star
47

learn-regex

An introduction to regular expressions in JavaScript
CSS
2
star
48

update-checker

Check the NPM registry for package updates
JavaScript
2
star
49

lit-ssr-vercel

Astro
2
star
50

heartbeat-monitor

Keep track of items via heartbeats
JavaScript
2
star
51

learn-regex-dev

The development repo for my learn-regex app
JavaScript
2
star
52

cl-strings

multi-colour string template for Node.js apps
JavaScript
2
star
53

opt-merger

Merge defaults opts with cli or api
JavaScript
2
star
54

amjs

Actor Model + Web Workers
TypeScript
2
star
55

create-epic-store

default setup for redux + redux-observable with added 'register'
TypeScript
2
star
56

portscanner-plus

Get multiple available ports with optional names
JavaScript
2
star
57

emitter-steward

Cycle & reset allowed emitters
JavaScript
2
star
58

docker-php-nginx

PHP
2
star
59

highlightjs-themes

CSS
2
star
60

wptuts-wordpress-faq

The files for this article http://wp.tutsplus.com/tutorials/creative-coding/create-an-faq-accordion-for-wordpress-with-jquery-ui/
PHP
2
star
61

async-nested-router

TypeScript
1
star
62

laravel-php-5-6

Dockerfile
1
star
63

vite-xstate-router

TypeScript
1
star
64

localstorage-html5

Test area for local storage
1
star
65

cb2

Rust
1
star
66

prom-seq

Just a sequence of promises
JavaScript
1
star
67

docker-wp

PHP
1
star
68

rx-react

JavaScript
1
star
69

rs-playground

Rust
1
star
70

norman.js

Low-level value mapping
JavaScript
1
star
71

actix-multi

Rust
1
star
72

magento2

HTML
1
star
73

closeup.js

A modern, minimalist image zoomer.
JavaScript
1
star
74

topics

A documentation system for developer workflows that you will keep up to date.
Rust
1
star
75

p-diff

Fork of http://pdiff.sourceforge.net/
C++
1
star
76

wdifu

What did I f*** up?
C++
1
star
77

scrape

playing with chromes remote debug protocol
JavaScript
1
star
78

vctest

Testing!
1
star
79

css-reg-test

JavaScript
1
star
80

imm-validate

a forgiving validator/formatter
JavaScript
1
star