Make Selenium Easy

REST Assured Tutorial 57 – Editing Existing JSON Object On The Fly Using JsonNode – Jackson

Introduction

As a part of End to End REST Assured Tutorial, in this post, we will edit or customize existing JSON object on the fly using JsonNode of Jackson API.

Many times we do not want to create POJO classes and we may need to edit an existing JSON quickly as per requirement. In that case, JsonNode provided by Jackson API is really helpful.

Prerequisite

Since we are using Jackson API of Java for this example, make sure you have the latest dependency of Jackson Databind in your project classpath. I have used below Jackson dependency for this post:-

You must go through with below posts first to understand this post better:-

How To Create A JSON Object Using Jackson API – ObjectMapper – CreateObjectNode()

How To Create JSON Array Using Jackson API – ObjectMapper – CreateArrayNode()

Editing JSON using JsonNode

Adding a new key with a primitive value to existing JSON

Suppose we have an existing JSON as below:-

Let’s add a new field “role” with value as “admin“.

We will deserialize the above JSON object as ObjectNode as given JSON is a JSON Object. Please note here that the superclass of ObjectNode is JsonNode.

Output

Adding a new key with a JSON object value to an existing JSON

Let’s add a new key with value as a JSON object. For example – Adding a new field “bookingDetails” with the below value. Basically, we need to add an ObjectNode and the post link under the Prerequisite section explains that concept.

Output

Adding a new key inside a nested JSON Object

In the output of the above program, suppose we need to add another key inside “bookingDetails” key. I.e final output should be as below:-

Update value of existing keys

We can also modify the value of existing keys.

Output

Remove or delete existing key

We can also remove keys from a given JSON Object.

Output

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “REST Assured Tutorial 57 – Editing Existing JSON Object On The Fly Using JsonNode – Jackson

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.