Friday, 26 March 2021

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

No comments:

Post a Comment