com.fasterxml.jackson.core.JsonParseException: Unrecognized token

12,770 views
Skip to first unread message

Cema N

unread,
Oct 17, 2016, 9:01:48 AM10/17/16
to REST assured
Hi,

I'm using to Wiremock to mock my response.

My Json Schema and Json body is as below:

Json Schema:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product set",
    "items": {
        "title": "product",
        "type": "object",
        "properties": {
            "productId": {
                "type": "number"
            },
            "productName": {
                "type": "string"
            },
            "productPrice": {
                "type": "number",
                "minimum": 0
            },
            "dimensions": {
                "type": "object",
                "properties": {
                    "length": {"type": "number"},
                    "width": {"type": "number"},
                    "height": {"type": "number"}
                }
            }
        },
        "required": ["productId", "productName"]
    }
}

Json Body:


                        "product":{
                            "productId":"5",
                            "productName":"Television",
                            "productPrice":1200,
                            "dimensions":[{
                            "length":35,
                            "width":50,
                            "height":45
                            }]
}
}

I have validated the Json body with schema with an online available validator and body conforms to schema.

I'm using below code to send the response and Json schema as string, where content contains the schema.

assertThat(response.toString(), matchesJsonSchema((content)));


But while I try to assert against schema i get the below error:

io.restassured.module.jsv.JsonSchemaValidationException: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'com': was expecting ('true', 'false' or 'null')
 at [Source: java.io.StringReader@b9267b; line: 1, column: 4]
    at io.restassured.module.jsv.JsonSchemaValidator.matchesSafely(JsonSchemaValidator.java:233)
    at io.restassured.module.jsv.JsonSchemaValidator.matchesSafely(JsonSchemaValidator.java:75)
    at org.hamcrest.TypeSafeMatcher.matches(TypeSafeMatcher.java:65)
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:12)
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
    at TestRestAssured.RestAssuredHamcrest.validateSchema(RestAssuredHamcrest.java:68)
    at TestRestAssured.RestAssuredHamcrest.main(RestAssuredHamcrest.java:57)
Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'com': was expecting ('true', 'false' or 'null')
 at [Source: java.io.StringReader@b9267b; line: 1, column: 4]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1586)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:521)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2754)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1820)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:708)
    at com.fasterxml.jackson.databind.MappingIterator.hasNextValue(MappingIterator.java:240)
    at com.github.fge.jackson.JsonNodeReader.readNode(JsonNodeReader.java:142)
    at com.github.fge.jackson.JsonNodeReader.fromReader(JsonNodeReader.java:127)
    at com.github.fge.jackson.JsonLoader.fromReader(JsonLoader.java:179)
    at com.github.fge.jackson.JsonLoader.fromString(JsonLoader.java:192)
    at io.restassured.module.jsv.JsonSchemaValidator.matchesSafely(JsonSchemaValidator.java:214)
    ... 6 more

Kindly advise.

Thanks for your help in advance.

Regards,
Cema

Cema N

unread,
Oct 18, 2016, 11:46:42 AM10/18/16
to REST assured
Hi,

Can anyone suggest anything that may be wrong with below?

Thanks for the help!

Regards,
Cema

Johan Haleby

unread,
Oct 19, 2016, 1:14:55 AM10/19/16
to rest-a...@googlegroups.com
Could you provide your JSON as well?

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cema N

unread,
Oct 19, 2016, 2:45:18 AM10/19/16
to REST assured
Hi Johan,

Thank you the reply. I have attached both JSON schema and body the question itself. I have also pasted the json file am using for wiremock.
Let me know what I may be missing:

{
    "request": {
            "method": "GET",
            "url": "/test/GETJsonSchemaValidation"
    },
    "response": {
            "status": 200,
            "headers": {
                    "Content-Type": "application/json"
            },
            "jsonBody" : {

                        "product":{
                            "productId":"5",
                            "productName":"Television",
                            "productPrice":1200,
                            "dimensions":[{
                            "length":35,
                            "width":50,
                            "height":45
                            }]
}
}
    }
}

Thanks,
Cema
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.

Johan Haleby

unread,
Oct 19, 2016, 3:11:33 AM10/19/16
to rest-a...@googlegroups.com
What is the JSON you want to validate? Your schema doesn't seem to take all of those properties into account? 

I've tried http://www.jsonschemavalidator.net/ and if I pass in your schema and the JSON:

{
                            "productId":"5",
                            "productName":"Television",
                            "productPrice":1200,
                            "dimensions":[{
                            "length":35,
                            "width":50,
                            "height":45
                            }]}

which I suspect is what you're trying to validate it works. This also works when I try it in REST Assured:

assertThat("{ \"productId\":\"5\",\n" +
" \"productName\":\"Television\",\n" +
" \"productPrice\":1200,\n" +
" \"dimensions\":[{\n" +
" \"length\":35,\n" +
" \"width\":50,\n" +
" \"height\":45\n" +
" }]\n" +
" }", JsonSchemaValidator.matchesJsonSchema("\n" +
"{\n" +
" \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n" +
" \"title\": \"Product set\",\n" +
" \"items\": {\n" +
" \"title\": \"product\",\n" +
" \"type\": \"object\",\n" +
" \"properties\": {\n" +
" \"productId\": {\n" +
" \"type\": \"number\"\n" +
" },\n" +
" \"productName\": {\n" +
" \"type\": \"string\"\n" +
" },\n" +
" \"productPrice\": {\n" +
" \"type\": \"number\",\n" +
" \"minimum\": 0\n" +
" },\n" +
" \"dimensions\": {\n" +
" \"type\": \"object\",\n" +
" \"properties\": {\n" +
" \"length\": {\"type\": \"number\"},\n" +
" \"width\": {\"type\": \"number\"},\n" +
" \"height\": {\"type\": \"number\"}\n" +
" }\n" +
" }\n" +
" },\n" +
" \"required\": [\"productId\", \"productName\"]\n" +
" }\n" +
"}"));

To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.

Cema N

unread,
Oct 19, 2016, 5:23:24 AM10/19/16
to REST assured
Thank you Johan. I was passing the response as a string using response.toString. I changed it to response.prettyPrint() and it worked.

Thanks again as i was stuck with this for 3 days now.


Regards,
Cema

On Monday, October 17, 2016 at 6:31:48 PM UTC+5:30, Cema N wrote:

Johan Haleby

unread,
Oct 19, 2016, 6:01:47 AM10/19/16
to rest-a...@googlegroups.com
Ah ok, I can see that it is confusing. You could probably change it to "asString()" instead to avoid pretty printing to the console.

--

Cema N

unread,
Oct 19, 2016, 7:49:55 AM10/19/16
to REST assured
Thank you Johan. "asString()" worked like a charm.

Regards,
Cema.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages