Search code examples
javaspring-bootorg.json

How to return a list of JSONObject as REST API response


I am creating a REST API using Spring Boot and using org.json for parsing data retrieved from another different service. From this service I am getting JSON data like in following format

   {

"my_data":[
{
"user_data":{
"first_name":"FirstTest1",
            "last_name":"LastTest1",
            "age":"25"
}
},
{
"user_data":{
            "first_name":"FirstTest2",
            "last_name":"LastTest2",
            "age":"35"
            }
},{
"user_data":{
            "first_name":"FirstTest3",
            "last_name":"LastTest3",
            "age":"45"
            }
}
],
"count":10,
"is_safe":false
}

and I have to transform received data to the following JSON

[
{
"user_data":{
"first_name":"FirstTest1",
            "last_name":"LastTest1",
            "age":"25"
}
},
{
"user_data":{
            "first_name":"FirstTest2",
            "last_name":"LastTest2",
            "age":"35"
            }
},{
"user_data":{
            "first_name":"FirstTest3",
            "last_name":"LastTest3",
            "age":"45"
            }
}
]

I know I can use a POJO to map the data and send it (already doing this) but here the issue is that the data received from another service is not fixed e.g. it may or may mot have "first_name" or may have a different field like "country". So, in this situation I can not make POJO beforehand.

After going through some online resources I made some changes and my POST Controller method looks like this.

@PostMapping(path = "/searchusersdata")
public RETURN_SOMETHING  searchUsersData(@RequestBody Map<String, String> searchData) {

List<JSONObject>  finalDataCollection = new ArrayList<JSONObject>();

//Making some REST API CALL TO GET 'response' using 'searchData'

String someResponse = response.getBody();

            JSONObject object = null;
            
            try {
                 object = new JSONObject(someResponse);
            } catch (JSONException  e) {
                e.printStackTrace();
            }
            
            String my_data= object.get("my_data").toString();           
            
            JSONArray  intermediateJA = null;
            
            intermediateJA = new JSONArray (my_data);
            
            for(int i = 0; i < intermediateJA.length(); i++) {
                
                JSONObject item = intermediateJA.getJSONObject(i);
                
                if (item.keySet().contains("user_data"))
                {
                    Object value = item.get("user_data");
                    finalDataCollection.add(new JSONObject(value));
                }
                
            }
            
            
            //WHAT TO RESTURN HERE
            }

Now, I don't know what to return hare. For a single JSONObject we can use return new ResponseEntity<>(return_data.toMap(), HttpStatus.OK); but for a collection I don't know. I am open to suggestion if I have to do it in entirely different way. I also know that with gson or jackson it might be easier but I have to use org.json.


Solution

  • instead of List , use JsonArray and use ResponseEntity to return it.

    Example

            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            jsonArray.put(jsonObject);
            return new ResponseEntity( jsonArray.toString(), HttpStatus.OK);