
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to convert a JSON array to CSV in Java?
The JSON can be used as a data-interchange format and is lightweight and language independent. A JSONArray can parse text from a String to produce a vector-like object and supports java.util.List interface. We can convert a JSON Array to CSV format using org.json.CDL class, it can provide a static method toString(), to convert a JSONArray into comma-delimited text. We need to import org.apache.commons.io.FileUtils package to store the data in a CSV file using the writeStringToFile() method.
Syntax
public static java.lang.String toString(JSONArray ja) throws JSONException
In the below example, we can convert a JSON Array to CSV format.
Example
import java.io.File; import org.apache.commons.io.FileUtils; import org.json.*; public class ConvertJsonToCSVTest { public static void main(String[] args) throws JSONException { String jsonArrayString = "{\"fileName\": [{\"first name\": \"Ravi\",\"last name\": \"Chandra\",\"location\": \"Bangalore\"}]}"; JSONObject output; try { output = new JSONObject(jsonArrayString); JSONArray docs = output.getJSONArray("fileName"); File file = new File("EmpDetails.csv"); String csv = CDL.toString(docs); FileUtils.writeStringToFile(file, csv); System.out.println("Data has been Sucessfully Writeen to "+ file); System.out.println(csv); } catch(Exception e) { e.printStackTrace(); } } }
Output
Data has been Sucessfully Writeen to EmpDetails.csv last name,first name,location Chandra,Ravi,Bangalore
- Related Articles
- How to convert a JSON array to array using JSON-lib API in Java?\n
- How to convert Java Array/Collection to JSON array?
- How to convert JSON Array to normal Java Array?
- How to convert JSON to CSV file using PowerShell?
- How to convert XML to JSON array in Java?
- Convert CSV to JSON using the Jackson library in Java?\n
- How to convert a Collection to JSON Array using JSON-lib API in Java?
- How to convert an array to JSON Array using JSON-lib API in Java?\n
- How to convert JSON file to CSV file using PowerShell?
- How to convert a Python csv string to array?
- How can we convert a list to the JSON array in Java?
- How to convert a 2D array to a CSV string in JavaScript?
- How to convert a List to JSON array using the Jackson library in Java?
- How can we convert a JSON string to a JSON object in Java?
- How to convert a Map to JSON object using JSON-lib API in Java?

Advertisements