How to escape double quotes inside a JSON given the data in the JSON will be obtained dynamically in Java.
Example:
{
"key": "I ask silly questions on "https://www.stackoverflow.com". "
}
In this case, the value in the JSON is populated from dynamically let's say from a user input.
I have tried StringEscapeUtils provided by apache-commons-lang (https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html), but this escapes the entire JSON where as the double quotes inside the JSON needs to escape again.
Possible solution will be to use regex and filter out the value in JSON and escape once before escaping the overall JSON.
But, is there any JAR to escape the inner contents of a JSON?
If doing json processing in java I would use Jackson databind and let it handle json generation. You will probably want to turn some knobs eventually, but simplest possible usage will probably do for this use case:
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(singletonMap("key", "I ask silly questions..."));
Related
I am using GSON to convert a stringified JSON object into a POJO I have defined. The JSON is very large but the POJO is a small subset of those properties. The original JSON has some properties whose values themselves are stringified JSON objects, so you have something like
{
"someAttribute": "{\"foo\": [{\"bar\"}]}"
}
This is a very simplified example. The actual JSON is much more nested and much larger.
Now when testing, I tried passing in a stringified version of the large json using JSON.stringify from my browser, which resulted in all \" sequences becoming \\". I the subsequently pasted the string into IntelliJ, which then converted the \\" sequences to \\\\\". When GSON tried to serialize this, it threw a malformed JSON exception.
Although I believe this to be an intellij issue of adding extra escape characters (once I changed \\\\\" to \\\" it worked fine), It occurred to me that if my function were to receive improperly escaped JSON due to possible differences in stringification, GSON would throw an exception. I don't know how any serialization package would know what is the correct or incorrect number of escape characters, but given that the actual JSON structure itself is not malformed, what can I do to avoid "false positive" JSON serialization issues?
Gson will deserialize non-conforming single quoted JSON perfectly. Unfortunately I need to serialize a random interior object that was passed in with single quotes instead of double quotes. The output should look like the object below - see the key "second":
{
"first":{"normal":1}
"second":{'single_quoted': 'random object'}
}
Assume I already have the "second" object as a JsonObject. I know there are ways to customize the serializers, but I haven't found what I'm looking for.
My application receives an encrypted json from front end, I decrypt it on backend but it is formatted like this:
"{\"firstname\":\"JOHN\",\"lastname\":\"DOE\"}"
yes, there are leading and trailing double quotes on the decrypted json.
GSON and object mapper doesnt work since they detect the quotes on the first character.
Is there way to properly convert this without manually removing the enclosing quotes?
It looks like your data has been double-encoded, so the solution is to double-decode. Specifically: thanks to the backslashes, this is actually a valid JSON string value that represents a stringified JSON object. So if you ask your JSON library to decode this into a String, you should get a string whose value is
{"firstname":"JOHN","lastname":"DOE"}
And then you can ask your JSON library to decode that string into the type you actually want.
I would like to output json as a string with double quotes surrounding it and all double quotes inside the string escaped. Does Jackson Json provide any function to do it? What I want to do is exactly opposite of How to parse a JSON string into JsonNode in Jackson?.
Hm, I think doing that would make your output a string (formatted like JSON), as opposed to valid JSON.
I tested it with JSONLint, worth having a go yourself just in case I'm being a dunce.
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.