https://github.com/hemantsonu20/json-merge
A light weight library to merge two json objects into a single json object.
https://github.com/hemantsonu20/json-merge
hacktoberfest hacktoberfest2020 java json json-merge maven
Last synced: 4 months ago
JSON representation
A light weight library to merge two json objects into a single json object.
- Host: GitHub
- URL: https://github.com/hemantsonu20/json-merge
- Owner: hemantsonu20
- License: apache-2.0
- Created: 2020-08-21T12:33:42.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-30T10:00:43.000Z (almost 3 years ago)
- Last Synced: 2025-08-02T23:36:49.029Z (4 months ago)
- Topics: hacktoberfest, hacktoberfest2020, java, json, json-merge, maven
- Language: Java
- Homepage:
- Size: 135 KB
- Stars: 7
- Watchers: 1
- Forks: 6
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README

[](https://search.maven.org/search?q=g:%22com.github.hemantsonu20%22%20AND%20a:%22json-merge%22)
[](https://javadoc.io/doc/com.github.hemantsonu20/json-merge)
# json-merge
A light weight library to merge two json objects into a single json object.
## Overview
This library merges two json of any nested level into a single json following below logic.
* When keys are different, both keys with there values will be copied at same level.
* When keys are same at some level, following table denotes what value will be used.
| Src / Target | JSON Value | JSON Array | JSON Object |
|----------------------- |------------------|-------------------|-------------------|
| JSON Value1 | Src | Src | Src |
| JSON Array | Src2 | Merge | Src |
| JSON Object | Src | Src | Merge3 |
**1** Json Value denotes boolean, number or string value in json.
**2** Src denotes `Src` value will be copied.
**3** Merge denotes both `Src` and `Target` values will be merged.
## Maven Artifact
To use this library add below to your dependencies in `pom.xml`.
```xml
com.github.hemantsonu20
json-merge
1.0.2
```
## Usage
This library exposes below method. It accepts source and target json and returns the merged json.
```java
public static String merge(String srcJsonStr, String targetJsonStr);
```
**Method Usage**
```java
String srcJsonStr = "{\"name\":\"json-merge\"}";
String targetJsonStr = "{\"age\":18}";
String output = JsonMerge.merge(srcJsonStr, targetJsonStr);
// {"name":"json-merge","age":18}
```
## Examples
#### Example 1
**Source Json**
```json
{
"name": "json-merge-src"
}
```
**Target Json**
```json
{
"name": "json-merge-target"
}
```
**Output**
```json
{
"name": "json-merge-src"
}
```
#### Example 2
**Source Json**
```json
{
"level1": {
"key1": "SrcValue1"
}
}
```
**Target Json**
```json
{
"level1": {
"key1": "targetValue1",
"level2": {
"key2": "value2"
}
}
}
```
**Output Json**
```json
{
"level1": {
"key1": "SrcValue1",
"level2": {
"key2": "value2"
}
}
}
```
For more examples see, [test json files](/src/test/resources/json-merge-test).
Each test json file contains a json array of three json objects, first is src, second is target and third is output json.
## Documentation
* Javadoc Releases are available at [javadoc.io](https://javadoc.io/doc/com.github.hemantsonu20/json-merge). (See javadoc badge above)
* Javadoc for latest code available via github pages [here](https://hemantsonu20.github.io/json-merge/apidocs/).
## Contributing
[See Contributing Guidelines](/CONTRIBUTING.md)
## License
[Apache License Version 2.0](/LICENSE)