1

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":"...
peacetype
  • 1,928
  • 3
  • 29
  • 49
AlleyCat
  • 11
  • 2
  • 1
    render works expected. your `backend map` contains some technical keys like `"this$0"` - just remove them from map – daggett Sep 15 '20 at 22:27
  • 1
    Well it _is_ a map of string to map. I'd proclaim garbage-in-garbage-out. Please provide the code, that builds up the map and then show us, where the result is different from what you have put in. – cfrick Sep 16 '20 at 05:47

1 Answers1

0

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
AlleyCat
  • 11
  • 2