How to Escape JSON String in Java – Eclipse IDE Tips
While working or JSON parsing in Java application it’s quite common to just copy paste a JSON String from some resources e.g. a RESTful web service and then use the Jackson library to parse the JSON. This is the quickest way to test and learn parsing JSON string in Java, but the main problem with this approach is that the JSON String also includes double quotes “” which is also used for enclosing String literals in Java. Since JSON String itself contains double quotes, when you just copy paste them in your IDE or Java source file, the didn’t work as expected. If you remember, if your String contains double quotes then those need to be escaped when you are pasting them as String literals in your Java code.
You can escape String in Java by putting a backslash in double quotes e.g.” can be escaped as\” if it occurs inside String itself. This is ok for a small JSON String but manually replacing each double quotes with escape character for even a medium size JSON is time taking, boring, and error-prone. So If you have a JSON String which you need to parse in Java using Jackson, what will you do?
Well, there are multiple solution e.g. you can save that String in a file and then read the file in your program instead of copy pasting String into your Java code, or, you can directly download from the internet by using HttpConnection utility of Apache Commons, though this required additional dependency in your Java application and may slow down your testing.
You can even use text editors like Notepad++, EditPlus, or Sublime (See my Java tools) which supports regex searching to replace all double quotes with escape characters before copying the String literal into Java code, but this is again error prone if your JSON string contains line breaks and new line because those needs to escaped manually.
How to Escape JSON String in Java Source Code using Eclipse
The quickest and the best way I found is to leverage Eclipse’s capability to Escape text when pasting into a String literal. This feature can be enabled from a setting and then Eclipse will escape all double quotes, line breaks, new lines or any other character which need escaping automatically. This can save you tons of time and can speed up your learning and testing.
Here are the exact steps to enable this String escape setting into Eclipse IDE:
1. Open Eclipse IDE
2. Go to Windows –> Preferences –> Java –> Editor –> Typing
3) Check the checkbox “Escape text when pasting into a String literal” on section “In String literals.
Here is how your settings should look like in Eclipse IDE:
Once you did this, Eclipse will escape String if it contains any characters which need escaping. Let’s see an example now.
Suppose this is your JSON String:
{ "name": "John", "email": "john.doe@gmail.com", "age": 29, "phone" : 5168161922, "city" : "NewYork", "hasCreditCard": false }
Here all those double quotes around attributes need to be escaped but when you paste this JSON String in Eclipse, it will automatically escape those for you as shown below:
You can see how Eclipse has escaped the JSON easily. This is an awesome feature of Eclipse IDE and you should enable it if you are working with JSON in Java.
That’s all about how to escape JSON String in Java using Eclipse IDE. This is a real time saver tip which you will appreciate every time you need to paste a JSON String into Java code. The Eclipse IDE is full of such gems and but many of us didn’t use Eclipse up to its full potential. Many programmers don’t even know essential Eclipse shortcuts and debugging tips, which can again save a lot of time while reading, writing, or troubleshooting Java code.
Other JSON articles you may like to explore
- How to convert JSON array to String array in Java? (answer)
- How to use Google Protocol Buffer in Java? (tutorial)
- How to parse large JSON file using Jackson Streaming API? (example)
- 5 Books to Learn REST and RESTful Web Services (books)
- What is the purpose of different HTTP methods in REST? (see here)
- How to consume JSON from RESTful Web Services using RestTemplate of Spring? (tutorial)
- How to convert JSON to HashMap and vice-versa (tutorial)
Further Learning
- REST with Spring Course
- Core Java Volume 2 – Advanced Features
- RESTful Web Service Interview Questions
Thanks for reading this far. If you like this JSON Eclipse tip then please share with your friends and colleagues. If you have any question or feedback or you want to share any other Eclipse tips with us then please drop a comment.
Reference: | How to Escape JSON String in Java – Eclipse IDE Tips from our JCG partner Javin Paul at the Javarevisited blog. |