Parsing JSON from S3 SelectObjectContent

Viewed 490

I am trying to create an object from the response I get after s3 selectObjectContent. But I see a problem in creating an inputstream from the response. The response seems to end with the delimiter being specified at the end, which is causing issues in converting Inputstream to JSON object or using ObjectMapper

My request object is as below:

             SelectObjectContentRequest request = new SelectObjectContentRequest();
                    request.setBucketName("<<bucket>>);
                    request.setKey("<<key>>");
                    request.setExpression("SELECT * FROM S3Object[*][*] s");
                    request.setExpressionType(ExpressionType.SQL);
            
            
                    InputSerialization inputSerialization = new InputSerialization();
                    JSONInput jsonInput = new JSONInput();
                    jsonInput.setType("DOCUMENT");
                    inputSerialization.setJson(jsonInput);
                    inputSerialization.setCompressionType(CompressionType.NONE);
            
                    request.setInputSerialization(inputSerialization);
            
                    OutputSerialization outputSerialization = new OutputSerialization();
                    JSONOutput jsonOutput = new JSONOutput();
                    jsonOutput.setRecordDelimiter(",");
                    outputSerialization.setJson(jsonOutput);
                    request.setOutputSerialization(outputSerialization);
                    return request;
            
            InputSteram conversion : 
            
                InputStream is = result.getPayload().getRecordsInputStream(
                                new SelectObjectContentEventVisitor() {
                                    @Override
                                    public void visit(SelectObjectContentEvent.EndEvent endEvent) {
                                        requestComplete.set(true);
                                    }
                                });
            

JsonObject Conversion:

        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject) jsonParser.parse(new InputStreamReader(is, "UTF-8"));

The above fails since the EOF is a
The error I get is Unexpected token END OF FILE at position 0 I have a work around for the above problem but don't really like it and looking for a cleaner approach. Help is appreciated. Thanks in advance

0 Answers
Related