How to render a nested map as JSON from the Controller

278 Views Asked by At

This solution does not work for a nested map. I'm trying to convert to JSON. I need to send this nested map because it contains a lot that I need for a graph which I'm going to make with plotly on the frontend.

This is what the nestedMap looks like on the backend:

Map<String, Map<String, Object>> nestedMap

Although after I passed it to the controller, I declared it as def nestedMap before I tried to render it. At the end of the controller I tried render nestedMap as JSON.

What I receive after my AJAX call:

{"FirstSample":
{"S1":
{"avg":2.367333,"computation":-7.314434,"computedAvg":9.26567,"this$0":
{"transactionManager: 
{"class":"org.codehaus.groovy.grails.transaction.ChainedTransactionManager",
"transactionManagers":[
{"dataSource":
{"class":"org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy",
"connection": 
{"autoCommit":true,"catalog":null,"class":"...
1

There are 1 best solutions below

0
On

This problem happens because the Map that is being rendered with JSON contains an object. I changed the map that I am trying to convert to JSON to MultiMap<String, List<String>> nestedMap to account for the multiple values I will need on the front-end. After this occured render nestedMap as JSON was able to work. So don't have an object in the Map if it is declared as def.

def nestedMap = [:]
render nestedMap as JSON