I have a string that is in Json format, only none of the keys or values are surrounded by quotes. For example, I have this:
String json = "{name: Bob, state: Colorado, Friends: [{ name: Dan, age: 23 }, {name: Zane, age: 24 }]}"
I want this to become a map that looks like so:
Map<String, Object> friend1Map = new HashMap<>();
friend1Map.put("name", "Dan");
friend1Map.put("age", 23);
Map<String, Object> friend2Map = new Hashmap<>();
friend2Map.put("name", "Zane");
friend2Map.put("age", 24);
Map<String, Object> newMap = new HashMap<>();
newMap.put("name", "Bob");
newMap.put("state", "Colorado");
newMap.put("Friends", Arrays.asList(friend1Map, friend2Map));
I have tried the following two methods:
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
This will throw an error, saying:
Unexpected character ('n'): was expecting double-quote to start field name
Then I tried changing the config of mapper:
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
But this threw an error saying:
com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Bob': was expecting ('true', 'false' or 'null')
at [Source: {name: Bob, state: Colorado, Friends: [{ name: Dan, age: 23 }, {name: Zane, age: 24 }]}; line: 1, column: 11]
Is there a way of getting this Map when quotes aren't included in the json string?
→ JSON is basically the “String” version of JavaScript Object. Its simply a text-based data format and single quote is not allowed for JSON.
We can easily convert JSON data into a map because the JSON format is essentially a key-value pair grouping and the map also stores data in key-value pairs. Let's understand how we can use both JACKSON and Gson libraries to convert JSON data into a Map.
replace(/"/g, '');
JSON names require double quotes.
ObjectMapper with jackson fasterxml doesn't support values without quotes, but GSON does:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonParser;
.
JsonNode json = json("{"
+ " name: Bob, "
+ " state: Colorado,"
+ " Friends: [ "
+ " {"
+ " name: Dan, "
+ " age: 23 "
+ " },"
+ " {"
+ " name: Zane, "
+ " age: 24 "
+ " }"
+ " ],"
+ " extra: \"text with spaces or colon(:) must be quoted\""
+ "}");
Map m = new ObjectMapper().convertValue(json, Map.class);
.
JsonNode json(String content) throws IOException {
String canonicalFormat = JsonParser.parseString(content).toString();
return json.readTree(canonicalFormat);
}
Before v2.8.6 GSON didn't have the static parseString method. So you should use the (deprecated in higher versions) instance method:
JsonNode json(String content) throws IOException {
String canonicalFormat = new JsonParser().parse(content).toString();
return json.readTree(canonicalFormat);
}
NOTE: we expect Java 15 will support unescaped double-quotes as is:
var json = """
{"name": "Bob", "state": "Colorado", "Friends": [{ "name": "Dan", "age": 23 }, {"name": "Zane", "age": 24 }]} """;
more details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With