How to read multiple JSON files and write it to a single JSON file using java

How to read multiple JSON files and write it to a single JSON file using java



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);







Hi, this question will be downvoted because you haven't included any details of your attempt to solve it - see stackoverflow.com/help/how-to-ask
– Joel Berkeley
Aug 29 at 10:43




1 Answer
1



Use Jackson Api.

Create Pojo class same as your json object (Class should have members like 'jobRunID','mappingResult').


//class JsonObj with getters & setters
String jobRunID;
List<Mappingresult> mappingResult; // Mappingresult is again POJO class with members like studyID,fileName etc



This are classes you have to use


com.fasterxml.jackson.core.JsonFactory;
com.fasterxml.jackson.core.JsonParser;
com.fasterxml.jackson.databind.ObjectMapper;



And Code.


ObjectMapper objMapper=new ObjectMapper();
JsonFactory jfactory = new JsonFactory();
JsonParser jParser1=jfactory.createJsonParser(jsonString1); //json String 1
JsonParser jParser2=jfactory.createJsonParser(jsonString2); //json String 2

JsonObj obj1=objMapper.readValue(jParser1,JsonObj.class);// JsonObj is Pojo for your jsonObject
JsonObj obj2=objMapper.readValue(jParser2,JsonObj.class);



obj1 and obj2 will have list mappingResult as list. You can merge those list into one list and then can write in file or print on console



EDIT :



To get back the result as String, you can use ObjectMapper::writeValueAsString


obj1.getMappingResult().putAll(obj2.getMappingResult());
String jsonString = objMapper.writeValueAsString(obj1);





What is jsonString1 and jsonString2 ? Also if there are n no. of json file how it will work ?
– Shweta Sharma
Aug 29 at 12:14





@ShwetaSharma jsonString is Your Json text in form of String which you are reading from "C:\Users\sweta.h.sharma\Test.json"
– Tarun
Aug 29 at 12:47






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages