1

I have got java string(containing json object)

{"Attribute_1":""test"","Attribute_2":"100"}

When I covert to java object MyCustomClass i get runtime error because of double quotes around test . I am not sure how to escape double quotes inside json attribute value

ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(requestData, MyCustomClass.class);

Error is

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('t' (code 116)): was expecting comma to separate OBJECT entries
 at [Source: {"Attribute_1":""test"","Attribute_2":"100"} line: 1, column: 12]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:508)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:437)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._skipComma(ReaderBasedJsonParser.java:1795)

This is not a duplicate of How to escape double quotes in JSON by an chance as that is display issue

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
user3198603
  • 5,528
  • 13
  • 65
  • 125
  • 6
    You escape it with backslash. `{"Attribute_1":"\"test\""`. If some webservice is giving you a json like that it should be corrected, the JSON is invalid. – BackSlash Oct 02 '17 at 12:22
  • Should `Attribute_1`'s value really be `"test"` rather than `test` ? – Aaron Oct 02 '17 at 12:24
  • Anyway the problem is that when it has reached `{"Attribute_1":""`, the parser thinks it has read a key-value pair and complains that what follows isn't either a `,` or a `}` – Aaron Oct 02 '17 at 12:25
  • @BackSlash From ui this is getting passed as only `{"Attribute_1":"\"test\""`. I am not getting how to escape only json values in java ? – user3198603 Oct 02 '17 at 12:25
  • I already tried `requestData=requestData.replaceAll("\"", "\\\"");` but it did not work – user3198603 Oct 02 '17 at 12:26
  • 1
    How do you retrieve that `String` ? because you can't just guess which quotes should be escaped at this point, it has to be fixed upstream. As @BackSlash mentioned this isn't valid JSON. – Aaron Oct 02 '17 at 12:27
  • @Aaron I am passing it from UI/html. On HTML console i see value being passed as`{"Attribute_1":"\"Test\"","Attribute_2":"1028"}`. Is it correct ? – user3198603 Oct 02 '17 at 17:58
  • @Aaron I am passing it from UI/html. On HTML console i see value being passed as{"Attribute_1":"\"Test\"","Attribute_2":"1028"}. Which seems to be valid json per https://jsonlint.com/ . So what is wrong here ? – user3198603 Oct 02 '17 at 18:09
  • Yeah the `String` `{"Attribute_1":"\"Test\"","Attribute_2":"1028"}` is valid JSON. Note that to represent this string in java code [you should write `"{\"Attribute_1\":\"\\\"Test\\\"\",\"Attribute_2\":\"1028\"}"`](https://ideone.com/BsWkXQ). What do you mean "passing it from UI/html" ? copy/pasting from a website? If it's a framework please provide a link, with such a name it's an hassle to search on the web. – Aaron Oct 02 '17 at 18:38

1 Answers1

0

You are escaping it incorrectly. The correct way is that you have to provide the json accordingly, so the compiler can escape the "" for json value. Write the json String as:

String json = "{\"c\" : \"Hello \"Test\" there is a problem\"}";

Sample Test Code:

public static void main(String[] args) throws Exception {
    TypeReference<HashMap<String, String>> tr = new TypeReference<HashMap<String, String>>() {
    };
    String json = "{\"c\" : \"Hello \"Test\" there is a problem\"}";
    System.out.println(new ObjectMapper().readValue(json, tr));
}
Gaurav Tyagi
  • 641
  • 7
  • 7