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 🙏

© 2024 – Pkg Stats / Ryan Hefner

guess-json-shape

v1.0.1

Published

Given a lump of JSON, this module will try to guess the shape of the JSON, returning a structure that describes the types and structure of the data.

Downloads

23

Readme

guess-json-shape

Given a lump of JSON, this module will try to guess the shape of the JSON, returning a structure that describes the types and structure of the data.

This can be useful when writing tools that perform transformations such as "json-to-typescript" or "json-to-runtypes"

The library is able make reasonable guesses about the structure of objects in arrays, including nullable fields.

The way most of the guessing is done is heavily inspired by json-to-ts.

Quick start

import { guess } from 'guess-json-shape';

// Some data to analyze, for example an API response
const jsonData = {
  data: {
    articles: [
      { id: '1', slug: 'tutorial', body: 'text here', published: true },
      { id: '2', slug: 'intermediate', body: 'text here', tags: ['docs'] },
    ],
  },
  links: {
    self: 'http://example.com/articles',
    next: 'http://example.com/articles?page=2',
    last: 'http://example.com/articles?page=10',
  },
};

const guessed = guess(jsonData);

The value of guessed is the following:

[
  {
    name: 'Articles',
    isRoot: false,
    type: {
      kind: 'object',
      fields: [
        {
          name: 'id',
          nullable: false,
          type: { kind: 'primitive', type: 'string' },
        },
        {
          name: 'slug',
          nullable: false,
          type: { kind: 'primitive', type: 'string' },
        },
        {
          name: 'body',
          nullable: false,
          type: { kind: 'primitive', type: 'string' },
        },
        {
          name: 'published',
          nullable: true,
          type: { kind: 'primitive', type: 'boolean' },
        },
        {
          name: 'tags',
          nullable: true,
          type: {
            kind: 'array',
            type: {
              kind: 'union',
              types: [{ kind: 'primitive', type: 'string' }],
            },
          },
        },
      ],
    },
  },

  {
    name: 'Data',
    isRoot: false,
    type: {
      kind: 'object',
      fields: [
        {
          name: 'articles',
          type: {
            kind: 'array',
            type: {
              kind: 'union',
              types: [{ kind: 'named', name: 'Articles' }],
            },
          },
        },
      ],
    },
  },

  {
    name: 'Links',
    isRoot: false,
    type: {
      kind: 'object',
      fields: [
        { name: 'self', type: { kind: 'primitive', type: 'string' } },
        { name: 'next', type: { kind: 'primitive', type: 'string' } },
        { name: 'last', type: { kind: 'primitive', type: 'string' } },
      ],
    },
  },

  {
    name: 'Root',
    isRoot: true,
    type: {
      kind: 'object',
      fields: [
        { name: 'data', type: { kind: 'named', name: 'Data' } },
        { name: 'links', type: { kind: 'named', name: 'Links' } },
      ],
    },
  },
];

The structure can be used to create for example type definitions. If you wrote code to convert the above to typescript, it would look like this:

type Articles = {
  id: string;
  slug: string;
  body: string;
  published?: boolean;
  tags?: Array<string>;
};

type Data = {
  articles: Array<Articles>;
};

type Links = {
  self: string;
  next: string;
  last: string;
};

// JSON root type
type Root = {
  data: Data;
  links: Links;
};

API

A single function is exposed: guess(json). It takes a single argument, that should be some parsed JSON. It returns an array of JsonType objects that represent the structure of the parsed JSON. See the

Cabeats and known issues

  • Discriminated unions are not detected. So all the candidate object shapes will be merged into a single object that is mostly wrong. For example:

    guess([
      { kind: 'user', name: 'Rune', passwordHash: 'some-hash' },
      { kind: 'bot', id: 'automation-bot', apiKey: 'some-key' },
    ]);

    Will be detected like this:

    type Guessed = {
      kind: string;
      name?: string;
      passwordHash?: string;
      id?: string;
      apiKey?: string;
    };
    
    type Root = Array<Guessed>;
  • No attempt is made to guess if particular strings are string union types.

  • Empty arrays are guessed to be arrays of never. That is, guessJsonShape([]) is inferred to be Array<never>. The consumer needs to decide how to represent that in their output.

  • Does not work on cirular structures. JSON can not be circular, but it's still possible to pass in something circular.