Showing posts with label patch. Show all posts
Showing posts with label patch. Show all posts

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

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

Friday, 26 March 2021

Exploring JSON merge patch

JSON merge patch defines the JSON merge patch format and processing rules.  The merge patch format is primarily intended for use with the HTTP PATCH method as a means of describing a set of modifications to a target resource's content.

 

Algorithm to apply a patch json on target json looks like below.

     define MergePatch(Target, Patch):
       if Patch is an Object:
         if Target is not an Object:
       Target = {} # Ignore the contents and set it to an empty Object
         for each Name/Value pair in Patch:
       if Value is null:
         if Name exists in Target:
           remove the Name/Value pair from Target
       else:
         Target[Name] = MergePatch(Target[Name], Value)
         return Target
       else:
         return Patch

 

Reference

https://tools.ietf.org/html/rfc7386

 

 

 

 

Previous                                                    Next                                                    Home

Apply json patch to a source document and generate target document

Follow below steps to apply a patch on source document to generate target document.

 

Source json

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

 

Patch json

[
  {
    "op":"replace",
    "path":"/firstName",
    "value":"Ram"
  },
  {
    "op":"replace",
    "path":"/hobbies/0",
    "value":"Football"
  },
  {
    "op":"replace",
    "path":"/hobbies/1",
    "value":"Chess"
  },
  {
    "op":"replace",
    "path":"/address/city",
    "value":"Amaravathi"
  },
  {
    "op":"replace",
    "path":"/address/state",
    "value":"Andhra Pradesh"
  }
]

 

Follow below step-by-step procedure to apply patch on the source json.

 

Step 1: Get source json value.

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

 

Step 2: Get json patch.

JsonReader jsonReader = Json.createReader(new StringReader(patchJson));
JsonArray jsonArray = jsonReader.readArray();
JsonPatch jsonPatch = Json.createPatch(jsonArray);

Step 3: Apply this patch to source json.

JsonValue targetJson = jsonPatch.apply(sourceJsonValue.asJsonObject());

 

Find the below working application.

 

App.java

package com.sample.app;

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.JsonArray;
import javax.json.JsonPatch;
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) {
        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" + 
                "}\n" + 
                "";
        
        String patchJson = "[\n" + 
                "  {\n" + 
                "    \"op\":\"replace\",\n" + 
                "    \"path\":\"/firstName\",\n" + 
                "    \"value\":\"Ram\"\n" + 
                "  },\n" + 
                "  {\n" + 
                "    \"op\":\"replace\",\n" + 
                "    \"path\":\"/hobbies/0\",\n" + 
                "    \"value\":\"Football\"\n" + 
                "  },\n" + 
                "  {\n" + 
                "    \"op\":\"replace\",\n" + 
                "    \"path\":\"/hobbies/1\",\n" + 
                "    \"value\":\"Chess\"\n" + 
                "  },\n" + 
                "  {\n" + 
                "    \"op\":\"replace\",\n" + 
                "    \"path\":\"/address/city\",\n" + 
                "    \"value\":\"Amaravathi\"\n" + 
                "  },\n" + 
                "  {\n" + 
                "    \"op\":\"replace\",\n" + 
                "    \"path\":\"/address/state\",\n" + 
                "    \"value\":\"Andhra Pradesh\"\n" + 
                "  }\n" + 
                "]\n" + 
                "";
        
        
        JsonValue sourceJsonValue = Json.createReader(new StringReader(sourceJson)).readValue();
        
        JsonReader jsonReader = Json.createReader(new StringReader(patchJson));
        JsonArray jsonArray = jsonReader.readArray();
        JsonPatch jsonPatch = Json.createPatch(jsonArray);
        
        JsonValue targetJson = jsonPatch.apply(sourceJsonValue.asJsonObject());
        
        System.out.println("Source Document");
        System.out.println(format(sourceJsonValue));
        
        System.out.println("\nPatch Array");
        System.out.println(format(jsonPatch.toJsonArray()));
        
        System.out.println("\nTarget Document");
        System.out.println(format(targetJson));
    }
}

 

Output

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

Patch Array
[
  {
    "op":"replace",
    "path":"/firstName",
    "value":"Ram"
  },
  {
    "op":"replace",
    "path":"/hobbies/0",
    "value":"Football"
  },
  {
    "op":"replace",
    "path":"/hobbies/1",
    "value":"Chess"
  },
  {
    "op":"replace",
    "path":"/address/city",
    "value":"Amaravathi"
  },
  {
    "op":"replace",
    "path":"/address/state",
    "value":"Andhra Pradesh"
  }
]

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

 

 

 



 

 

 

 

Previous                                                    Next                                                    Home

Create a patch document from between json documents

In this post, I am going to explain how to get a patch json document from source and target json documents.

 

Source json

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

 

Target json

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

 

Follow below step-by-step procedure to generate a patch document from source and target json documents.

 

Step 1: Get JsonValue instances from source and target jsons.

JsonValue sourceValue = Json.createReader(new StringReader(sourceJson)).readValue();
JsonValue targetValue = Json.createReader(new StringReader(targetJson)).readValue();

 

Step 2: Get JsonPatch using createDiff method.

JsonPatch jsonPatch = Json.createDiff(sourceValue.asJsonObject(), targetValue.asJsonObject());

 

Find the below working application.

 

App.java

package com.sample.app;

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.JsonPatch;
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) {
        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" + 
                "}\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 sourceValue = Json.createReader(new StringReader(sourceJson)).readValue();
        JsonValue targetValue = Json.createReader(new StringReader(targetJson)).readValue();

        JsonPatch jsonPatch = Json.createDiff(sourceValue.asJsonObject(), targetValue.asJsonObject());
        System.out.println(format(jsonPatch.toJsonArray()));

    }

}

 

Output

[
  {
    "op":"replace",
    "path":"/firstName",
    "value":"Ram"
  },
  {
    "op":"replace",
    "path":"/hobbies/0",
    "value":"Football"
  },
  {
    "op":"replace",
    "path":"/hobbies/1",
    "value":"Chess"
  },
  {
    "op":"replace",
    "path":"/address/city",
    "value":"Amaravathi"
  },
  {
    "op":"replace",
    "path":"/address/state",
    "value":"Andhra Pradesh"
  }
]

 

 

 

 

 

 

 

  

Previous                                                    Next                                                    Home