4

I want convert json document into json schema. I googled it but not got the exact idea according to my requirement.

here is JSON

 {
 "empId":1001,
 "firstName":"jonh",
 "lastName":"Springer",
 "title": "Engineer",
 "address": {
    "city": "Mumbai",
    "street": "FadkeStreet",
    "zipCode":"420125",
    "privatePhoneNo":{
            "privateMobile": "2564875421",
            "privateLandLine":"251201546"
    }
},
"salary": 150000,
"department":{
     "departmentId": 10521,
     "departmentName": "IT",
     "companyPhoneNo":{
             "cMobile": "8655340546",
             "cLandLine": "10251215465"
      },
     "location":{
             "name": "mulund",
             "locationId": 14500
      }
  }
}

I want to generate like this

   {
   "$schema": "http://json-schema.org/draft-04/schema#",
   "type": "object",
   "title": "Employee",
   "properties": {
     "empId": {
           "type": "integer"
      },
      "firstName":{
           "type":"string"
      },
      "lastName": {
           "type": "string"
      },
      "title": {
           "type": "string"
     },
     "address": {
         "type": "object",
         "properties": {
                       "city": {
                                 "type": "string"
                        },
                        "street": {
                                 "type": "string"
                        },
                        "zipCode": {
                                 "type": "string"
                        },
                       "privatePhoneNo": {
                                 "type": "object",
                                 "properties": {
                                        "privateMobile": {
                                                 "type": "string"
                                         },
                                        "privateLandLine": {
                                                 "type": "string"
                                         }
                                   }
                          }
            }
      },
      "salary": {
            "type": "number"
      },
      "department": {
            "type": "object",
            "properties": {
                   "departmentId": {
                            "type": "integer"
                    },
                    "departmentName": {
                            "type": "string"
                    },
                    "companyPhoneNo": {
                            "type": "object",
                            "properties": {
                                         "cMobile": {
                                              "type": "string"
                                          },
                                         "cLandLine": {
                                              "type": "string"
                                          }
                              }
     },
     "location": {
                 "type": "object",
                 "properties": {
                               "name": {
                                     "type": "string"
                                 },
                              "locationId": {
                                     "type": "integer"
                               }
                   }
      }
    }
   }
 }
}

Is there any library is doing like this or what is another way?

3

2 Answers 2

2

It's been a while since this was asked but I was having the same issue. So far the best solution I have come across is this library: https://github.com/saasquatch/json-schema-inferrer

I found this from the json-schema doc itself. It has links to implementations for other languages as well: https://json-schema.org/implementations.html#from-data

Sign up to request clarification or add additional context in comments.

Comments

1

https://github.com/perenecabuto/json_schema_generator

http://jsonschema.net/#/

I'm think this maybe will help

3 Comments

yes @Garik Khachanyan like this I want to generate it but I want to generate by java code.I want to create utility that converts json to json schema
I am not understanding how to use json_schema_generator git hub from java code ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.