I have a csv file,
Employee: Salutation,LegalEntityId,FirstName,MiddleName,LastName,DisplayName,WorkPhoneCountryCode,WorkPhoneAreaCode,WorkPhoneNumber,WorkMobilePhoneCountryCode,WorkMobilePhoneAreaCode,WorkMobilePhoneNumber,HomeFaxCountryCode,HomeFaxAreaCode,HomeFaxNumber,HomePhoneCountryCode,HomePhoneAreaCode,HomePhoneNumber,NameSuffix,NationalIdExpirationDate,WorkEmail,AddressLine1,AddressLine2,AddressLine3,City,Region,Region2,Country,PostalCode,CitizenshipLegislationCode,CitizenshipStatus,PassportNumber,PassportIssueDate,Religion,LicenseNumber,DateOfBirth,Ethnicity,Gender,MaritalStatus,NationalIdCountry,NationalId,NationalIdType,UserName,AssignmentName,BusinessUnitId,JobId,WorkerCategory,AssignmentCategory,WorkingAtHome,WorkingAsManager,SalaryCode,WorkingHours,Frequency,SalaryAmount,SalaryBasisId,ActionCode,ActionReasonCode,AssignmentStatus,ManagerType
MS.,3.00E+14,Ciran,Myle,Machell,Ciran Myle Machell,1,1,781-993-0000,1,1,581-993-0000,1,1,1111664,1,1,122933334,Jr.,1027428,[email protected],Oracle Corporation,500 Oracle Parkway,null,San Francisco,San Francisco,CA,US,94123,US,A,11553397,37621,CHRISTIAN,9955501858,33033,8,F,S,US,254-44-3022,SSN,MachellCira,Analyst,3.00E+14,3.00E+14,WC,FR,N,N,H,12,D,100000,3.00E+14,HIRE,NEWHIRE,ACTIVE,LINE_MANAGER
Please help me modify the code get the output of the form,
{
"Salutation" : "MS.",
"LegalEntityId" : "300100003448002",
"FirstName": "Miranda",
"MiddleName": "Kyle",
"LastName": "Rachell",
"DisplayName" : "Miranda Kyle Rachell",
"WorkPhoneCountryCode" : "1",
"WorkPhoneAreaCode" : "1",
"WorkPhoneNumber" : "781-993-0000",
"WorkMobilePhoneCountryCode" : "1",
"WorkMobilePhoneAreaCode" : "1",
"WorkMobilePhoneNumber" : "581-993-0000",
"HomeFaxCountryCode":"1",
"HomeFaxAreaCode":"1",
"HomeFaxNumber":"1111663",
"HomePhoneCountryCode":"1",
"HomePhoneAreaCode":"1",
"HomePhoneNumber":"122993333",
"NameSuffix":"Jr.",
"NationalIdExpirationDate" : "4712-12-31",
"WorkEmail" : "[email protected]",
"AddressLine1" : "Oracle Corporation",
"AddressLine2" : "500 Oracle Parkway",
"AddressLine3" : null,
"City" : "San Francisco",
"Region" : "California",
"Region2" : null,
"Country" : "US",
"PostalCode": "94065",
"CitizenshipLegislationCode" : "US",
"PassportNumber" : "11557799",
"PassportIssueDate" : "2002-12-31",
"Religion" : "CHRISTIAN",
"LicenseNumber" : "9955500858",
"DateOfBirth": "1990-09-09",
"Ethnicity": "8",
"Gender" : "F",
"MaritalStatus" : "S",
"NationalIdCountry": "US",
"NationalId": "554-44-3025",
"NationalIdType": "SSN",
"UserName":"rachell",
**"assignments" : **
[
{
"AssignmentName": "Rachell_Assignment_1",
"BusinessUnitId" : "202",
"JobId" : "18",
"WorkerCategory" : "WC",
"AssignmentCategory" : "FR",
"WorkingAtHome" : "N",
"WorkingAsManager" : "N",
"SalaryCode" : "H",
"WorkingHours" : "12",
"Frequency" : "D",
"SalaryAmount" : "100000",
"SalaryBasisId" : "100010024999994",
"ManagerAssignmentId" : "389",
"ManagerId" : "383",
"ActionCode" : "HIRE",
"ActionReasonCode" : "NEWHIRE",
"AssignmentStatus" : "ACTIVE",
"WorkTaxAddressId" : "300100011952957",
"ManagerType" : "LINE_MANAGER"
}
]
}
The data given is a demo data. I am not that into Java.
The code is given below
package post;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.io.BufferedInputStream;
import org.apache.commons.io.IOUtils;
import java.io.InputStream;
//import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class PostEmployee {
public static void main(String[] args) throws IOException {
String file1Path = "Employee.csv";
String username = "hcm_imp9";
String password = "pwd";
List<JsonNode> jsonArray = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(file1Path));
String line;
JsonNode jsonObject;
String[] headers = null;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
if (headers == null) {
headers = values;
} else {
ObjectMapper objectMapper = new ObjectMapper();
jsonObject = objectMapper.convertValue(headersZip(values, headers), JsonNode.class);
jsonArray.add(jsonObject);
}
}
br.close();
String jsonString = jsonArray.toString();
jsonString = jsonString.replace(":", ": ");
System.out.println(jsonString);
}
private static JsonNode headersZip(String[] values, String[] headers) {
if (values == null || headers == null || values.length != headers.length) {
return null;
}
ObjectMapper objectMapper = new ObjectMapper();
JsonNode json = objectMapper.createObjectNode();
for (int i = 0; i < values.length; i++) {
((ObjectNode) json).put(headers[i], values[i]);
}
return json;
}
}
The Result:
[{"Salutation": "MS.","LegalEntityId": "3.00E+14","FirstName": "Ciran","MiddleName": "Myle","LastName": "Machell","DisplayName": "Ciran Myle Machell","WorkPhoneCountryCode": "1","WorkPhoneAreaCode": "1","WorkPhoneNumber": "781-993-0000","WorkMobilePhoneCountryCode": "1","WorkMobilePhoneAreaCode": "1","WorkMobilePhoneNumber": "581-993-0000","HomeFaxCountryCode": "1","HomeFaxAreaCode": "1","HomeFaxNumber": "1111664","HomePhoneCountryCode": "1","HomePhoneAreaCode": "1","HomePhoneNumber": "122933334","NameSuffix": "Jr.","NationalIdExpirationDate": "1027428","WorkEmail": "[email protected]","AddressLine1": "Oracle Corporation","AddressLine2": "500 Oracle Parkway","AddressLine3": "null","City": "San Francisco","Region": "San Francisco","Region2": "CA","Country": "US","PostalCode": "94123","CitizenshipLegislationCode": "US","CitizenshipStatus": "A","PassportNumber": "11553397","PassportIssueDate": "37621","Religion": "CHRISTIAN","LicenseNumber": "9955501858","DateOfBirth": "33033","Ethnicity": "8","Gender": "F","MaritalStatus": "S","NationalIdCountry": "US","NationalId": "254-44-3022","NationalIdType": "SSN","UserName": "MachellCira","AssignmentName": "Analyst","BusinessUnitId": "3.00E+14","JobId": "3.00E+14","WorkerCategory": "WC","AssignmentCategory": "FR","WorkingAtHome": "N","WorkingAsManager": "N","SalaryCode": "H","WorkingHours": "12","Frequency": "D","SalaryAmount": "100000","SalaryBasisId": "3.00E+14","ActionCode": "HIRE","ActionReasonCode": "NEWHIRE","AssignmentStatus": "ACTIVE","ManagerType": "LINE_MANAGER"}]
The Desired Output:
{
"Salutation" : "MS.",
"LegalEntityId" : "300100003448002",
"FirstName": "Miranda",
"MiddleName": "Kyle",
"LastName": "Rachell",
"DisplayName" : "Miranda Kyle Rachell",
"WorkPhoneCountryCode" : "1",
"WorkPhoneAreaCode" : "1",
"WorkPhoneNumber" : "781-993-0000",
"WorkMobilePhoneCountryCode" : "1",
"WorkMobilePhoneAreaCode" : "1",
"WorkMobilePhoneNumber" : "581-993-0000",
"HomeFaxCountryCode":"1",
"HomeFaxAreaCode":"1",
"HomeFaxNumber":"1111663",
"HomePhoneCountryCode":"1",
"HomePhoneAreaCode":"1",
"HomePhoneNumber":"122993333",
"NameSuffix":"Jr.",
"NationalIdExpirationDate" : "4712-12-31",
"WorkEmail" : "[email protected]",
"AddressLine1" : "Oracle Corporation",
"AddressLine2" : "500 Oracle Parkway",
"AddressLine3" : null,
"City" : "San Francisco",
"Region" : "California",
"Region2" : null,
"Country" : "US",
"PostalCode": "94065",
"CitizenshipLegislationCode" : "US",
"PassportNumber" : "11557799",
"PassportIssueDate" : "2002-12-31",
"Religion" : "CHRISTIAN",
"LicenseNumber" : "9955500858",
"DateOfBirth": "1990-09-09",
"Ethnicity": "8",
"Gender" : "F",
"MaritalStatus" : "S",
"NationalIdCountry": "US",
"NationalId": "554-44-3025",
"NationalIdType": "SSN",
"UserName":"rachell",
**"assignments" : **
[
{
"AssignmentName": "Rachell_Assignment_1",
"BusinessUnitId" : "202",
"JobId" : "18",
"WorkerCategory" : "WC",
"AssignmentCategory" : "FR",
"WorkingAtHome" : "N",
"WorkingAsManager" : "N",
"SalaryCode" : "H",
"WorkingHours" : "12",
"Frequency" : "D",
"SalaryAmount" : "100000",
"SalaryBasisId" : "100010024999994",
"ManagerAssignmentId" : "389",
"ManagerId" : "383",
"ActionCode" : "HIRE",
"ActionReasonCode" : "NEWHIRE",
"AssignmentStatus" : "ACTIVE",
"WorkTaxAddressId" : "300100011952957",
"ManagerType" : "LINE_MANAGER"
}
]
}```