How to read and write JSON in Java using org.json

10 months ago codedestine 0

How to read and write JSON in Java :- If you had worked on restful web services, then you would have faced a problem of reading and writing JSON in java. JSON stands for JavaScript Object Notation, it is language independent, easy to understand, lightweight data-interchange format. 

Let’s see how to read and write JSON in java :- 

Sample JSON

JSON for book type :-

{
    "id": 01,
    "language": "Java",
    "edition": "third",
    "author": "Herbert Schildt",
    "chapters": ["chapter 1","chapter 2","chapter 3"]
} 

Org.JSON (Maven) :-

This library is very easy to use, if you don’t need object deserialization but to get an attribute use this library.

Read :- Let’s see how to read JSON in Java :-

package com.codedestine;

import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonUtility {

   public static String json = "{ \"id\": 01, \"language\": \"Java\", \"edition\": \"third\", \"author\": \"Herbert Schildt\", \"chapters\": [\"chapter 1\",\"chapter 2\",\"chapter 3\"] }";
 
   public void readJson(String json){
     JSONObject jsonObj = new JSONObject(json);
 
     String id = jsonObj.getString("id");
     System.out.println(id);
 
     String language = jsonObj.getString("language");
     System.out.println(language);
 
     String edition = jsonObj.getString("edition");
     System.out.println(edition);
 
     String author = jsonObj.getString("author");
     System.out.println(author);
 
     JSONArray chapters = (JSONArray) jsonObj.get("chapters");
     Iterator<Object> iterator = chapters.iterator();
     while (iterator.hasNext()) {
        System.out.println(iterator.next());
     }
   }
 
   public static void main(String args[]){
     JsonUtility utility = new JsonUtility();
     //read
     utility.readJson(json);
   }

}

Result :- 

How to read and write JSON in Java

Write :- Let’s see how to write JSON in Java :-

package com.codedestine;

import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonUtility {

   public static String json = "{ \"id\": 01, \"language\": \"Java\", \"edition\": \"third\", \"author\": \"Herbert Schildt\", \"chapters\": [\"chapter 1\",\"chapter 2\",\"chapter 3\"] }";
 
   public void writeJson(){ 
     JSONObject jsonObj = new JSONObject();
 
     jsonObj.put("id", 2);
     jsonObj.put("language", "english");
     jsonObj.put("edition", "fourth");
     jsonObj.put("author", "edison");

     JSONArray list = new JSONArray();
     list.put("chapter 1");
     list.put("chapter 2");
     list.put("chapter 3");
     jsonObj.put("chapters", list);
 
     System.out.println(jsonObj.toString(2));
   }
 
   public static void main(String args[]){
     JsonUtility utility = new JsonUtility();
     //write
     utility.writeJson();
   }

}

Result :- 

How to read and write JSON in Java

You can also check How to parse JSON in Java using fasterXML / jackson-databind lib

That’s all for how to read and write JSON in java using org.json. If you liked it, please share your thoughts in comments section and share it with others too.