user6284034 user6284034 - 3 months ago 44
JSON Question

Mule json schema validation

I am trying to use Validate JSON schema component in Mule flow and I am getting com.fasterxml.jackson.core.JsonParseException for the json that is passed. Below is the json schema, json sample and code of Mule flow. Can you please point me where am I doing mistake?

Json schema:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",

"properties": {

"id": {
"description": "The unique identifier for a product",
"type": "integer"
},

"name": {
"description": "Name of the product",
"type": "string"
},

"price": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
}
},

"required": ["id", "name", "price"]
}


Json passed to POST method:

[
{
"id": 2,
"name": "An ice sculpture",
"price": 12.50,
},

{
"id": 3,
"name": "A blue mouse",
"price": 25.50,
}
]


Error :

Root Exception stack trace:
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('}' (code 125)): was expecting double-quote to start field name
at [Source: java.io.InputStreamReader@6e7f030; line: 6, column: 5]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:508)


Mule Flow:

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="jsonschemavalidationFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" allowedMethods="POST" doc:name="HTTP"/>
<json:validate-schema schemaLocation="jsonschema.json" doc:name="Validate JSON Schema"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>

Answer

There are few errors in both the JSON and the Schema as follows:-
There is an extra comma, after "price": 12.50
So the Valid JSON will be :-

[
   {
      "id": 2,
      "name": "An ice sculpture",
      "price": 12.50
   },

   {
      "id": 3,
      "name": "A blue mouse",
      "price": 25.50
   }
]

In JSON Schema file jsonschema.json There are two errors:-
You need to put "type": "array" instead of "type": "object"
and next is "exclusiveMinimum": true seems to be invalid with this JSON input...
So, the correct working JSON schema will be :-

{
   "$schema": "http://json-schema.org/draft-04/schema#",
   "title": "Product",
   "description": "A product from Acme's catalog",
   "type": "array",

   "properties": {

      "id": {
         "description": "The unique identifier for a product",
         "type": "integer"
      },

      "name": {
         "description": "Name of the product",
         "type": "string"
      },

      "price": {
         "type": "number",
         "minimum": 0
      }
   },

   "required": ["id", "name", "price"]
}  

Try it.. it will work fine :)

Comments