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.

4
  • 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 - 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. Commented Mar 16, 2021 at 3:04
  • If it is in invalid json format, you can not map using json library. You have to write your own mapper for that. Commented Mar 16, 2021 at 3: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. Commented Mar 16, 2021 at 3:13
  • @CryptoFool hi. I solve the problem this link how-to-convert-string-into-hashmap-in-java but your comment is very useful. thank you. Commented Mar 16, 2021 at 5:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.