
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How can we merge two JSON objects in Java?
A JSON is a lightweight data-interchange format and the format of JSON is a key with value pair. The JSONObject can parse a text from a String to produce a map-like object and supports java.util.Map interface. We can use org.json.simple.JSONObject to merge two JSON objects in Java.
We can merge two JSON objects using the putAll() method (inherited from interface java.util.Map) in the below program.
Example
import java.util.Date; import org.json.simple.JSONObject; public class MergeJsonObjectsTest { public static void main(String[] args) { JSONObject jsonObj = new JSONObject(); // first json object jsonObj.put("Name", "Adithya"); jsonObj.put("Age", 25); jsonObj.put("Address", "Hitech City"); JSONObject jsonObj1 = new JSONObject(); // second json object jsonObj1.put("City", "Hyderabad"); jsonObj1.put("DOB", new Date(104, 3, 6)); jsonObj.putAll(jsonObj1); // merging of first and second json objects System.out.println(jsonObj); } }
Output
{"Address":"Hitech City","DOB":Tue Apr 06 00:00:00 IST 2004,"City":"Hyderabad"," Age":25,"Name":"Adithya"}
- Related Articles
- How can we merge two JSON arrays in Java?
- How we can convert Python objects into JSON objects?
- How can we write JSON objects to a file in Java?\n
- How can we merge two Python dictionaries?
- How can I merge properties of two JavaScript objects dynamically?
- How to merge two JavaScript objects?
- How to merge two JSON strings in an order using JsonParserSequence in Java?
- How can we read a JSON file in Java?
- How can we encode a JSON object in Java?
- How can we decode a JSON object in Java?
- How can we convert a JSON string to a JSON object in Java?
- How can we create a JSON using JsonGenerator in Java?
- How can we parse a nested JSON object in Java?
- How can we ignore the fields during JSON serialization in Java?
- How to merge properties of two JavaScript Objects dynamically?

Advertisements