Jackson Unexpected character ('h' (code 104)): expected a valid value

jackson, java, json

Solution

I'm guessing your variable `url` is a `String`, rather than an `URL`, so Jackson is parsing the URL as if it was an actual JSON string. This would explain the "Unexpected character 'h'", as that is presumably the first letter of your URL (i.e. http... something.) The code "104" corresponds to the ASCII value of "h".

If you use `mapper.readValue(new URL(url), ErrorDataRest.class);` does it work?

Problem

I am accessing a REST API using an URL which gives me the following JSON result: ``` {"size":1,"filter":{"applicationName":"xx.x1", "fromTimestamp":1261746800000, "toTimestamp":1361833200000, "company":"xx", "groupedBy":"COMPANY_APPLICATION"}, "values"[{"applicationName":"xx.x1","count":17,"company":"xx"}], "start":0,"limit":25,"lastPage":true} ``` Using Jackson i try to parse the json this way: ``` ObjectMapper mapper = new ObjectMapper(); try { ErrorDataRest error = mapper.readValue(url, ErrorDataRest.class); Map<String,Object> map = mapper.readValue(url, Map.class); ``` Dosn't matter if i use the first or the second, i get this exception: ``` dk.jyskebank.jee.core.exception.JyskeSystemException: Auto generated exception at dk.sd.dumpmonitor.domainservice.RestErrorServiceBean.queryRESTurl(RestErrorServiceBean.java:46) at service.RestErrorServiceTest.testQueryRESTurl(RestErrorServiceTest.java:19) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: org.codehaus.jackson.JsonParseException: Unexpected character ('h' (code 104)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: java.io.StringReader@30d82d; line: 1, column: 2] at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433) at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521) at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442) at org.codehaus.jackson.impl.ReaderBasedParser._handleUnexpectedValue(ReaderBasedParser.java:1198) at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:485) at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2770) at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2718) at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863) at dk.sd.dumpmonitor.domainservice.RestErrorServiceBean.queryRESTurl(RestErrorServiceBean.java:43) ... 25 more ``` I have no idea what is wrong or how to fix it. i have no acces to the rest API, but i know it uses RESTeasy. The only thing i want is to map the JSON response to a Java class, so perhaps there is some other way to do it?

Original source