0

I want to invalid json string convert to map on java.
target string like this. (without quotes on key)
{depth=2, url=google.com}

i try this

ObjectMapper mapper = new ObjectMapper();
String dataJson = ABOVE_INVALID_JSON_STRING;
Map<String, String> dataMap = mapper.readValue(dataJson, Map.class);

error

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('d' (code 100)): was expecting double-quote to start field name
 at [Source: (String)"{depth=2, url=...; line: 1, column: 3]

i know that string is invalid json. but i think that easy way exist what i don't know.

palbok
  • 48
  • 6
  • Welcome to Stack Overflow! My experience is that you can get Jackson to handle map keys that are not quoted, but not map values. I found an existing S.O. answer that seems to back this up - [how to parse unquoted JSON string](https://stackoverflow.com/questions/15005185/how-to-parse-unquoted-json-string) - I think you're generally only going to be able to go so far in terms of what sort of invalid JSON you can expect to have parsed by a JSON parser. After all, if it isn't JSON, it isn't JSON. – CryptoFool Mar 16 '21 at 03:04
  • If it is in invalid json format, you can not map using json library. You have to write your own mapper for that. – janith1024 Mar 16 '21 at 03:08
  • 1
    @janith1024 - per my comment, what you say isn't strictly true. Some languages will print structures that are almost JSON, only failing to quote key values. The Jackson JSON parser allows for this. Check out the link in my comment, which talks about Jackson's `JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES` flag that allows for this. – CryptoFool Mar 16 '21 at 03:13
  • @CryptoFool hi. I solve the problem this link [how-to-convert-string-into-hashmap-in-java](https://stackoverflow.com/questions/26485964/how-to-convert-string-into-hashmap-in-java) but your comment is very useful. thank you. – palbok Mar 16 '21 at 05:52

0 Answers0