• Stars
    star
    162
  • Rank 232,284 (Top 5 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Java library for inferring JSON schema from sample JSONs

json-schema-inferrer

License JavaCI

Java library for inferring JSON schema based on sample JSONs.

Demo site

Here is a simple demo site for this library that showcases some of the things it's capable of.

Sample usage

import java.util.Arrays;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.saasquatch.jsonschemainferrer.*;

public class Example {

  private static final ObjectMapper mapper = new ObjectMapper();
  private static final JsonSchemaInferrer inferrer = JsonSchemaInferrer.newBuilder()
      .setSpecVersion(SpecVersion.DRAFT_06)
      // Requires commons-validator
      .addFormatInferrers(FormatInferrers.email(), FormatInferrers.ip())
      .setAdditionalPropertiesPolicy(AdditionalPropertiesPolicies.notAllowed())
      .setRequiredPolicy(RequiredPolicies.nonNullCommonFields())
      .addEnumExtractors(EnumExtractors.validEnum(java.time.Month.class),
          EnumExtractors.validEnum(java.time.DayOfWeek.class))
      .build();

  public static void main(String[] args) throws Exception {
    final JsonNode sample1 = mapper.readTree(
        "{\"πŸ™ˆ\":\"https://saasquatch.com\",\"πŸ™‰\":[-1.5,2,\"[email protected]\",false],\"πŸ™Š\":3,\"weekdays\":[\"MONDAY\",\"TUESDAY\"]}");
    final JsonNode sample2 = mapper.readTree(
        "{\"πŸ™ˆ\":1,\"πŸ™‰\":{\"πŸ’\":true,\"🐡\":[2,\"1234:5678::\"],\"🍌\":null},\"πŸ™Š\":null,\"months\":[\"JANUARY\",\"FEBRUARY\"]}");
    final JsonNode resultForSample1 = inferrer.inferForSample(sample1);
    final JsonNode resultForSample1And2 =
        inferrer.inferForSamples(Arrays.asList(sample1, sample2));
    for (JsonNode j : Arrays.asList(sample1, sample2, resultForSample1, resultForSample1And2)) {
      System.out.println(mapper.writeValueAsString(j));
    }
  }

}

In the code above, sample1 is:

{
  "πŸ™ˆ": "https://saasquatch.com",
  "πŸ™‰": [-1.5, 2, "[email protected]", false],
  "πŸ™Š": 3,
  "weekdays": ["MONDAY", "TUESDAY"]
}

sample2 is:

{
  "πŸ™ˆ": 1,
  "πŸ™‰": { "πŸ’": true, "🐡": [2, "1234:5678::"], "🍌": null },
  "πŸ™Š": null,
  "months": ["JANUARY", "FEBRUARY"]
}

resultForSample1 is:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "object",
  "properties": {
    "πŸ™ˆ": { "type": "string" },
    "πŸ™Š": { "type": "integer" },
    "weekdays": { "type": "array", "items": { "enum": ["MONDAY", "TUESDAY"] } },
    "πŸ™‰": {
      "type": "array",
      "items": {
        "anyOf": [
          { "type": ["number", "boolean"] },
          { "type": "string", "format": "email" }
        ]
      }
    }
  },
  "additionalProperties": false,
  "required": ["πŸ™ˆ", "πŸ™Š", "weekdays", "πŸ™‰"]
}

And resultForSample1And2 is:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "object",
  "properties": {
    "πŸ™ˆ": { "type": ["string", "integer"] },
    "months": { "type": "array", "items": { "enum": ["JANUARY", "FEBRUARY"] } },
    "πŸ™Š": { "type": ["null", "integer"] },
    "weekdays": { "type": "array", "items": { "enum": ["MONDAY", "TUESDAY"] } },
    "πŸ™‰": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "🐡": {
              "type": "array",
              "items": {
                "anyOf": [
                  { "type": "integer" },
                  { "type": "string", "format": "ipv6" }
                ]
              }
            },
            "🍌": { "type": "null" },
            "πŸ’": { "type": "boolean" }
          },
          "additionalProperties": false,
          "required": ["🐡", "πŸ’"]
        },
        {
          "type": "array",
          "items": {
            "anyOf": [
              { "type": ["number", "boolean"] },
              { "type": "string", "format": "email" }
            ]
          }
        }
      ]
    }
  },
  "additionalProperties": false,
  "required": ["πŸ™ˆ", "πŸ™‰"]
}

For more examples, see package com.saasquatch.jsonschemainferrer.examples.

Adding it to your project

Add the repository

Maven

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>

Gradle

repositories {
  maven { url 'https://jitpack.io' }
}

Add the dependency

Maven

<dependency>
  <groupId>com.github.saasquatch</groupId>
  <artifactId>json-schema-inferrer</artifactId>
  <version>0.2.0</version>
</dependency>

Gradle

implementation 'com.github.saasquatch:json-schema-inferrer:0.2.0'

Transitive Dependencies

This project requires Java 8. The only required transitive dependencies are Jackson and FindBugs (JSR305). If you opt into using some of the built-in FormatInferrers, Commons Validator will also be needed. This is encapsulated with a Gradle feature variant named builtInFormatInferrerSupport.

Pre-release Versions

Pre-release versions and snapshots (as well as stable releases) can be obtained through JitPack.

License

Unless explicitly stated otherwise all files in this repository are licensed under the Apache License 2.0.

License boilerplate:

Copyright 2023 ReferralSaaSquatch.com, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

bunshi

Molecule pattern for jotai, valtio, zustand, nanostores, xstate, react and vue
TypeScript
218
star
2

stencil-hooks

A React-hooks API for Stencil components
TypeScript
39
star
3

jsonata-visual-editor

Visual editor for jsonata
TypeScript
21
star
4

dom-context

A context library for web components and vanilla dom
TypeScript
13
star
5

jsonata-ui-core

Core AST and serializers for jsonata-ui
TypeScript
13
star
6

jotai-hook-form

Build complex forms with jotai, using JSON pointers.
TypeScript
11
star
7

saasquatch-docs

πŸ“– SaaSquatch public documentation
HTML
8
star
8

squatch-ios

Official Swift SDK for SaaSquatch. REST API wrapper and widgets in iOS WKWebView
Swift
7
star
9

squatch-js

🌐 The official SaaSquatch Javascript Web/Browser SDK
TypeScript
5
star
10

squatch-android

Official Android SDK for SaaSquatch. REST API wrapper and widgets in Android WebViews
Java
5
star
11

universal-hooks

The "Write Once, Run Anywhere" React Hooks API
TypeScript
4
star
12

raisins

Likes GrapesJS but more dry
HTML
4
star
13

saasquatch-cli

❯ A command line interface to the Referral SaaSquatch API
JavaScript
4
star
14

git-branch-util

Makes integrating feature branches into "staging" and "master" branches easy
Ruby
3
star
15

mobile-sdk-android-sample

Sample Android Mobile SDK App
Java
2
star
16

saasquatch-java-sdk

SaaSquatch SDK for Java
Java
2
star
17

program-tools

Collection of tools and examples to help create program templates for the SaaSquatch platform
TypeScript
2
star
18

mobile-sdk-ios-sample

Sample ios SDK App
Swift
2
star
19

picklesdoc

Generate XLSX and JSON documents from your Gherkin feature files. Javascript port of picklesdoc.com
TypeScript
1
star
20

schema

πŸ€– Machine readable schemas global to the SaaSquatch platform
TypeScript
1
star
21

micro-job-scheduler

Promise-based in-memory micro job scheduler.
TypeScript
1
star
22

mobile-sdk-objectivec-cocoapod

πŸ“± The official Referral SaaSquatch iOS Mobile SDK for Objective-C
Objective-C
1
star
23

time-credit-theme

A generic time credit theme for Referral Saasquatch
HTML
1
star
24

rhino-jsonata

JSONata for Java backed by jsonata-js and Rhino
1
star
25

stencil-docx-docs

Stencil Word .docx component documentation generator
TypeScript
1
star
26

dollar-credit-theme

A generic dollar credit theme for Referral Saasquatch
HTML
1
star
27

percent-credit-theme

A generic percent credit theme for Referral Saasquatch
HTML
1
star
28

apache-client5-reactive

Thin wrapper around Apache HttpAsyncClient 5.x to expose Reactive Streams interfaces.
Java
1
star
29

stencil-html-parser

HTML to Stencil Parser. Stencil fork of https://github.com/remarkablemark/html-react-parser
TypeScript
1
star