Giter Site home page Giter Site logo

json-string-schema-generator's Introduction

Build Status codecov

json-string-schema-generator

Generates JSON Schema or POJO based from JSON data using JAVA

Motivation

I have been googling a lot to find a library in JAVA that would generate me a JSON Schema based on the JSON data I have. But most of the libraries I found need to have a JAVA class in order to generate a JSON schema. So I made a simple JSON Schema Generator based on JSON data. If there is a library that already exists please let me know (via posting an Issue here) and I will be happy to use it in the current project I am working on. Thanks!

Features

  1. Generates JSON Schema (output as String or File)
  2. Generates POJOs

Usage

Just call the static method JsonSchemaGenerator.outputAsString or JsonSchemaGenerator.outputAsFile

public class GenerateSchema {
    public static void main(String[] args) throws IOException {
        String json = "{\"sectors\": [{\"times\":[{\"intensity\":30," +
                "\"start\":{\"hour\":8,\"minute\":30},\"end\":{\"hour\":17,\"minute\":0}}," +
                "{\"intensity\":10,\"start\":{\"hour\":17,\"minute\":5},\"end\":{\"hour\":23,\"minute\":55}}]," +
                "\"id\":\"dbea21eb-57b5-44c9-a953-f61816fd5876\"}]}";
        String result = JsonSchemaGenerator.outputAsString("Schedule", "this is a test", json);
            /* sample output
            {
              "title": "Schedule",
              "description": "this is a test",
              "type": "object",
              "properties": {
                "sectors": {
                  "type": "array",
                  "items": {
                    "properties": {
                      "times": {
                        "type": "array",
                        "items": {
                          "properties": {
                            "intensity": {
                              "type": "number"
                            },
                            "start": {
                              "type": "object",
                              "properties": {
                                "hour": {
                                  "type": "number"
                                },
                                "minute": {
                                  "type": "number"
                                }
                              }
                            },
                            "end": {
                              "type": "object",
                              "properties": {
                                "hour": {
                                  "type": "number"
                                },
                                "minute": {
                                  "type": "number"
                                }
                              }
                            }
                          }
                        }
                      },
                      "id": {
                        "type": "string"
                      }
                    }
                  }
                }
              } 
            }
             */

        // To generate JSON schema into a file
        JsonSchemaGenerator.outputAsFile("Schedule", "this is a test", json, "output-schema.json");
        
        // To generate POJO(s)
        JsonSchemaGenerator.outputAsPOJO("Schedule", "this is a test", json, "com.example", "generated-sources");
    }
}

Author

Built With

  1. Java 8
  2. Jackson Databind
  3. JSON Schema Validator
  4. jsonschema2pojo

License

This project is licensed under the MIT License - see the LICENSE file for details

json-string-schema-generator's People

Contributors

arturoeanton avatar dependabot[bot] avatar simplymequeeny avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

json-string-schema-generator's Issues

type:string missing

Hi, i am a user of this project.
i realized that if I add below....

{
"vegetables":
{
"veggieName": "potato",
"size":[""]
}

}

generated schema looks like this..

{
"title": "test",
"description": "new test",
"type": "object",
"properties": {
"vegetables": {
"type": "object",
"properties": {
"veggieName": {
"type": "string"
},
"size": {
"type": "array",
"items": {
"properties":{}
}

},
}
},
}
}

the items don't have --type:object -- and
unnecessary commas also be located

If Type Array is empty then it is failing.

For Example if my input JSON is below :
{
"metadata": {
"properties": []
}
}
Then schema is not generating properly . we are getting below exception
"main" java.lang.NullPointerException.
Because in code we have taken directly
node = jsonNode.get(key).get(0);

Do you have any solution for this ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.