Home Java How to Convert String to JSON Object in Java

    How to Convert String to JSON Object in Java

    Nowadays, it’s very common to receive JSON string from a Java web service instead of XML, but unfortunately, JDK (Java Development Kit) doesn’t still support conversion between  String to JSON object. However, keeping JSON as String is not always a good option as you cannot operate on it readily, there is a need to convert it into JSON object before doing anything else, as an example, retrieve any field or set different values. 

    Must Read: Kotlin or Java? Which One is Better for Android App Development

    Digging Deeper:

    The good news is that you can find several open-source libraries that allows you to create JSON object from JSON (JavaScript Object Notation) formatted String, for instance:

    • Gson from Google
    • Jackson
    • JSON-Simple

    Well, give a read to this article to know how to use these above three main libraries to do String to JSON conversions!

    So, Convert String to JSON Object in Java:

    Swipe down!

    String to JSON Object using Gson:

    The Gson is said to be an open-source library to deal with JSON online in Java programs. Yes, Gson comes from none other than Google that is also behind Guava, (for Java programmers, it is a common purpose library). You can readily convert JSON String to JSON object in just 2 lines by using Gson as mentioned below:

    Gson g = new Gson();

    Player p = g.fromJson(jsonString, Player.class)

    Also, you can convert Java object to JSON by using toJson() method as mentioned below:

    String str = g.toJson(p);

    JSON String to JSON Java object using JSON-Simple:

    The JSON-simple is said to be another open-source library that supports JSON parsing and formatting. The amazing thing about this open-source library is its small size that is splendid for memory constraint environments like J2ME and Android.

    JSONParser parser = new JSONParser();

    JSONObject json = (JSONObject) parser.parse(stringToParse);

    Also, another good thing about this JSON-simple that it is JDK (Java Development Kit) 1.2 compatible, which means you, can readily utilize it on a legacy project which is not yet in Java 5.

    String to JSON – Jackson Example:

    Jackson is the most well-known JSON parsing library in JAVA world. It is fully packed with fast, feature-rich, and supports streaming that makes its great for parsing a large JSON output from web services. Well, the mentioned one linear JSON string indicating a soccer player into a Java class representing player:

    Player ronaldo = new ObjectMapper().readValue(jsonString, Player.class);

    One of the major drawbacks of Jackson is that it requires JDK (Java Development Kit) 1.5 so it on you stick within earlier Java version than it may not fit there. Also, remember that Jackson doesn’t support J2ME, but it supports streaming works great to parse huge JSON response without loading it fully in memory.


    Thankfully, you come to know how to convert String to JSON object in Java! However, you can account any of the JSON-simple, Gson, or Jackson simple for parsing JSON messages that you are received from web services, each of them has their own advantage. In fact, JSON-simple contains a small memory footprint, which means that it’s quite suitable for J2ME and Android client, on the other hand, Jackson is loaded with feature-rich, means so better supported for a large project. Finally, Gson is between them and one of the highly recommended JSON parser in Java programs. 

    Editor's Pick

    Flutter vs Android Studio: Which is Better?

    Here begins the brief assessment of Flutter vs Android Studio. Flutter UI development kit developed by Google. This Flutter framework is...

    Cybersecurity 101 – How to Spot the Most Common Types of Malware

    The internet is a marvelous invention, but just as any great thing in life, it can often be a double-edged sword. You...

    10 Efficient Link Building Tactics for Ecommerce Stores

    Good organic ranking in search engines is essential for any eCommerce business, and link building is a powerful method to achieve it....

    11 Best Video Presentation Softwares To Up Your Presentation Game

    Have you got a major presentation coming up? Wish to attract the attention of your audience? The best way to ensure that...

    Top 3 Cameras For Recording Podcasts in 2020

    Video podcasting is a new and effective way of sharing, and airing your opinions. The platform gives a wide range of advantages...
    How to Convert String to JSON Object in Java 2
    Vaibhav Jain
    Vaibhav is a specialist in Website Development whose goal is to never write a boring article. His enthusiasm for online business has spanned over two years, accompanying his articles with thorough knowledge about Website Development and many other topics. He also enjoys playing Cricket and is a Fitness enthusiast.
    Notify of
    Inline Feedbacks
    View all comments
    Would love your thoughts, please comment.x