Generating Typescript interfaces from JSON on VS Code

Learn how you can generate Typescript interfaces from JSON on VS Code

Firmino Changani

If you caught yourself setting variables using the any type from typescript to declare variables that will hold the response object coming from an HTTP operation, then this article may help you to tackle that in the future.

Table of contents

The problem that's not exactly a problem

Let's suppose you're building a web application that has the requirement of fetching the information regarding all the repositories of a specific user on Github. Knowing Github large payload, you might try to define a constant like this:

// ...
const response = await fetch(`${user}/repos`);
const userRepositories: any = await response;
// ...

Whilst this is a valid approach, you'd lose Typescript's type safety and predictability since userRepositories type was set to any.

One quick way to tackle this is by defining an interface beforehand. However here comes the problem: who likes to manually define the interface our interfaces of a response payload as large as this:

  "id": 178168699,
  "node_id": "MDEwOlJlcG9zaXRvcnkxNzgxNjg2OTk=",
  "name": "all-contributors",
  "full_name": "flowck/all-contributors",
  "private": false,
  "owner": {
    "login": "flowck",
    "id": 1679333,
    "node_id": "MDQ6VXNlcjE2NzkzMzM=",
    "avatar_url": "",
    "gravatar_id": "",
    "url": "",
    "html_url": "",
    "followers_url": "",
    "following_url": "{/other_user}",
    "gists_url": "{/gist_id}",
    "starred_url": "{/owner}{/repo}",
    "subscriptions_url": "",
    "organizations_url": "",
    "repos_url": "",
    "events_url": "{/privacy}",
    "received_events_url": "",
    "type": "User",
    "site_admin": false
  "html_url": "",
  "description": "✨ Recognize all contributors, not just the ones who push code ✨",
  "fork": true,
  "url": "",
  "forks_url": "",
  "keys_url": "{/key_id}",
  "collaborators_url": "{/collaborator}",
  "teams_url": "",
  "hooks_url": "",
  "issue_events_url": "{/number}",
  "events_url": "",
  "assignees_url": "{/user}",
  "branches_url": "{/branch}",
  "tags_url": "",
  "blobs_url": "{/sha}",
  "git_tags_url": "{/sha}",
  "git_refs_url": "{/sha}",
  "trees_url": "{/sha}",
  "statuses_url": "{sha}",
  "languages_url": "",
  "stargazers_url": "",
  "contributors_url": "",
  "subscribers_url": "",
  "subscription_url": "",
  "commits_url": "{/sha}",
  "git_commits_url": "{/sha}",
  "comments_url": "{/number}",
  "issue_comment_url": "{/number}",
  "contents_url": "{+path}",
  "compare_url": "{base}...{head}",
  "merges_url": "",
  "archive_url": "{archive_format}{/ref}",
  "downloads_url": "",
  "issues_url": "{/number}",
  "pulls_url": "{/number}",
  "milestones_url": "{/number}",
  "notifications_url": "{?since,all,participating}",
  "labels_url": "{/name}",
  "releases_url": "{/id}",
  "deployments_url": "",
  "created_at": "2019-03-28T09:20:02Z",
  "updated_at": "2019-03-28T09:20:04Z",
  "pushed_at": "2019-03-25T21:08:05Z",
  "git_url": "git://",
  "ssh_url": "",
  "clone_url": "",
  "svn_url": "",
  "homepage": "",
  "size": 15873,
  "stargazers_count": 0,
  "watchers_count": 0,
  "language": "HTML",
  "has_issues": false,
  "has_projects": true,
  "has_downloads": true,
  "has_wiki": false,
  "has_pages": false,
  "forks_count": 0,
  "mirror_url": null,
  "archived": false,
  "disabled": false,
  "open_issues_count": 0,
  "license": {
    "key": "mit",
    "name": "MIT License",
    "spdx_id": "MIT",
    "url": "",
    "node_id": "MDc6TGljZW5zZTEz"
  "forks": 0,
  "open_issues": 0,
  "watchers": 0,
  "default_branch": "master"

Challenging isn't it? Manually? No!

Generating interfaces from JSON objects

On VS Code, I use the extension Paste JSON as CODE, and it enables me to paste JSON as an interface, even one as large as the described above.

After you've installed it in your VS Code, follow the steps below:

  • Open the desired .ts where you would like to paste the interface;
  • Type the combination: CMD + SHIFT + P (macOS) or CTRL + SHIFT + P (Windows and Linux);
  • Search for Paste JSON as CODE and press ENTER;
  • A new input field will be displayed, paste the JSON code on it and press ENTER;
  • The last step is to review and edit the names of the generated interfaces according to your needs;

VS Code

What can you do if you use another editor

I did a quick search for editors like Webstorm and Sublime Text, and unfortunately didn't find an extension that does the same. Maybe I didn't put enough effort into my search, however, the creators of Past JSON as CODE created an online editor where you can paste JSON code and get the equivalent interface.



I've been using Past JSON as CODE extension for a while, and one thing that saves me time is the fact that it generates the interfaces recursively on nested JSON objects, giving me access to its child interfaces on a single operation.

To close this post, I'd say:

Automate whenever you can, your mind should busy with non-trivial thoughts and decisions