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 🙏

© 2026 – Pkg Stats / Ryan Hefner

json-forger

v1.0.1

Published

Library to generate a JSON file generator based on a custom json schema

Readme

JSON Forger

This script is a JSON file generator based on a custom json schema

Installation

  • Clone the repository to your local machine.
  • Install dependencies by running npm install.

Library Usage

  • Execute this command to install the library npm i json-forger
// Method 01:
import { JsonGenerator, readAndSaveSchema } from "json-forger";

const generator = new JsonGenerator();

const schema = await readAndSaveSchema("schema.json");
console.log(generator.generateJsonFromSchema(schema));


// Method 02:
const { JsonGenerator, readAndSaveSchema } = require("json-forger");

(async () => {
  const generator = new JsonGenerator();
  const schema = await readAndSaveSchema("schema.json");
  console.log(generator.generateJsonFromSchema(schema));
})();
  • Information about the functions
    • readAndSaveSchema This function, parses the JSON schema file from a given file path, parses its contents into a JavaScript object, and returns the parsed object. If there's an error during this process, it catches the error, logs it, and returns null.
    • generateJsonFromSchema This function, generateJsonFromSchema, takes a propertySchema object and an array of propertyPath as input and generates JSON data based on the schema provided. Let's break down how it works:
      • Destructuring: The function destructures properties from the propertySchema object such as type, format, pattern, enum, useDefault, default, minimum, maximum, minLength, maxLength, length, items, properties, and countryCode.
      • Switch Statement: The function checks the type property of the propertySchema object using a switch statement.
      • Case Handling:
        • Integer: Generates a random integer within a specified range (if provided) or uses a default value.
        • String: Generates a random string based on different conditions like format, pattern, enum values, length, etc. Handles cases for phone numbers specifically if format is 'phone' and countryCode is provided (country code examples: US, FR, LB, NO, etc.)
        • Object: Calls a function generateObjectData to generate data for nested objects.
        • Array: Calls a function generateArrayData to generate data for arrays.
        • Boolean: Generates a random boolean value.
        • Date: Generates a random date. It considers format and default value if provided. The supported formats are: "YYYY-MM-DD", "YYYY/MM/DD", "YYYY-MMM-DD", "YYYY/MMM/DD", "DD-MM-YYYY", "DD/MMM/YYYY", "DD-MMM-YYYY", "DD/MMM/YYYY"
        • Default: Returns the default value or null if none specified.
      • Return Values: The function returns the generated value based on the type of the property defined in the schema.

Contributing

Contributions to the JSON Generator are welcome! Here are some ways you can contribute:

  • Submit bug reports or feature requests by opening an issue.
  • Fork the repository and submit pull requests for bug fixes or new features.
  • Improve documentation or README.

Example

  • Json Schema Template
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
      "user": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "username": { "type": "string", "pattern": "^[A-Za-z]{3,16}$" },
          "email": { "type": "string", "format": "email", "default": "[email protected]", "useDefault": false},
          "work_email": { "type": "string", "format": "email", "default": "[email protected]", "useDefault": true},
          "phone_number": {"type": "string", "format": "phone"},
          "profile": {
            "type": "object",
            "properties": {
              "name": { "type": "string", "length": 5 },
              "age": { "type": "integer", "minimum": 18, "maximum": 150 },
              "city": { "type": "string", "length": 20 },
              "dob": {"type": "date", "format": "YYYY-MMM-DD", "default": "1999-01-01", "useDefault": false}
            }
          }
        }
      }
    },
    "required": ["user"]
  }
  • Result
{
      "user": {
        "id": 4576033970402622,
        "username": "KZaLLwiCjom",
        "email": "[email protected]",
        "work_email": "[email protected]",
        "phone_number": "+33 (02) 00 14 23 07",
        "profile": {
          "name": "uJKdi",
          "age": 48,
          "city": "2lBJTXQxl5KtNCD8hm7s",
          "dob": "1958-Feb-02"
        }
      }
}