1

My application is using camel rest (2.15.2) to catch a POST json String and then uses jettison to convert to a JSON Object. It is working fine with normal request.

POST request: {"request"={"lname"="aaa", "fname"="bb"}}

1. String body = exchange.getIn().getBody(String.class);
2. JSONObject obj = new JSONObject(body);

When i debug, variable body = {request={lname=aaa, fname=bb}}. And line 2 returns a JSONObject. so far so good

if we try to another the request:

{"request"={"lname"="   ", "fname"="aa"}}

then body = {request={lname= , fname=aa}} line2 returns Exception.

Could you please help me to fix this issue: convert json string which contains element has empty value string to json object.

The above request is acceptable in my scenarios.

Error:

org.codehaus.jettison.json.JSONException: Missing value. at character 15 of {request={lname= , fname=aa}} at org.codehaus.jettison.json.JSONTokener.syntaxError(JSONTokener.java:463) at org.codehaus.jettison.json.JSONTokener.nextValue(JSONTokener.java:356) at org.codehaus.jettison.json.JSONObject.(JSONObject.java:230) at org.codehaus.jettison.json.JSONTokener.newJSONObject(JSONTokener.java:412) at org.codehaus.jettison.json.JSONTokener.nextValue(JSONTokener.java:327) at org.codehaus.jettison.json.JSONObject.(JSONObject.java:230) at org.codehaus.jettison.json.JSONObject.(JSONObject.java:311)

Sushil
  • 2,837
  • 4
  • 21
  • 29
David
  • 3,538
  • 9
  • 39
  • 50
  • Well, something seems to be stripping your quotations and it seems to happen before the message gets on the exchange. So actually no problem with Jettison i would say? – J2B Aug 25 '15 at 13:00
  • Yes, I agree. Is there any way that jettison can work with that kind of request? – David Aug 25 '15 at 13:44
  • No, you should investigate why your quotations are stripped from the string – J2B Aug 26 '15 at 11:13
  • Camel-rest stripped off the double quote. Currently, to bypass this issue, i have to manually replace all "blank " by "double quote". But i still prefer a way that jettison can work with it Or camel-rest stops stripping off the double quote. Pattern.compile("=\\s*,").matcher(body).replaceAll("=\"\","); – David Aug 26 '15 at 15:34
  • Sounds very weird anyway. Jettison won't be able to handle it at least. And I don't know why they are getting stripped, probably because of how you are sending the data or some config of the restConfig? Try to debug it – J2B Aug 27 '15 at 13:09
  • gson and org.json do it by default. – Jugi Mar 15 '18 at 07:15

0 Answers0