Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-to-typing

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-to-typing

[![NPM version](https://img.shields.io/npm/v/json-to-typing.svg)](https://www.npmjs.com/package/json-to-typing) [![Build Status](https://travis-ci.org/atayahmet/json-to-typing.svg?branch=main)](https://travis-ci.org/atayahmet/json-to-typing) [![Coverage S

  • 0.1.2
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

json-to-typing

NPM version Build Status Coverage Status Conventional Commits Standard Version

Transform json data to Typescript type elements. If do not use Typescript and need to cover type of json data, this tool will be helpfuly for you. You can also use in terminal to automate the transfom process as cli command.

Install

Npm:

$ npm i json-to-typing --save

Yarn:

$ yarn add json-to-typing

Examples:

Basic

import jsonToTyping from 'json-to-typing';

jsonToTyping('{"name": "John", "age": 30, "isActive": true}');

Result:

{
  name: string;
  age: number;
  isActive: boolean;
}

Nested

{
  "name": "John",
  "age": 30,
  "isActive": true,
  "books": ["Book 1", "Book 2", "Book 3"],
  "movies": {
    "genres": ["sci-fi", "comedy", "fantastic"],
    "favorite": "The Lord Of The Rings"
  }
}
jsonToTyping(nested);

Result:

{
  name: string;
  age: number;
  isActive: boolean;
  books: [string, string, string];
  movies: {
    genres: [string, string, string];
    favorite: string;
  }
}

CLI

Install Globally

$ npm i -g json-to-typing
$ json2typing --help

Run locally

$ node_modules/.bin/json2typing --help

Run with npx

$ npx json2typing --help

Examples

Basic:

$ json2typing --source '{"name": "John"}'

output:

{
  name: string;
}

Interface Example:

$ json2typing interface IUser --source '{"name": "John"}'

output:

interface IUser {
  name: string;
}

Note: You can use --export flag to add export keyword.

$ json2typing interface IUser --source '{"name": "John"}' --export

output:

export interface IUser {
  name: string;
}

Typing types

Name
interface
declare
type

Flags

NameDescription
--sourceJSON string or json file path
--exportAdd export keyword
--helpShow help menu

Use in CMD Pipeline

$ echo '{"name": "John"}' | json2typing type Member --export

output:

export type Member {
  name: string;
}

Another:

$ cat ./package.json | json2typing declare Package --export

output:

export declare const Package: {
  version: string;
  license: string;
  main: string;
  typings: string;
  bin: {
    json2typing: string;
  };
  files: [string, string];
  engines: {
    node: string;
  };
  scripts: {
    start: string;
    build: string;
  };
  peerDependencies: {};
  husky: {
    hooks: {
      'pre-commit': string;
      'commit-msg': string;
    };
  };
  prettier: {
    printWidth: number;
    semi: boolean;
    singleQuote: boolean;
    trailingComma: string;
  };
  name: string;
  author: string;
  module: string;
  devDependencies: {
    husky: string;
  };
  dependencies: {};
};

Tests

$ npm test

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Keywords

FAQs

Package last updated on 18 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc