Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
json
jackson
gson
communitycreator

Different ways to convert a JSON string to a JSON object in Java

abhilash

In this shot, we will cover different ways to convert a JSON string to a JSON object in Java.

Let’s generate a random JSON with the help of a JSON generator.

{
    "_id": "61055ba08cf4754d7ba5d191",
    "index": 6,
    "guid": "f74a0878-0dc5-47a4-a7cf-c7ff806765af",
    "isActive": false,
    "balance": "$3,211.08",
    "picture": "http://placehold.it/32x32",
    "age": 31,
    "eyeColor": "brown",
    "name": "Estrada Martinez",
    "gender": "male",
    "company": "QUIZKA",
    "email": "estradamartinez@quizka.com",
    "phone": "+1 (896) 583-3530",
    "address": "265 Williams Court, Manila, Wisconsin, 5703",
    "about": "Nulla consequat duis minim velit nisi elit deserunt eu sint. In nisi et proident ullamco velit. Amet consequat irure tempor non consequat ad consequat non reprehenderit fugiat nostrud sit. Ex eu irure nostrud fugiat exercitation esse commodo ad id magna id consequat eiusmod.\r\n",
    "registered": "2018-09-17T04:47:34 -06:-30",
    "latitude": -74.60479,
    "longitude": 34.229968,
    "tags": [
        "non",
        "nulla",
        "nostrud",
        "occaecat",
        "eiusmod",
        "commodo",
        "elit"
    ],
    "friends": [
        {
            "id": 0,
            "name": "Raquel James"
        },
        {
            "id": 1,
            "name": "Katina Walsh"
        },
        {
            "id": 2,
            "name": "Leah Guerrero"
        }
    ],
    "greeting": "Hello, Estrada Martinez! You have 3 unread messages.",
    "favoriteFruit": "apple"
}

Code

Jackson

Jackson is a popular and powerful library in Java that is used to deal with JSON data.

To use the Jackson library, add the Jackson databind dependency from Maven Repository.

A node in the JSON is represented in the form of JsonNode in Jackson. Hence, if you convert a string JSON to a JSON object, it would return a JsonNode as the root node.

ObjectMapper objectMapper = new ObjectMapper();
String jsonString = "{\"_id\":\"61055ba08cf4754d7ba5d191\",\"index\":6,\"guid\":\"f74a0878-0dc5-47a4-a7cf-c7ff806765af\",\"isActive\":false,\"balance\":\"$3,211.08\",\"picture\":\"http://placehold.it/32x32\",\"age\":31,\"eyeColor\":\"brown\",\"name\":\"Estrada Martinez\",\"gender\":\"male\",\"company\":\"QUIZKA\",\"email\":\"estradamartinez@quizka.com\",\"phone\":\"+1 (896) 583-3530\",\"address\":\"265 Williams Court, Manila, Wisconsin, 5703\",\"about\":\"Nulla consequat duis minim velit nisi elit deserunt eu sint. In nisi et proident ullamco velit. Amet consequat irure tempor non consequat ad consequat non reprehenderit fugiat nostrud sit. Ex eu irure nostrud fugiat exercitation esse commodo ad id magna id consequat eiusmod.\\r\\n\",\"registered\":\"2018-09-17T04:47:34 -06:-30\",\"latitude\":-74.60479,\"longitude\":34.229968,\"tags\":[\"non\",\"nulla\",\"nostrud\",\"occaecat\",\"eiusmod\",\"commodo\",\"elit\"],\"friends\":[{\"id\":0,\"name\":\"Raquel James\"},{\"id\":1,\"name\":\"Katina Walsh\"},{\"id\":2,\"name\":\"Leah Guerrero\"}],\"greeting\":\"Hello, Estrada Martinez! You have 3 unread messages.\",\"favoriteFruit\":\"apple\"}";
JsonNode root = objectMapper.readTree(jsonString);
System.out.println(root);

Json Simple

Json Simple is another library that provides basic functionalities as far as JSON processing is concerned.

To use the JSON simple library, add the JSON-simple dependency from Maven Repository.

If you convert a string JSON to a JSON object, it would return a JSONObject, which can also be used to manipulate the data in the JSON.

String jsonString = "{\"_id\":\"61055ba08cf4754d7ba5d191\",\"index\":6,\"guid\":\"f74a0878-0dc5-47a4-a7cf-c7ff806765af\",\"isActive\":false,\"balance\":\"$3,211.08\",\"picture\":\"http://placehold.it/32x32\",\"age\":31,\"eyeColor\":\"brown\",\"name\":\"Estrada Martinez\",\"gender\":\"male\",\"company\":\"QUIZKA\",\"email\":\"estradamartinez@quizka.com\",\"phone\":\"+1 (896) 583-3530\",\"address\":\"265 Williams Court, Manila, Wisconsin, 5703\",\"about\":\"Nulla consequat duis minim velit nisi elit deserunt eu sint. In nisi et proident ullamco velit. Amet consequat irure tempor non consequat ad consequat non reprehenderit fugiat nostrud sit. Ex eu irure nostrud fugiat exercitation esse commodo ad id magna id consequat eiusmod.\\r\\n\",\"registered\":\"2018-09-17T04:47:34 -06:-30\",\"latitude\":-74.60479,\"longitude\":34.229968,\"tags\":[\"non\",\"nulla\",\"nostrud\",\"occaecat\",\"eiusmod\",\"commodo\",\"elit\"],\"friends\":[{\"id\":0,\"name\":\"Raquel James\"},{\"id\":1,\"name\":\"Katina Walsh\"},{\"id\":2,\"name\":\"Leah Guerrero\"}],\"greeting\":\"Hello, Estrada Martinez! You have 3 unread messages.\",\"favoriteFruit\":\"apple\"}";
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonString);
System.out.println(jsonObject);

Gson

There are two ways to convert a string JSON to a JSON object using the Gson library:

  1. JsonParser
  2. fromJson

To use the Gson library, add the Gson dependency from Maven Repository.

String jsonString = "{\"_id\":\"61055ba08cf4754d7ba5d191\",\"index\":6,\"guid\":\"f74a0878-0dc5-47a4-a7cf-c7ff806765af\",\"isActive\":false,\"balance\":\"$3,211.08\",\"picture\":\"http://placehold.it/32x32\",\"age\":31,\"eyeColor\":\"brown\",\"name\":\"Estrada Martinez\",\"gender\":\"male\",\"company\":\"QUIZKA\",\"email\":\"estradamartinez@quizka.com\",\"phone\":\"+1 (896) 583-3530\",\"address\":\"265 Williams Court, Manila, Wisconsin, 5703\",\"about\":\"Nulla consequat duis minim velit nisi elit deserunt eu sint. In nisi et proident ullamco velit. Amet consequat irure tempor non consequat ad consequat non reprehenderit fugiat nostrud sit. Ex eu irure nostrud fugiat exercitation esse commodo ad id magna id consequat eiusmod.\\r\\n\",\"registered\":\"2018-09-17T04:47:34 -06:-30\",\"latitude\":-74.60479,\"longitude\":34.229968,\"tags\":[\"non\",\"nulla\",\"nostrud\",\"occaecat\",\"eiusmod\",\"commodo\",\"elit\"],\"friends\":[{\"id\":0,\"name\":\"Raquel James\"},{\"id\":1,\"name\":\"Katina Walsh\"},{\"id\":2,\"name\":\"Leah Guerrero\"}],\"greeting\":\"Hello, Estrada Martinez! You have 3 unread messages.\",\"favoriteFruit\":\"apple\"}";
JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
System.out.println(jsonObject);

// Using fromJson
JsonObject jsonObject1 =  new Gson().fromJson(jsonString, JsonObject.class);
System.out.println(jsonObject1);

RELATED TAGS

java
json
jackson
gson
communitycreator
RELATED COURSES

View all Courses

Keep Exploring