How to output json string with all qutation mark escaped? - java

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.

Related

How do I convert this JSON string enclosed in double quotes to object

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.

How to escape double quotes in a JSON in Java dynamically

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..."));

How to remove this double quote before curly braces from this json string

I found no particular solution for my json string. I want to remove the double quotes from both end of the jsonObject. For better understanding I am sharing you the screenshot.
Thanks in Advance
If your JSON string format is the same you can use the following code to get your desired result:
jsonString = jsonString.replaceAll("(\"\\{\")","\\{\"").replaceAll("(\"\\}\")","\"\\}");
It simply removes all the leading and trailing double quotes from every array element.
You can convert your string into JSON using the following code:
JSONObject jsonObject= new JSONObject(jsonString);

String to JSONObject conversion in Java

I need to convert
{"officeId":1,"clientId":97,"resourceId":97}
Please note that for values I don't have quotes. I have seen similar questions asked and answered, i haven't seen one that looks exactly like this one, i.e values have no quotes in the string to be converted.
Here's the original string from REST server
"{\"officeId\":1,\"clientId\":98,\"resourceId\":98}"
Are you getting an error? You shouldn't be because it's valid JSON. If you quote the numbers, they're now strings instead.
See Can JSON numbers be quoted.
A regular parse will suffice:
JSONObject jsonObj = new JSONObject("{\"officeId\":1,\"clientId\":98,\"resourceId\":98}");

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