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>>(){});


When the program is executed, it hits following error message

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


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>>(){});	
	} catch (JsonParseException e) {
	} catch (JsonMappingException e) {
	} catch (IOException e) {


In JSON, unquoted field names are non-standard, and should be avoided.


  1. Wikipedia : JSON
  2. JsonParser.Feature JavaDoc
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website mkyong.com is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts