1

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"}]
3
  • Try using BufferedReader to read out the strings in the InputStream into a StringBuilder that you can convert to a JsonObject: convert an InputStream to a JsonObject. Commented Jan 12, 2022 at 17:51
  • 2
    As a whole, your input stream content does not represent valid JSON content. Each individual JSON object is valid and that's what JSONObject consumes, the first line/object. Commented Jan 12, 2022 at 17:54
  • The last comma appears since You're appending it unconditionally, try the opposite: prepend the comma before the next object if the buffer is not empty. The commas between the objects may appear because of empty lines: test the line before feeding it to the builder. Alternatively, You may create the JSONArray manually and then populate it with JSONObjects one by one, this is (arguably) easier and objectively more efficient. Commented Jan 12, 2022 at 20:48

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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>

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.