Json string handling with back slash - java

I know this question was asked several times before on this forum. But none of it addresses my problem.
I am using Jmeter to post json string with content-type "application/json"
My Spring mvc app is happy consuming the same and converting it to Java Object.
However, when i send a string with a backslash everything seems to fall apart.
Here is my Json Input string
{"signup":{"firstName":"joe", "lastName":"beedu","email":"jb1407874596956#hotmail.com",
"password":"O;\l-wslD6RQ5#M)","confirmPassword":"O;\l-wslD6RQ5#M)",
"picImage":[81,109,81,109,109,81,109,109,89]}}
Note: password fields have a back slash. More on this later.
Here is my java class for the same.
#JsonRootName(value = "signup")
#XmlRootElement(name = "signup")
public class Signup {
private String firstName;
private String lastName;
private String email;
private String password;
private String confirmPassword;
private byte[] picImage;
}
Removed getters/setters and also validation annotations.
Issue number 1: When i manually escape the password field values with an extra slash like
"O;\l-wslD6RQ5#M)" My Spring mvc web app is happy, but doesn't automatically unescape the extra back slash for me. I was expecting my spring converter MappingJackson2HttpMessageConverter to read the json payload and do the magic unescape thing for me, but it doesn't. It will be very ugly if i manually unescape every field in json json objects. Any help here to address this?
Issue number 2: On the client side, i tried several different ways to see if any library automatically escapes the strings in Json object. Given a raw json string like above, is there a way to escape the string that goes into json object? I tried, StringEscapeUtils from apache escapeJson, it didn't work. I tried json-lib too. None of them were able to handle
string "O;\l-wslD6RQ5#M)". I had to manually add extra back slash before i add the json string to Http Body.
I would like to use a client side library that escapes json string fields and also on the server side to unescape. I deeply appreciate you guys for reading a lengthy question. I wasted a day on this one. I hope you guys will help me out with an answer or at least a direction.
Thanks
Sri

Related

Datadog string encoding when character is not from English alphabet

I have a service and send Datadog events from it using com.github.arnabk.java-dogstatsd-client. In order to send json string I use JsonObject where I put all properties which I need then convert it to string using toString() method on JsonObject and send string as a message body. Everything works perfect unless I have a character in a string which is not from english alphabet. Example: µ. In this case instead of having correct json {"Smth":"µ"} in Datadog I'm getting incorrect string without closing curly brace {"Smth":"µ". Has anybody experienced the same and knows how to deal with this?

Json Parser not working on request mapping

Problem:
I am unable to parse json request to an object containing double quotes in it.
For example:
jsonString = {
"desc":"Hello stackOverFlow, please reach on this email "asdas#gmail.com". thanks";
}
When I am trying to convert this I am not able parse to an variable, because it looks like invalid json but in real time the request has double quotes in it.
Please show me some good parsing techniques which can do parse this type of requests.
Thanks
You have to escape all of the double quotes, so for example:
String json = "\"{\"desc\":\"Hello stackOverFlow, please reach on this email \"asdas#gmail.com\". thanks\"}";
As you can see it is a lot of work to create very simple JSON, so you are better of using some library for that. I recommend you org.json, it is very lightweight and easy to use. Using it, it would look like this:
JSONObject json = new JSONObject();
json.put("description", "Your description....");
String jsonString = json.toString();

How to send JSON as a parameter in url using Spring RestTemplate?

I am trying to achieve same thing as this: How to use query parameter represented as JSON with Spring RestTemplate?, sending JSON string as a URL parameter in restTemplate.exchange().
The accepted answer mentions that sending JSON object as request parameter is generally not a good idea since you will probably face problem with curly brackets inside JSON. That is exactly what is happening when I am trying to make a GET call to an API. Since this is an API from another system, I cannot ask them to change the format and will have to call the GET endpoint, passing JSON as parameter. How can I achieve this in restTemplate.exchange() call?
Note: The mentioned related question does not guide on how to overcome this problem and I do not have enough reputation to comment on it to ask the author of the answer.
Answering my own question. While it is a bad idea to pass JSON like this in a query/url parameter, there is a workaround as suggested here: https://jira.spring.io/browse/SPR-9220?focusedCommentId=76760&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-76760.
Replicating the code here in case this link goes dead:
String url = "http://localhost:8983/solr/select?wt=json&indent=true&fl=*&q=*:*&fq={!geofilt}&sfield=venue_location&pt=28.0674,-80.5595&d=25";
URI uri = UriComponentsBuilder.fromUriString(url).build().encode().toUri();
System.out.println(uri);
// http://localhost:8983/solr/select?wt=json&indent=true&fl=*&q=*:*&fq=%7B!geofilt%7D&sfield=venue_location&pt=28.0674,-80.5595&d=25
Basically, instead of passing url having JSON query/url parameters as a string, pass it as a URI. Then call exchange method as before, but with URI instead of String:
restTemplate.exchange(uri, HttpMethod.GET, requestEntity, String.class)
If this is 3rd party API and you cannot control or change JSON processing on backend side - there is no solution. Even if you will encode with URLEncoder - there is no guarantee that API backend would process such request correctly.
You can use URLEncoder class to encode the URL in exchange method, e.g.:
String url = "http://www.yoursite.com/api?param={\"some_key\":\"some_value\"}";
System.out.println(URLEncoder.encode(url, StandardCharsets.UTF_8.name()));
This will encode the characters (like braces and double quotes) and server then will decode it back to json.

How would I split this string?

How would I go about cutting this giant string? I need the data of the first ID," preferably for the string output to be:
"id":1,"name":"site1","has_doneit":true,"destination":"4613"
Is this possible? Or if not any other ways of grabbing the name:"site1" and the has_doneit:true would be perfectly fine.
{"needs_complete":true,"has_done":true,"sites":[
{"id":1,"name":"site1","has_doneit":true,"destination":"4613"},{"id":2,"name":"site2","has_doneit":true,"destination":"4613"},{"id":3,"name":"site3","has_doneit":true,"destination":"4339"},{"id":4,"name":"site4","has_doneit":true,"destination":"4340"},{"id":5,"name":"site5","has_doneit":true,"destination":"4341"},
{"id":6,"name":"site6","has_doneit":true,"destination":"4622"},{"id":7,"name":"site7","has_doneit":true,"destination":"4623"},{"id":8,"name":"site8","has_doneit":true,"destination":"4828"},
{"id":9,"name":"site9","has_doneit":true,"destination":"4829"},{"id":10,"name":"site10","has_doneit":true,"destination":"4861"}]}
That seems like a JSON string so use a Json Parser...
PHP: JSON_decode
$parsedStr = json_decode($yourString, true);
you can access sites array in $parsedStr['sites']
So, to access the id of the first site:
echo $parsedStr['sites'][0]['id'];
Java
check this answer in SO Decoding JSON String in Java

Java Spring: Web Service REST producing double backslash at JSON

So i have this String e.g.
"B\z#b#s#rB{FpMgBrD~DK|"
But Spring is producing this at json:
"B\\z#b#s#rB{FpMgBrD~DK|"
And it's the same with any string that has a backslash \. Replacing the character before generating the json is useless, since the String is correct before the json is generated.
Every client that consumes the service could .replace("\\","\\\\") the json, but I wonder if there will be a cleaner way to solve the problema and at server side.
Here is the code for the WS:
#RequestMapping(value = "/rest/sinc/{ms}", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
#ResponseBody
public String sincronizar(#PathVariable("ms") Long ms) {
return sincService.getSinc(ms).toString();
}
I have tried other ways to generate the json with libraries like Gson but the result is the same.
That is the expected behavior in JSON. \ needs to be escaped. See the specification, here.
All Unicode characters may be placed within the quotation marks,
except for the characters that must be escaped: quotation mark,
reverse solidus, and the control characters (U+0000 through U+001F).

Categories

Resources