0

I have a text file present in an Amazon S3 bucket containing JSON objects which I need to parse and extract information out of it. How do I do that in java?

I am not able to do that in Java. I just need to be able to parse this JSON file and convert to csv and upload this CSV file to Amazon S3.

A sample file is(file.txt):

{"ID":"abc","mscore":45,"cscore":42,"grade":"A"}
{"ID":"xyz","mscore":41"cscore":35,"grade":"B"}
{"ID":"pqr","mscore":37"cscore":41,"grade":"B"}
{"ID":"mno","mscore":32,"cscore":29,"grade":"C"}

I want to convert this to (file.csv): where score=mscore+cscore

ID      score      grade
"abc"   87         A
"xyz"   76         B
"pqr"   78         B
"mno"   61         C

-------- UPDATE ---------

I created a class Student

public class Student{
String ID;
int mscore;
int cscore;
String grade;
}

I used the below code snippet to read the text file of JSON objects and store the data in a studentList.

S3Object s3Object = s3Client.getObject(new GetObjectRequest(s3BucketName, fileKey)); 
S3ObjectInputStream s3Stream = s3Object.getObjectContent(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(s3Stream, StringUtils.UTF8)); 
List<Student> studentList=null; 
while ((line = reader.readLine()) != null) {
  Student student = objectMapper.readValue(line, Student.class);
  studentList.add(student);
}

However, i cannot figure out how to convert this list to a CSV file and upload it to s3. I do not want to store file locally.

himanshi
  • 23
  • 4
  • This should be helpful : https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Thomas Martin Oct 13 '21 at 14:37
  • Does this answer your question? [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Thomas Martin Oct 13 '21 at 14:38
  • Hey, I'm able to parse the data using jackson's object mapper and I have a list of students as List studentList. I'm not sure how to create a csv file and upload to s3 without creating a file on my local. – himanshi Oct 22 '21 at 06:52

1 Answers1

0

I would look at writing a Lambda function that uses the Java run-time API and the AWS SDK for Java. In that Lambda function, use a Java JSON Lib - like GSON that can read and parse JSON data. Create the resulting CSV file and then use the AWS SDK for Java API to invoke S3 to upload the new file.

smac2020
  • 9,637
  • 4
  • 24
  • 38