Header Ad Section

How to Consume Dynamic JSON Objects Passing to Controller


Introduction

Through this article we're going to discuss how to access and consume dynamic JSON objects passing to the backend Controllers from the frontend. These are kind of situations that we can't exactly guarantee the no of parameters count we're going to receive from the request.

Prerequisites

I'll use SpringBoot RestController to demonstrate the process we're going to make happen at the controller level.

As a prerequisite,  you only need to have the "spring-web" dependency. No other additional dependency is used here to perform this task. So, please add this dependency to your pom.xml file of your project.


<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>


Getting Started

So, in this scenario, we're going to expecting dynamic JSON object formats which the attribute count may be vary from time to time (not having a specific static JSON format). So, let's assume that the below mentioned kind of an JSON object need to be accessed and consume from the backend controller.




So, since object structure is dynamic(changing time to time), we have to define a common Super type object that we can access any providing object easily. So, in that case I've used "java.lang.Object" as the common format object to catch data since it is the most super type of any Object format in Java. So, we're going to map the json object values comes from the Request Body to "java.lang.Object" type (@RequestBody Object o). 

So, now the data binding part is completed since we can now catch any format of the data to a super type Object. But still we can't access those data which is coming from the JSON mapped as Key-Value pairs. So, in order to access data separately, we've to cast our previously catch object into a LinkedHashMap (That's the default data type of a JSON format object contains).  So, then we takes the data to a LinkedHashMap type by casting the object data(LinkedHashMap<String, Object> hashMap = (LinkedHashMap<String, Object>) o).




import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;

@RestController
@RequestMapping("/api/v1/test")
public class TestController {


@PostMapping(value = "/submit", consumes = MediaType.APPLICATION_JSON_VALUE,
                                    produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> submitData(@RequestBody Object o) {
System.out.println(o.toString());
LinkedHashMap<String, Object> hashMap = (LinkedHashMap<String, Object>) o;
System.out.println(hashMap.get("name"));
System.out.println(hashMap.get("age"));
System.out.println("John".equals(hashMap.get("name")));
System.out.println(12 >= (Integer) hashMap.get("age"));
System.out.println(12 <= (Integer) hashMap.get("age"));

ArrayList<String> addresses = (ArrayList<String>) hashMap.get("addressLines");  
ArrayList<Integer> mobileNos = (ArrayList<Integer>) hashMap.get("mobileNos");  

System.out.println(addresses.contains("New York"));
System.out.println(mobileNos.contains(18884521570
)); // 1-888-452-1570

return ResponseEntity.ok(o.toString());
}
}


So, in that case we can access each attribute one by one as we're now consuming the data through a Map type Object. So, that's the whole process we need to to do in order to access and read a dynamic request content data.





Keywords: json  json format  jsonviewer  java  intellij  spring framework  learn java  java basics 




Post a Comment

0Comments