Sunday 28 March 2021

Apply json merge patch on the document

 

Source json

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

 

Patch json

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

 

Let’s apply the patch json document on source json to produce target json.

 

Step 1: Get JsonValue instance from source json string

 

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

 

Step 2: Get JsonMergePatch instance from the patch json document.

 

JsonReader jsonReader = Json.createReader(new StringReader(patchJson));

JsonValue patchValue = jsonReader.readValue();

JsonMergePatch jsonMergePatch = Json.createMergePatch(patchValue);

 

Step 3: Apply the patch on source json.

JsonValue targetJsonValue = jsonMergePatch.apply(sourceJsonValue);

Find the below working application.

 

App.java 

package com.sample.app;

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 javax.json.Json;
import javax.json.JsonMergePatch;
import javax.json.JsonReader;
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 patchJson = "{\n" + 
            "    \"firstName\": \"Ram\",\n" + 
            "    \"hobbies\": [\n" + 
            "        \"Football\",\n" + 
            "        \"Chess\"\n" + 
            "    ],\n" + 
            "    \"address\": {\n" + 
            "        \"city\": \"Amaravathi\",\n" + 
            "        \"state\": \"Andhra Pradesh\"\n" + 
            "    }\n" + 
            "}\n" + 
            "";
    
    
    JsonValue sourceJsonValue = Json.createReader(new StringReader(sourceJson)).readValue();
    
    JsonReader jsonReader = Json.createReader(new StringReader(patchJson));
    JsonValue patchValue = jsonReader.readValue();
    JsonMergePatch jsonMergePatch = Json.createMergePatch(patchValue);

    JsonValue targetJsonValue = jsonMergePatch.apply(sourceJsonValue);
    
    System.out.println("Source Document");
    System.out.println(format(sourceJsonValue));
    
    System.out.println("\nMerge patch Document");
    System.out.println(format(jsonMergePatch.toJsonValue()));
    
    System.out.println("\nTarget Document");
    System.out.println(format(targetJsonValue));
    
  }
}

 

Output

Source Document

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

Merge patch Document

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

Target Document

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

 


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment