Sunday 28 March 2021

Get Json merge patch

Follow below step-by-step procedure to get json merge patch.

 

Step 1: Get JsonValue instances from source and target json documents.

 

JsonValue sourceJsonValue = Json.createReader(new StringReader(sourceJson)).readValue();

JsonValue targetJsonValue = Json.createReader(new StringReader(targetJson)).readValue();

 

Step 2: Get JsonMergePatch instance from source and target json value instances

JsonMergePatch jsonMergePatch = Json.createMergeDiff(sourceJsonValue, targetJsonValue);

 

Find the below working application.

 

App.java

package com.sample.app;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.Map;
import java.util.Scanner;

import javax.json.Json;
import javax.json.JsonMergePatch;
import javax.json.JsonValue;
import javax.json.JsonWriter;
import javax.json.JsonWriterFactory;
import javax.json.stream.JsonGenerator;

public class App {

  public static String format(JsonValue jsonValue) {
    StringWriter stringWriter = new StringWriter();
    prettyPrint(jsonValue, stringWriter);
    return stringWriter.toString();
  }

  public static void prettyPrint(JsonValue jsonValue, Writer writer) {
    Map<String, Object> config = Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true);
    JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(config);
    try (JsonWriter jsonWriter = jsonWriterFactory.createWriter(writer)) {
      jsonWriter.write(jsonValue);
    }
  }

  public static void main(String[] args) throws FileNotFoundException {
    
    String sourceJson = "{\n" + 
        "  \"firstName\": \"Krishna\",\n" + 
        "  \"lastName\": \"Gurram\",\n" + 
        "  \"hobbies\": [\n" + 
        "    \"Cricket\",\n" + 
        "    \"Football\"\n" + 
        "  ],\n" + 
        "  \"address\": {\n" + 
        "    \"city\": \"Bangalore\",\n" + 
        "    \"state\": \"Karnataka\",\n" + 
        "    \"country\": \"India\"\n" + 
        "  }\n" + 
        "}";
    
    String targetJson = "{\n" + 
        "  \"firstName\": \"Ram\",\n" + 
        "  \"lastName\": \"Gurram\",\n" + 
        "  \"hobbies\": [\n" + 
        "    \"Football\",\n" + 
        "    \"Chess\"\n" + 
        "  ],\n" + 
        "  \"address\": {\n" + 
        "    \"city\": \"Amaravathi\",\n" + 
        "    \"state\": \"Andhra Pradesh\",\n" + 
        "    \"country\": \"India\"\n" + 
        "  }\n" + 
        "}";
    
    
    JsonValue sourceJsonValue = Json.createReader(new StringReader(sourceJson)).readValue();
    JsonValue targetJsonValue = Json.createReader(new StringReader(targetJson)).readValue();
    
    JsonMergePatch jsonMergePatch = Json.createMergeDiff(sourceJsonValue, targetJsonValue);
    
    System.out.println("Source Document");
    System.out.println(format(sourceJsonValue));
    
    System.out.println("\nTarget Document");
    System.out.println(format(targetJsonValue));
    
    System.out.println("\nMerge patch Document");
    System.out.println(format(jsonMergePatch.toJsonValue()));
  }
}

 

Output

Source Document

{
    "firstName": "Krishna",
    "lastName": "Gurram",
    "hobbies": [
        "Cricket",
        "Football"
    ],
    "address": {
        "city": "Bangalore",
        "state": "Karnataka",
        "country": "India"
    }
}

Target Document

{
    "firstName": "Ram",
    "lastName": "Gurram",
    "hobbies": [
        "Football",
        "Chess"
    ],
    "address": {
        "city": "Amaravathi",
        "state": "Andhra Pradesh",
        "country": "India"
    }
}

Merge patch Document

{
    "firstName": "Ram",
    "hobbies": [
        "Football",
        "Chess"
    ],
    "address": {
        "city": "Amaravathi",
        "state": "Andhra Pradesh"
    }
}

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment