How to convert InputStream to JsonArray Object using Java - java

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"}]

AFAIU, this is expected, since Your JSON object is only partially valid.
Although it is not a valid JSON array either, it could be parsed into JSONArray after small modifications (mind the starting and closing brackets and a comma between the objects):
[
{"id":4,"productId":9949940,"data":"product data 1","productPrice":"653.90"},
{"id":5,"productId":4940404,"data":"product data 2","productPrice":"94.12"}
]
Or, alternatively, You could split the input into individual JSON objects by hand and parse them one by one.

As per your Product details file, You are not having valid JSON array objects in file. So, It can not be possible to directly create JSONArray from the file.
What you can do is, Read the product lines one by one and Create JSONObject and convert it to the JSONArray. Please find below piece of code which can help.
Scanner s = new Scanner(new File("filepath")); //Read the file
JSONArray jsonArray = new JSONArray();
while (s.hasNext()){
JSONObject jsonObject = new JSONObject(s.next());
jsonArray.put(jsonObject);
}
//jsonArray can be print by iterating through it.

Here is the code that you can use, the idea is InputStream does not represent a valid JSON so you have to convert it into a valid JSON string using StringBuilder. But first, you need to take care of the JSON which is not valid.
StringBuilder sb;
try(InputStream inputStream = new FileInputStream(new File("Path"))) {
sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
JSONArray jsonArray = new JSONArray(sb.toString());
Dependecy
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20211205</version>
</dependency>

Get Stream from file content to List<String>.
FileReader fileReader = null;
try {
File file = new File("path");
InputStream inputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
List<String> list = bufferedReader.lines().collect(Collectors.toList());
JSONArray array = new JSONArray();
for (String s : list) {
array.put(new JSONObject(s));
}
bufferedReader.close();
System.out.println(array);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (Objects.nonNull(null)) {
fileReader.close();
}
}
Dependency:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220320</version>
</dependency>

Related

Reading text file into arraylist

I really new to Java so I'm having some trouble figuring this out. So basically I have a text file that looks like this:
1:John:false
2:Bob:false
3:Audrey:false
How can I create an ArrayList from the text file for each line?
Read from a file and add each line into arrayList. See the code for example.
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
try (BufferedReader br = new BufferedReader(new FileReader(<your_file_path>)))
{
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
arr.add(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
While the answer above me works, here's another way to do it. Make sure to import java.util.Scanner.
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
Scanner scan = new Scanner("YOURFILE.txt");
while(scan.hasNextLine()){
list.add(scan.nextLine());
}
scan.close();
}
If you know how to read a file line by line, either by using Scanner or by using BufferedReader then reading a text file into ArrayList is not difficult for you. All you need to do is read each line and store that into ArrayList, as shown in following example:
BufferedReader bufReader = new BufferedReader(new
FileReader("file.txt"));
ArrayList<String> listOfLines = new ArrayList<>);
String line = bufReader.readLine(); while (line != null)
{
listOfLines.add(line);
line = bufReader.readLine();
}
bufReader.close();
Just remember to close the BufferedReader once you are done to prevent resource leak, as you don't have try-with-resource statement
This will be help to you.
List<String> list = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader("list.txt"));
String line;
while ((line = reader.readLine()) != null) {
list.add(line);
}
reader.close();
Then you can access those elements in the arraylist.
java 8 lets you do this
String fileName = "c://lines.txt";
List<String> list = new ArrayList<>();
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
list = stream
.map(String::toUpperCase)
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
list.forEach(System.out::println);

Format a String in Json style

I extracted a huge String from a webpage and want to style/formatting this in Json style. The extracted String was originally a Json format but now after extracting this is just a long String. I used JsonObj for this and the formatter does curios things, he moved text from the bottom to top changed the generally the line orders etc.
http://pastebin.com/exwwc6SY JsonFile after Formatting
http://pastebin.com/WHXtE36G The extracted String
And here the code
try {
FileWriter fw = new FileWriter("/tmp/1.txt");
String line = ROUtils.getStringFromInputStream(urlConnection.getInputStream());
System.out.println(line);
String jsonObj = new JSONObject(line).toString(2);
fw.write(jsonObj);
} catch (IOException e) {
e.printStackTrace();
}
And the getStringFromInputStream() method
public static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return sb.toString();
}
Update
I found a new issue. The JsonObj File its not equal to the original String.
I compared the number of Characters (no spaces). The original String has 96311 and the JsonObj has 92636. Can anyone give me a hint what should I do?
You cannot and should not rely on the ordering of elements within a JSON object.
From the JSON specification at http://www.json.org/
An object is an unordered set of name/value pairs.
I found it out why i missed 4000 characters after converting.
I forgot to close the FileWriter!
fw.close();
The close() methods calls the flush() method so that the last buffered piece of the String can written down.
Thank u guys.

Unable to a read a large file using BufferedReader in Java

I am trying to read a file using BufferedReader, but when I tried to print, It is returning some weird characters.
Code of reading file is:
private static String readJsonFile(String fileName) throws IOException{
BufferedReader br = null;
try {
StringBuilder sb = new StringBuilder();
br = new BufferedReader(new FileReader(fileName));
String line = br.readLine();
while(line != null ){
sb.append(line);
System.out.println(line);
line=br.readLine();
}
return sb.toString();
} finally{
br.close();
}
}
This function is being called as :
String jsonString = null;
try {
jsonString = readJsonFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
But when I tried to print this in console using System.out.println(jsonString);, It is returning some fancy pictures.
Note: It is Working file when file size is small.
Is there any limit on size of file it can read ?
You're using the platform default encoding to read the file, which is probably encoded in UTF8. Check the actual encoding of the file, and specify the encoding:
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream("...", StandardCharsets.UTF_8));
Note that since you simply want to read everything from the file, you could simply use
String json = new String(Files.readAllBytes(...), StandardCharsets.UTF_8);

Storing single csv line object into arrays

hey ive got a chunk of code here trying to read a single line in a .csv file:
rows = new WarehouseItem[];
public void readCSV(String filename) {
FileInputStream fileStrm = null;
InputStreamReader rdr;
BufferedReader bufRdr;
int lineNum;
String line;
try {
fileStrm = new FileInputStream(filename);
rdr = new InputStreamReader(fileStrm);
bufRdr = new BufferedReader(rdr);
numRows = 0;
line = bufRdr.readLine();
while (line != null) {
rows[numRows] = line;
numRows++;
line = bufRdr.readLine();
}
fileStrm.close();
}
catch (IOException e) {
if (fileStrm != null) {
try {
fileStrm.close();
} catch (IOException ex2) {}
}
System.out.println("Error in file processing: " + e.getMessage());
}
}
on the rows[numRows] = line im trying to store the line into an array of objects(i have premade an object which contains an array of strings and the number of columns)
im not entirely sure how to store the single line im trying to read in my object.
any help would be really appreciated :)
Your life would be an awful lot easier if you used a CSV library to do this. With jackson it's really simple to read CSV into an array of objects.
For example:
CsvMapper mapper = new CsvMapper();
mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
File csvFile = new File("input.csv"); // or from String, URL etc
MappingIterator<Object[]> it = mapper.reader(Object[].class).readValues(csvFile);
See here for more info on parsing CSV in java: http://demeranville.com/how-not-to-parse-csv-using-java/

Efficiency of open a very big JSONArray (in a file), just to see first position

I'm reading a JSON file in my android project, just to see the first position of the array.
In fact, if this JSON is more big what normal, I think this isn't efficient...
My json file reader:
public static JSONObject parseJSONfromInputStrem (InputStreamReader isr){
try {
BufferedReader reader = new BufferedReader(isr,8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
Where I call the function:
FileInputStream fis = c.openFileInput(file);
InputStreamReader isr = new InputStreamReader(fis);
JSONObject jso = JSONParser.parseJSONfromInputStrem(isr);
JSONArray myArray = jso.getJSONArray("data");
There are any way to read, efficiently, the first position of a JSONArray?
You should use a streaming JSON parser (think DOM vs SAX).
https://stackoverflow.com/a/823632/18573 lists some. There may be others.
You can either read in a small amount of the file and pull the value out manually, or put the value in to a separate file. Even if it's duplicated data, a secondary file which contains summarized or key information from the primary file will make things much more efficient.

Categories