Spring escapes already escaped string - java

So I have this string that I am saving in database:
{\"facebook\":\"fb.com\",\"twitter\":\"twitter.com\",\"instagram\":\"\",\"googlePlus\":\"\",\"others\":\"espn.com\"}
But when I call the GET api, I get this in JSON
{\\\"facebook\\\":\\\"fb.com\\\",\\\"twitter\\\":\\\"twitter.com\\\",\\\"instagram\\\":\\\"\\\",\\\"googlePlus\\\":\\\"\\\",\\\"others\\\":\\\"espn.com\\\"}
Why is this happening and how can I get the exact same data that is stored in database?

It is escaped again when you're retrieving the data because Spring thinks that the \ character is part of the data and not used to escape ".
You never want to store escaped characters (be it for JSON special characters, HTML characters in a text, ...), you have to store unescaped data to fix your issue. Escaping has to be done when displaying data, not when storing it.
It is a bad practice to store escaped data because of the problem you're having but also because it will take useless storage space in the database (which might not be a problem right now for you, but will be with millions of rows).

You could also use apache's build in StringEscapeUtils.unescapeJson(String input) mechanism for Json data. See reference https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/apache/commons/lang3/StringEscapeUtils.html

After getting the data, save it in a String and then :
String newJava = str.replace("\\\", "\");

Related

How to insert a value that contains an apostrophe (single quote) in postgresql from java insert?

I have data coming from html contact form which i am sending in a email through a java webservice. Everything is working fine but when user inserts data containing apostrophe (single quote) insert fails.
Pls help me to understand How and where to handle this data.
TIA
You just need to use Prepared Statements,it will escape special characters and avoid SQL_injection
You can use Prepared Statements, and you can just escape all special characters like single quote, double quote etc. before inserting in the database.

Storing Unicode and special characters in MySQL tables

My current requirement is to store Unicode and other special characters, such as double quotes in MySQL tables. For that purpose, as many have suggested, we should use Apache's StringEscapeUtils.escapeJava() method. The problem is, although this method does replace special characters with their respective unicodes (\uxxxx), the MySQL table stores them as uxxxx and not \uxxxx. Due to this, when I try to decode it while fetching from the database, StringEscapeUtils.unescapeJava() fails (since it cannot find the '\').
Here are my questions:
Why is it happening (that is, '\' are skipped by the table).
What is the solution for this?
Don't use Unicode "codepoints" (\uxxxx), use UTF8.
Dont' use any special functions. Instead announce that everything is UTF-8 (utf8mb4 in MySQL).
See Best Practice
(If you are being provided \uxxxx, then you are stuck with converting to utf8 first. If your real question is on how to convert, then ask it that way.)
`

Error while parse an XML with cyrillic serbian chars anda save into DB

I've a complex XML file and I've to parse it with Java to get some text inside some tags.
This is done correctly, but there are some rows with cyrillic characters (serbian) and in XML appear in correct mode, when I get it with Java in another one, and when I save it into Oracle, in another one!
How I can elaborate and save this kind of data in the correct cyrillic format from xml to oracle? Thanks.
First: read http://www.joelonsoftware.com/articles/Unicode.html
Second: you don't get a "simple string", you have a file. Which contains bytes. That given an encoding represent a string. When you read it in as a string, you need to specify that encoding or things will get corrupted.
Once you have a java.lang.String, it is an actual unicode representation and encoding-independent but when you want to push that string to a database, you once again need to think about encoding because at some point somewhere, the database will have to transform that string to bytes to store it.
Additionally: never "trust" an editor when it comes to examining encoding issues. They almost always have automagic stuff to make stuff work so something that "looks fine" might actually be corrupt or only valid given the assumptions that that specific editor made.

Reading Json data containing special symbols like curly braces, square brackets etc

I have json data saved as a string in a database. The structure of JSON data is fine with the problem that it contains regex expressions and even urls which contains curry braces {} or square brackets [] etc. I can replace some of special symbols with the encodings available e.g hex or decimal encodings. and do string manipulations to take care of these. I was just wondering is there another way for handling this situation. I am getting following exception for the strings containing this type of Json data.
org.json.JSONException: Expected a ',' or '}' at character 22891 of {"wires":[{"id"....so on
Please let me know if I need to elaborate more.
Here's the first thing coming to my mind:
When putting the thing in the database try php addslashes/stripslashes (assuming you are using php to contact the database).

How to add a URL String in a JSON object

I need to add a URL typically in the format http:\somewebsite.com\somepage.asp.
When I create a string with the above URL and add it to JSON object json
using
json.put("url",urlstring);
it's appending an extra "\" and when I check the output it's like http:\\\\somewebsite.com\\somepage.asp
When I give the URL as http://somewebsite.com/somepage.asp
the json output is http:\/\/somewebsite.com\/somepage.asp
Can you help me to retrieve the URL as it is, please?
Thanks
Your JSON library automatically escapes characters like slashes. On the receiving end, you'll have to remove those backslashes by using a function like replace().
Here's an example:
string receivedUrlString = "http:\/\/somewebsite.com\/somepage.asp";<br />
string cleanedUrlString = receivedUrlString.replace('\', '');
cleanedUrlString should be "http://somewebsite.com/somepage.asp".
Hope this helps.
Reference: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace(char,%20char)
Tichodroma's answer has nailed it. You can solve the "problem" by storing valid URLs.
In addition, the JSON format requires that backslashes in strings are escaped with a second backslash. If the 2nd backslash is left out, the result is invalid JSON. Refer to the JSON syntax diagrams at http://www.json.org
The fact that the double backslashes are giving you problems actually means that the software that is reading the files is broken. A properly written JSON parser will automatically de-escape the strings. The site I linked to above lists many JSON parser libraries written in many languages. You should use one of these rather than trying to write the JSON parsing code yourself.

Categories