org.json.CDL: Convert between Java JSONArray and CSV

By Arvind Rai, April 24, 2023
On this page, we will learn to convert JSONArray into CSV and CSV to JSONArray using org.json.CDL class.
1. The org.json.CDL provides static methods to convert comma-separated values into a JSONArray, and to convert a JSONArray into comma-separated values.
2. CSV is comma-separated values that is a popular format for data interchange. CSV is useful in most database, spreadsheet, and organizer programs for data interchange.
3. The rule for CDL is that each row of text represents a row in a table or a data record. Each row ends with NEWLINE (\n) character. Each row contains one or more values separated by commas. A value can contain any character except comma, unless is wrapped in single quotes or double quotes.
4. A comma delimited list can be converted into a JSONArray of JSONObject. We need to use first row of text for names to assign names in JSONObject.
5. The CDL class has following static methods.
rowToJSONArray(JSONTokener x)
rowToJSONObject( JSONArray names, JSONTokener x)
rowToString(JSONArray ja)
toJSONArray(JSONArray names, JSONTokener x)
toJSONArray(JSONArray names, String string)
toJSONArray(JSONTokener x)
toJSONArray(String string)
toString(JSONArray ja)
toString(JSONArray names, JSONArray ja) 
6. Resolve ORG.JSON dependency using Maven as following.
<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20230227</version>
</dependency> 

On this page, we will discuss each and every static method of org.json.CDL in detail with examples.

1. rowToJSONArray() - String to JSONArray

static JSONArray rowToJSONArray(JSONTokener x)  throws JSONException 
Produces a JSONArray of strings from a row of comma delimited values. A JSONTokener takes a source string and extracts characters and tokens from it.
Example:
JSONArray jarray1 = CDL.rowToJSONArray(new JSONTokener("Krishna, Mohit, Mahesh, Shiva"));
System.out.println(jarray1);

JSONArray jarray2 = CDL.rowToJSONArray(new JSONTokener("@Krishna, Mohit-Sharma, Mahesh Singh, 'Shiva, India'"));
System.out.println(jarray2); 
Output
["Krishna","Mohit","Mahesh","Shiva"]
["@Krishna","Mohit-Sharma","Mahesh Singh","Shiva, India"] 
The rowToJSONArray works for CSV of a single row. It means a string with comma separated is considered to be converted into JSONArray. If rowToJSONArray finds NEWLINE (\n) in string, rowToJSONArray ends reading further data because rowToJSONArray considers only one row. If the value contains comma character then that value should be quoted.

2. rowToJSONObject() - String to JSONObject

static JSONObject rowToJSONObject( JSONArray names,
         JSONTokener x) throws JSONException 
Produces JSONObject. The values are taken from a row of comma delimited text as JSONTokener and names are taken from JSONArray.
Example:
JSONArray jsonArray = CDL.rowToJSONArray(new JSONTokener("name, age, city"));
JSONObject jsonObj1 = CDL.rowToJSONObject(jsonArray, new JSONTokener("Krishna, 20, Varanasi"));
System.out.println(jsonObj1);
JSONObject jsonObj2 = CDL.rowToJSONObject(jsonArray, new JSONTokener("@Mahesh, 30, 'Noida, UP'"));
System.out.println(jsonObj2); 
Output
{"name":"Krishna","city":"Varanasi","age":"20"}
{"name":"@Mahesh","city":"Noida, UP","age":"30"} 


3. rowToString() and toString() - JSONArray to String

(a)
static String rowToString(JSONArray ja) 
Produces a comma delimited text row from a JSONArray. Values containing comma character will be quoted.
Example:
JSONArray jsonArray1 = new JSONArray(List.of("USA", "France", "India"));
String csv1 = CDL.rowToString(jsonArray1);
System.out.println(csv1);
JSONArray jsonArray2 = new JSONArray(List.of("Varanasi, UP", "New Delhi", "@Noida"));
String csv2 = CDL.rowToString(jsonArray2);
System.out.println(csv2); 
Output
USA,France,India
"Varanasi, UP",New Delhi,@Noida 
(b)
static String toString(JSONArray ja) throws JSONException 
Produces a comma delimited text from a JSONArray of JSONObject. In the output string, the first line is names obtained by inspecting the first JSONObject in the given JSONArray.
Example:
Map<String, String> map1 = new HashMap<>();
map1.put("name", "Mohit");
map1.put("age", "20");
map1.put("city", "Varanasi");

Map<String, String> map2 = new HashMap<>();
map2.put("name", "Shiva");
map2.put("age", "30");
map2.put("city", "Chennai"); 

JSONArray jsonArray = new JSONArray();
jsonArray.put(map1);
jsonArray.put(map2);
System.out.println(jsonArray); 	

System.out.println("\n---Using CDL.toString(JSONArray ja)---");
String csv = CDL.toString(jsonArray);
System.out.println(csv); 
Output
[{"name":"Mohit","city":"Varanasi","age":"20"},{"name":"Shiva","city":"Chennai","age":"30"}]

---Using CDL.toString(JSONArray ja)---
name,city,age
Mohit,Varanasi,20
Shiva,Chennai,30 
To understand more about the working of toString(JSONArray ja) method, find its definition code from org.json.CDL doc.
public static String toString(JSONArray ja) throws JSONException {
  JSONObject jo = ja.optJSONObject(0);
  if (jo != null) {
    JSONArray names = jo.names();
    if (names != null) {
      return rowToString(names) + toString(names, ja);
    }
  }
  return null;
} 
(c)
static String toString(JSONArray names,
                 JSONArray ja) throws JSONException 
Produces a comma delimited text from a JSONArray of JSONObjects using a provided list of names. The list of names will not be included in the string output.
Example:
Map<String, String> map1 = new HashMap<>();
map1.put("id", "100");
map1.put("username", "Mohit");
map1.put("city", "Varanasi");

Map<String, String> map2 = new HashMap<>();
map2.put("id", "101");
map2.put("username", "Shiva");
map2.put("city", "Chennai");

JSONArray jsonArray = new JSONArray();
jsonArray.put(map1);
jsonArray.put(map2);
System.out.println(jsonArray);

System.out.println("\n---Using CDL.toString(JSONArray ja)---");
JSONArray jsonArrayNames = CDL.rowToJSONArray(new JSONTokener("id, username, city"));
String csv = CDL.toString(jsonArrayNames, jsonArray);
System.out.println(csv);

System.out.println("\n---With selected names---");
JSONArray selectedNames = CDL.rowToJSONArray(new JSONTokener("id, city"));
csv = CDL.toString(selectedNames, jsonArray);
System.out.println(csv); 
Output
[{"city":"Varanasi","id":"100","username":"Mohit"},{"city":"Chennai","id":"101","username":"Shiva"}]

---Using CDL.toString(JSONArray ja)---
100,Mohit,Varanasi
101,Shiva,Chennai

---With selected names---
100,Varanasi
101,Chennai 

4. toJSONArray - String to JSONArray of JSONObject

(a)
static JSONArray toJSONArray(JSONArray names,
                  JSONTokener x) throws JSONException
static JSONArray toJSONArray(JSONArray names,
                  String string) throws JSONException 
Produces a JSONArray of JSONObject from a comma delimited text string using a supplied JSONArray as the source of element names.
Example:
String s ="Mohit,Varanasi,20\n"
	+ "Shiva,Chennai,30\n";
JSONArray jsonArrayNames = CDL.rowToJSONArray(new JSONTokener("name, city, age"));
JSONArray jsonArray1 = CDL.toJSONArray(jsonArrayNames, new JSONTokener(s));
System.out.println(jsonArray1);
JSONArray jsonArray2 = CDL.toJSONArray(jsonArrayNames, s);
System.out.println(jsonArray2); 
Output
[{"name":"Mohit","city":"Varanasi","age":"20"},{"name":"Shiva","city":"Chennai","age":"30"}]
[{"name":"Mohit","city":"Varanasi","age":"20"},{"name":"Shiva","city":"Chennai","age":"30"}] 
(b)
static JSONArray toJSONArray(JSONTokener x) throws JSONException
static JSONArray toJSONArray(String string) throws JSONException 
Produces a JSONArray of JSONObject from a comma delimited text string, using the first row as a source of names.
Example:
String s = "id,name,age\n"
	+ "101,Mohit,20\n"
	+ "102,Shiva,30\n";
JSONArray jsonArray1 = CDL.toJSONArray(new JSONTokener(s));
System.out.println(jsonArray1);
JSONArray jsonArray2 = CDL.toJSONArray(s);
System.out.println(jsonArray2); 
Output
[{"name":"Mohit","age":"20","id":"101"},{"name":"Shiva","age":"30","id":"102"}]
[{"name":"Mohit","age":"20","id":"101"},{"name":"Shiva","age":"30","id":"102"}] 

5. Reference

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE












©2024 concretepage.com | Privacy Policy | Contact Us