Hectooorr
Hectooorr

Reputation: 735

Merge JSON files

Does anybody know the best way to merge 2 JSON files? I am looking to move the 'awardClaimForms' from file 1 to file 2 while keeping the same 'number'. Would consider manual work if it was a few but I'm dealing with just over 1k entries. Any direction or help would be appreciated!

File 1 contains:

{
    "Notices": [
        {
            "awardClaimForms": "form-21",
            "number": "2015-031"
        },
        {
            "awardClaimForms": "form22",
            "number": "2015-030"
        },
    ]
}

File 2 contains:

{
    "Notices": [
        {
            "number": "2015-031",
            "link": "http://www.yahoo.com",
            "title": "myother sample title",
            "awardClaimDueDate": "March 01, 2013",
            "datePosted": "12/01/2012"
        },
        {
            "number": "2015-030",
            "link": "http://www.google.com",
            "title": "my sample title",
            "awardClaimDueDate": "March 01, 2013",
            "datePosted": "12/01/2012"
        },
    ]
}

Desired Outcome:

{
    "Notices": [
        {
            "number": "2015-031",
            "link": "http://www.yahoo.com",
            "title": "myother sample title",
            "awardClaimDueDate": "March 01, 2013",
            "awardClaimForms": "form-21",
            "datePosted": "12/01/2012"
        },
        {
            "number": "2015-030",
            "link": "http://www.google.com",
            "title": "my sample title",
            "awardClaimDueDate": "March 01, 2013",
            "awardClaimForms": "form-22",
            "datePosted": "12/01/2012"
        },
    ]
}

Upvotes: 1

Views: 371

Answers (1)

Praveen
Praveen

Reputation: 1485

import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class JsonMerge {

    public static void main(String[] args) throws IOException, JSONException {
// Read the json from the files
        FileInputStream fileOne = new FileInputStream(new File("D:\\jsonOne.json"));
        FileInputStream fileTwo = new FileInputStream(new File("D:\\jsonTwo.json"));
        String stringOne = null, stringTwo = null;
        try {
            stringOne = IOUtils.toString(fileOne);
            stringTwo = IOUtils.toString(fileTwo);
        } finally {
            fileOne.close();
            fileTwo.close();
        }

// Convert them into json objects
        JSONObject jsonOne = new JSONObject(stringOne);
        JSONObject jsonTwo = new JSONObject(stringTwo);

// Extract the arrays from the Notices Array
        JSONArray jsonOneNoticesArray = jsonOne.getJSONArray("Notices");
        JSONArray jsonTwoNoticesArray = jsonTwo.getJSONArray("Notices");
        JSONObject mergedJsonArray = new JSONObject();

// Iterate and compare the required condition
        for (int i = 0; i < jsonOneNoticesArray.length(); i++) {
            for (int j = 0; j < jsonTwoNoticesArray.length(); j++) {
                JSONObject mergedJson = new JSONObject();
                if (jsonOneNoticesArray.getJSONObject(i).getString("number").equals(jsonTwoNoticesArray.getJSONObject(j).getString("number"))) {
                    mergedJson.accumulate("number", jsonOneNoticesArray.getJSONObject(i).getString("number"));
                    mergedJson.accumulate("link", jsonOneNoticesArray.getJSONObject(i).getString("link"));
                    mergedJson.accumulate("title", jsonOneNoticesArray.getJSONObject(i).getString("title"));
                    mergedJson.accumulate("awardClaimDueDate", jsonOneNoticesArray.getJSONObject(i).getString("awardClaimDueDate"));
                    mergedJson.accumulate("datePosted", jsonOneNoticesArray.getJSONObject(i).getString("datePosted"));
                    mergedJson.accumulate("awardClaimForms", jsonTwoNoticesArray.getJSONObject(j).getString("awardClaimForms"));
                    mergedJsonArray.append("Notices", mergedJson);
                    break;
                }
            }

        }
        System.out.println("Done merging.. " + mergedJsonArray);
    }

}

Adding the below line (instead of System.out.println line) could write the output to the file

FileUtils.write(new File("D:\\mergedJson.json"), mergedJsonArray.toString());

and here is the output of the mergedJsonArray from the above code.

enter image description here

Upvotes: 2

Related Questions