I'm trying to convert InputStream
to JSON Array object
but not getting the JSON object properly, please find my inputStream
record below:
{"id":4,"productId":9949940,"data":"product data 1","productPrice":"653.90"}
{"id":5,"productId":4940404,"data":"product data 2","productPrice":"94.12"}
I'm getting extra commas for each item and for last record as well - please find the java code below. Can someone please help me to resolve this issue. Appreciated your help in advance. Thanks!
Product.java
public void getProduct() {
String bucketName = "myProductBucket";
String key = "products/product-file";
StringBuilder sb = null;
JSONArray jsonArray = new JSONArray();
try(InputStream inputStream = s3Service.getObjectFromS3(bucketName, key);) {
sb = new StringBuilder();
sb.append("[");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line = reader.readLine()) != null) {
sb.append(line).append(",");
}
sb.append("]");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sb.toString());
}
Output:
[{"id":4,"productId":9949940,"data":"product data 1","productPrice":"653.90"},,
{"id":5,"productId":4940404,"data":"product data 2","productPrice":"94.12"},]
Expected Output:
[{"id":4,"productId":9949940,"data":"product data 1","productPrice":"653.90"},
{"id":5,"productId":4940404,"data":"product data 2","productPrice":"94.12"}]