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

@jddf/json-schema-to-jddf

v0.1.4

Published

Convert JSON Schema schemas into JSON Data Definition Format

Downloads

1

Readme

json-schema-to-jddf npm

This repository contains json-schema-to-jddf, which comes in two forms:

  • json-schema-to-jddf, a CLI tool which converts JSON Schema into JSON Data Definition Format.

  • @jddf/json-schema-to-jddf, a JavaScript package which takes in already-parsed JSON Schema objects, and converts them into JSON Data Definition Format objects. Internally, the json-schema-to-jddf CLI tool uses this package.

Example Usage (CLI)

Let's say you have a JSON Schema like the one below in a file called example.json:

{
  "type": "object",
  "properties": {
    "is_admin": { "type": "boolean" },
    "name": { "type": "string" },
    "favorite_numbers": {
      "type": "array",
      "items": {
        "type": "number"
      }
    }
  },
  "required": ["is_admin", "name"]
}

You can convert that into a JDDF schema with the following command:

json-schema-to-jddf example.json

This outputs the following JSON:

{
  "properties": {
    "is_admin": {
      "type": "boolean"
    },
    "name": {
      "type": "string"
    }
  },
  "optionalProperties": {
    "favorite_numbers": {
      "elements": {
        "type": "float64"
      }
    }
  },
  "additionalProperties": true
}

For more help using the json-schema-to-jddf tool, run:

json-schema-to-jddf --help

In particular, note that if you run json-schema-to-jddf without arguments, it will attempt to read a JSON Schema from standard input. That's why the program appears to "hang" if you run it without arguments.

Example Usage (JavaScript)

The equivalent use of this package from JavaScript / TypeScript looks like this:

import { toJDDF } from "@jddf/json-schema-to-jddf";

const jsonSchema = {
  type: "object",
  properties: {
    is_admin: { type: "boolean" },
    name: { type: "string" },
    favorite_numbers: {
      type: "array",
      items: {
        type: "number"
      }
    }
  },
  required: ["is_admin", "name"]
}

// This outputs essentially the same thing as the CLI example above.
console.log(toJDDF(jsonSchema))

Caveats

There are a few challenges in writing a tool like json-schema-to-jddf:

  • There are many different ways for JSON Schemas to express the same thing. This makes it difficult, for instance, to detect the different ways you could express "a map from strings to booleans" in JSON Schema.

    This tool attempts to detect the most common ways of doing things in JSON Schema. For example, "a map from strings to booleans", which in JDDF can be expressed only using:

    { "values": { "type": "boolean" }}

    Is expected in JSON Schema to look like:

    {
      "type": "object",
      "additionalProperties": {
        "type": "boolean"
      }
    }
  • JSON Schema is capable of expressing more complex sorts of data formats than JDDF (this is by design -- JDDF lies on the simpler end of the "simplicity vs expressiveness" continuum). Therefore, converting from JSON Schema to JDDF is necessarily a lossy process.

    Where there is no JDDF capable of expressing the constraints in a particular JSON Schema, this tool outputs an empty JDDF schema ({}). An empty JDDF schema will accept anything.

    This caveat is especially important if you have schemas like this one:

    {
      "items": { "type": "string" }
    }

    It may seem as though that schema would accept only arrays of strings. But in fact, it would also accept any non-array JSON value, since there is no "type": "array". The json-schema-to-jddf tool will convert the above JSON Schema to {}, as JDDF cannot express "accept anything that's not an array, and if it's array then all of the elements of that array must be strings".

    If you add "type": "array" to the above JSON Schema, then the tool will output the following JDDF schema:

    { "elements": { "type": "string" }}

If you encouter a JSON Schema which you feel ought to be supported by this tool, please open a GitHub ticket! Your suggestion will be warmly welcomed, and we'll try to find a way to support your use-case.