Search code examples
javajsonspring-bootmodelmapper

How to map Json (from procedure) to Java object


I have the following SP (SQL server) that return a Json output.

BEGIN
SET @jsonOutput = (
SELECT 
    Program.Name AS ProgramName,
    ProgramOwner.FirstName AS OwnerFirstName,
FROM ProgramOwner, Program
WHERE Program.Id = ProgramOwner.ProgramOwner2Program
FOR JSON PATH,WITHOUT_ARRAY_WRAPPER)

I would like to map the return Json output to a List of ProgramDto via modelMapper. Not sure hot to do that since the return values from call.execute is an Object.

Something like this:

SimpleJdbcCall call = new 
SimpleJdbcCall(jdbcTemplate).withProcedureName(programProc).declareParameters(
    new SqlOutParameter("jsonOutput",  Types.VARCHAR));
    Map<String,Object>out = call.execute(new MapSqlParameterSource());
if(out.size()>0) {
    // Only to show what I am trying to do 
    Type rootType = new TypeToken<List<ProgramDto>>() {}.getType();
    modelMapper.map(out.get("jsonOutput"),rootType );
}

Thank you


Solution

  • As I understood you are trying to get a list of object from You can use Jackson api

    Like this

    say for example your json is in variable named jsonData, then you can get the object you need like below.

    ObjectMapper mapper = new ObjectMapper();
    List<Type> myList = Arrays.asList(mapper.readValue(jsonData, Type[].class));
    
    

    You can also find more examples here