[Solved] com.fasterxml.jackson.core.JsonParseException: Unexpected character (”’ (code 39)): was expecting double-quote to start field name

JsonParseException is subclass of JsonProcessingException. This exception occurred because parsing issues or non-well-formed content because of not fulling JSON syntax specification.

JsonParserException Example

Here JSonParserException occurred because not fulfilling the syntax specification because properties name  and String values in JSON should always write in double-quotes (“);

public class StudentDetail {
	private int rollNumber;
	private String firstName;
	private String lastName;
	//getter and setter of class
	}
public class TestJacksonExample2 {

	public static void main(String[] args) {

	    String json="{\'rollNumber\':21 , \'firstName\':\'Saurabh\' , \'lastName\':\'Gupta\'} ";

	    System.out.println(json);
		try
		{
	    ObjectMapper mapper = new ObjectMapper();
	    StudentDetail student= mapper.readValue(json, StudentDetail.class);
	    System.out.println(student);
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}

	}

}

Exception Stacktrace


{'rollNumber':21 , 'firstName':'Saurabh' , 'lastName':'Gupta'} 
com.fasterxml.jackson.core.JsonParseException: Unexpected character (''' (code 39)): was expecting double-quote to start field name
 at [Source: (String)"{'rollNumber':21 , 'firstName':'Saurabh' , 'lastName':'Gupta'} "; line: 1, column: 3]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:693)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:591)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddName(ReaderBasedJsonParser.java:1767)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:692)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
    at com.fiot.json.jackson.exceptions.TestJacksonExample2.main(TestJacksonExample2.java:18)

Solutions

There are two solutions to resolve this issue:

  1. Send JSON in a standard format as with properties name and String value in double-quotes.
     String json="{\"rollNumber\":21 , \"firstName\":\"Saurabh\" , \"lastName\":\"Gupta\"}";
    
  2. By configuring the ObjectMapper to allow single quotes as below:
    public static void main(String[] args) {
    String json="{\'rollNumber\':21 , \'firstName\':\'Saurabh\' , \'lastName\':\'Gupta\'} ";
    
    System.out.println(json);
    try
       {
    	//add to allow single quote
    	JsonFactory factory = new JsonFactory();
    	factory.enable(Feature.ALLOW_SINGLE_QUOTES);
    	ObjectMapper mapper = new ObjectMapper(factory);
    
    	StudentDetail student= mapper.readValue(json, StudentDetail.class);
    	System.out.println(student);
        }
     catch(IOException ex)
    {
    	ex.printStackTrace();
    }
    catch(Exception ex)
    {
    	ex.printStackTrace();
    }
    }
    

You would like to see

Follow below link to see more JSON issues solutions:

JSON Issues Solutions

References

https://fasterxml.github.io/jackson-core/javadoc/2.2.0/com/fasterxml/jackson/core/JsonParseException.html