Friday, 26 March 2021

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

No comments:

Post a Comment