Search code examples
jsongrailsgroovygsp

Access JSON via a variable within a gsp


There is following JSON structure given:

"filter": {
    "1": {
        "value": "swiss"
    },
    "2": {
        "value": "google"
    }
}

How to access the variable "value" via an index variable within a gsp template like this?

<g:each in="${...}" var="data" status="i">
    ${filter?.i?.value} 
</g:each>

Solution

  • If your "key" there is a variable you can use a GString notation like for any other property referenced by variable:

    ${filter?."$i"?.value}