1

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

}
}
1
  • 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 Commented Aug 29, 2018 at 10:43

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

What is jsonString1 and jsonString2 ? Also if there are n no. of json file how it will work ?
@ShwetaSharma jsonString is Your Json text in form of String which you are reading from "C:\\Users\\sweta.h.sharma\\Test.json"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.