Main Tutorials

Jackson : was expecting double-quote to start field name

A simple example to use Jackson to convert a JSON string to a Map.


  String json = "{name:\"mkyong\"}";
		
  Map<String,String> map = new HashMap<String,String>();
  ObjectMapper mapper = new ObjectMapper();
		
  map = mapper.readValue(json, new TypeReference<HashMap<String,String>>(){});

Problem

When the program is executed, it hits following error message


org.codehaus.jackson.JsonParseException: 
   Unexpected character ('n' (code 110)): was expecting double-quote to start field name
   at [Source: java.io.StringReader@7c87c24a; line: 1, column: 3]
 

Solution

In JSON specification, it requires the use of double quotes for field names. To enable Jackson to handle the unquoted field name, add JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES in the ObjectMapper configuration.


package com.mkyong.jsonexample;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JsonMapExample {
    public static void main(String[] args) {

	String json = "{name:\"mkyong\"}";
		
	Map<String,String> map = new HashMap<String,String>();
	ObjectMapper mapper = new ObjectMapper();
		
	mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		
	try {

		map = mapper.readValue(json, 
                         new TypeReference<HashMap<String,String>>(){});	
		System.out.println(map);
			
	} catch (JsonParseException e) {
		e.printStackTrace();
	} catch (JsonMappingException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}	
		
  }
}

Output


{name=mkyong}
Note
In JSON, unquoted field names are non-standard, and should be avoided.

References

  1. Wikipedia : JSON
  2. JsonParser.Feature JavaDoc

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
10 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Kevin Thanki
7 years ago

not working for me as well, getting the following exception.
com.fasterxml.jackson.core.JsonParseException: Unexpected character (‘f’ (code 102)): was expecting double-quote to start field name

Johana
9 years ago

It doesn’t work for me 🙁

Ibsr
4 years ago

What is FEATURE in JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES?

srini
5 years ago

When I am deserialising the given String {“name”: “123\g”, “data”: “Manual”} I am getting this com.fasterxml.jackson.databind.JsonMappingException: Unrecognized character escape ‘g’ (code 103) but I need in this way only {“name”: “123\g”, “data”: “Manual”} Can any one help me here thanks in advance

Jack Donalds
5 years ago

and how about this invalid json:
[,
{“id”:4},{“id”:5}
]

can you help to convert to array?

Abdelaziz Khabthani
7 years ago

Thanks, you saved the day.

shohel khatri
7 years ago

This really saved my day, cheers!

Amine
9 years ago

Excellent, thank your for the help and the explanation !

Abraão Isvi
10 years ago

Interesting, thanks for sharing.

alex
4 years ago

nice support !!good guy!!!!!