15

I try to check user detail object with use JSON schema. But I don't know how to check JSON object in Java.

My Schema:

{
     "type" : "object",
     "properties" : {
     "first_name" : {
                     "type" : "string" , 
                     "minLength"  : 3 , 
                     "maxLength" : 255 
                  }, 
   "last_name" : {
                     "type" : "string" , 
                     "minLength"  : 3 , 
                     "maxLength" : 255 
                  },
    "age"       : { 
                     "type" : "integer" , 
                     "minimum" : 16 ,
                      "maximum" : 40
                  },
    "phone_number" : {
                        "type" : "integer",
                        "pattern" : "[6-9][0-9]{9}"
                     } ,
     "email"     : { 
                       "type" : "string",
                       "pattern" : "[a-z0-9]+"
                   } , 
     "password" : { 
                       "type" : "string" ,
                       "minLength" : 7 ,
                       "maxLength" : 255 ,
                       "pattern" : "^.{7, 255}$"
                  } , 
      "gender" : { "enum" : ["Male" , "Female"] }   
},

"required" : ["first_name","last_name" , "age"  ,"email" , "password" 
, "gender" ]
}

My Sample Input:

{
"first_name" : "Sample" ,
"last_name" : "Name" ,
"age"  : 19,
"gender" : "Male",
"phone_number"  :  9080245591,
"email" : "samplle@gmail.com",
"password" : "uni=versity"
}

Any one can say how to check this input with use JSON schema in Java.

lospejos
  • 1,976
  • 3
  • 19
  • 35
Obeth Samuel
  • 590
  • 1
  • 6
  • 18
  • 3
    Java doesn't have native JSON support, so you need JSON library for processing JSON. Your question is basically "What JSON library can check JSON file using JSON schema?", and that is off-topic for StackOverflow: ***Questions asking us to recommend or find a** book, tool, **software library**, tutorial or other off-site resource **are off-topic for Stack Overflow***. Do you even have a JSON Schema? – Andreas Jan 17 '18 at 07:32
  • 1
    @Andreas his example gives a JSON Schema, He didn't really ask for a Library. – pdem Jan 17 '18 at 08:47
  • 1
    @pdem Guess I shouldn't comment at 3 AM, when I can't even see the JSON schema right in front of me. DOH! But the question really is a request to find a library, otherwise why would both answers (incl. yours) be doing just that? – Andreas Jan 17 '18 at 16:04
  • @Andreas Well, ok :). I just didn't find a minimal working example of this on stackoverflow, – pdem Jan 18 '18 at 09:51

2 Answers2

12

The json-schema-validator in GitHub, Perhaps It will help you to check the json object in java.

holten
  • 119
  • 5
11

You can use FasterXML jackson with the module:json-schema-validator as holten proposed.

Include it in maven: com.github.java-json-tools json-schema-validator 2.2.8

Complementary to this, to generate schema from object instead of writing it manually you can also use another module: https://github.com/FasterXML/jackson-module-jsonSchema

I may add a functional example if needed

pdem
  • 3,880
  • 1
  • 24
  • 38