So I am trying to convert a JSON file (Which I have exported as JSON using Google Sheets API) to a CSV file, even though there is a JSON file placed correctly at that location, or trying with instead a String, it either shows null pointer exception or returns an empty CSV file.
Sample JSON file:
{
"range": "products!A1:E4",
"majorDimension": "ROWS",
"values": [
[
"product_name",
"price"
],
[
"Rugrats - Bedtime Bash [VHS]",
"36.95"
],
[
"Teenage Mutant Ninja Turtles II - The Secret of the Ooze [VHS]",
"29.89"
],
[
"Teenage Mutant Ninja Turtles II - The Secret of the Ooze [VHS]",
"29.89"
]
]
}
The sample code I am trying to use here for converting them to CSV,
JSONObject output = new JSONObject(<String or file location that holds JSON file>);
File finalFile=new File(<Destination file path>);
JSONArray docs = output.getJSONArray("values");
String csv = CDL.toString(docs);
FileUtils.writeStringToFile(finalFile, csv);
Did I make any mistake in code or should I handle this JSON with different Java code?