Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

How to Merge/Concat Multiple JSONObjects in Java? Best way to Combine two JSONObjects

$
0
0
How to Merge:Concat Multiple JSONObjects in Java? Best way to Combine two JSONObjects

In Java – What is the best way to combine / merge multiple JSONObjects? JSONObject is an unordered collection of name/value pairs and widely used in Java Enterprise applications for data transfer between client / server over REST interface.

You need below Maven Dependency for JSONObject.

<dependency>
	<groupId>org.json</groupId>
	<artifactId>json</artifactId>
	<version>20140107</version>
</dependency>

Sometimes it will be great to have handy utility ready which combines two or multiple JSONObjects. Here is simple Java example which shows the same.

package crunchify.com.tutorials;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author Crunchify.com
 * Program: How to Merge/Concat Multiple JSONObjects in Java? Best way to Combine two JSONObjects.
 */
public class CrunhifyMergeJSONObjects {

	public static void main(String[] args) {

		// JSONObject(): Construct an empty JSONObject.
		JSONObject crunchifyJSON1 = new JSONObject();
		JSONObject crunchifyJSON2 = new JSONObject();

		// put(): Put a key/value pair in the JSONObject.
		// If the value is null, then the key will be removed from the JSONObject if it is present.
		crunchifyJSON1.put("Crunchify", "Java Company");
		crunchifyJSON1.put("Google", "Search Company");
		crunchifyJSON1.put("Yahoo", "Web Company");

		crunchifyJSON2.put("Facebook", "Social Network Company");
		crunchifyJSON2.put("Twitter", "Another Social Company");
		crunchifyJSON2.put("Linkedin", "Professional Network Company");

		System.out.println("json1: " + crunchifyJSON1);
		System.out.println("json2: " + crunchifyJSON2);

		JSONObject mergedJSON = mergeJSONObjects(crunchifyJSON1, crunchifyJSON2);
		System.out.println("\nmergedJSON: " + mergedJSON);
	}

	public static JSONObject mergeJSONObjects(JSONObject json1, JSONObject json2) {
		JSONObject mergedJSON = new JSONObject();
		try {

			// getNames(): Get an array of field names from a JSONObject.
			mergedJSON = new JSONObject(json1, JSONObject.getNames(json1));
			for (String crunchifyKey : JSONObject.getNames(json2)) {

				// get(): Get the value object associated with a key.
				mergedJSON.put(crunchifyKey, json2.get(crunchifyKey));
			}

		} catch (JSONException e) {

			// RunttimeException: Constructs a new runtime exception with the specified detail message.
			// The cause is not initialized, and may subsequently be initialized by a call to initCause.
			throw new RuntimeException("JSON Exception" + e);
		}
		return mergedJSON;
	}
}

Eclipse Console:

Just run above program as a Java Application and you will see result as below.

json1: 
{
    "Crunchify": "Java Company",
    "Yahoo": "Web Company",
    "Google": "Search Company"
}
json2: 
{
    "Linkedin": "Professional Network Company",
    "Facebook": "Social Network Company",
    "Twitter": "Another Social Company"
}

mergedJSON: 
{
    "Crunchify": "Java Company",
    "Linkedin": "Professional Network Company",
    "Yahoo": "Web Company",
    "Facebook": "Social Network Company",
    "Google": "Search Company",
    "Twitter": "Another Social Company"
}

Next I’ll write example on ObjectMapper which PrettyPrint JSON object in Eclipse Console.

The post How to Merge/Concat Multiple JSONObjects in Java? Best way to Combine two JSONObjects appeared first on Crunchify.


Viewing all articles
Browse latest Browse all 1037

Trending Articles