I have multiple JSON file like below and want to merge the files and store the data from it into a new single json file as mentione below:
JSON1:
{
"jobRunID" : "1940",
"mappingResult": [
{
"studyID": "ca209-030",
"fileName": "aesae.csv",
"ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/aesae.csv",
"columnName": "ae_start_dt",
"currentMapping": "ae_start_dt,ae_st_dt",
"isMapped": "N",
"timeStamp": "20180827093033"
},
{
"studyID": "ca209-004",
"fileName": "labressae.csv",
"ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/labressae.csv",
"columnName": "ae_verbatim",
"currentMapping": "ae_vtm,ae_vt",
"isMapped": "N",
"timeStamp": "20180827093033"
}
]
}
JSON2:
{
"jobRunID" : "1940",
"mappingResult": [
{
"studyID": "ca209-030",
"fileName": "aesae.csv",
"ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/aesae.csv",
"columnName": "ae_start_dt",
"currentMapping": "ae_start_dt,ae_st_dt",
"isMapped": "N",
"timeStamp": "20180827093033"
},
{
"studyID": "ca209-004",
"fileName": "labressae.csv",
"ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/labressae.csv",
"columnName": "ae_verbatim",
"currentMapping": "ae_vtm,ae_vt",
"isMapped": "N",
"timeStamp": "20180827093033"
}
]
}
Expected O/P:
{
"jobRunID": "1940",
"mappingResult": [
{
"studyID": "ca209-030",
"fileName": "aesae.csv",
"ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/aesae.csv",
"columnName": "ae_start_dt",
"currentMapping": "ae_start_dt,ae_st_dt",
"isMapped": "N",
"timeStamp": "20180827093033"
},
{
"studyID": "ca209-004",
"fileName": "labressae.csv",
"ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/labressae.csv",
"columnName": "ae_verbatim",
"currentMapping": "ae_vtm,ae_vt",
"isMapped": "N",
"timeStamp": "20180827093033"
},
{
"studyID": "ca209-030",
"fileName": "aesae.csv",
"ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/aesae.csv",
"columnName": "ae_start_dt",
"currentMapping": "ae_start_dt,ae_st_dt",
"isMapped": "N",
"timeStamp": "20180827093033"
},
{
"studyID": "ca209-004",
"fileName": "labressae.csv",
"ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/labressae.csv",
"columnName": "ae_verbatim",
"currentMapping": "ae_vtm,ae_vt",
"isMapped": "N",
"timeStamp": "20180827093033"
}
]
}
Any help will be appreciated as i am new handling JSON files in JAVA. As per the above JOSNs for all the same JobID i want to merge the mappingResult from both the files in one o/p file as explained above in expected o/p.
I am trying with java by using below code but it read well for 2 input files but not sure how to di for multiple json file :
public class MergeJSON {
@JsonMerge
List<Integer> contacts;
public List<Integer> getContacts() {
return contacts;
}
public void setContacts(List<Integer> contacts) {
this.contacts = contacts;
}
@Override
public String toString() {
return contacts.toString();
}
public static void main(String[] args) throws JsonProcessingException, IOException {
TypeReference<Map<String, MergeJSON>> type = new TypeReference<Map<String, MergeJSON>>() {};
InputStream input = new ClassPathResource("C:\\Users\\sweta.h.sharma\\Test.json").getInputStream();
InputStream input2 = new ClassPathResource("C:\\Users\\sweta.h.sharma\\Test1.json").getInputStream();
ObjectMapper mapper = new ObjectMapper();
Object contacts = mapper.readValue(input, type);
mapper.reader(type)
.withValueToUpdate(contacts)
.readValues(input2);
System.out.println(contacts);
}
}