Configure Jackson to deserialize single quoted (invalid) JSON

This is the way it works in my case:

var jsonString ='{"it":"Stati Uniti d'America"}';
jsonString =jsonString.replace("'", "\\\\u0027");

It's not valid JSON, but you can tell Jackson to allow it. Here's how.

String x = "{'candidateId':'k','candEducationId':1,'activitiesSocieties':'Activities for cand1'}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
JsonNode df = mapper.readValue(x, JsonNode.class);
System.out.println(df.toString());
// output: {"candidateId":"k","candEducationId":1,"activitiesSocieties":"Activities for cand1"}

Strings in JSON may only be specified using double quotes ("), not single quotes ('), this is the reason for your error; use double quotes.

Here's the pipe diagram that specifies valid JSON strings (note they may only be encapsulated with double quotes!)

Valid JSON Strings Pipe Diagram
(source: json.org)

(See json.org for a complete specification of JSON.)

Tags:

Java

Jackson