0
votes

I am a complete Freemarker newbie and I'm using a framework that uses freemarker template. I'm trying to sort a sequence of hashes based on a certain field "date".

My input json looks like below:

{"fields": [
                [
                    {
                        "contentType": "application/json",
                        "date": 1.563457325E9,
                        "id": "abc",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563426843E9,
                        "id": "def",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563454092E9,
                        "id": "ghi",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563425862E9,
                        "id": "jkl",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563426128E9,
                        "id": "mno",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563453696E9,
                        "id": "pqr",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563426813E9,
                        "id": "stu",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563426177E9,
                        "id": "vwx",
                        "size": 0.0
                    }
                ]
            ]
        }

When I'm trying to do this: <#assign j=fields[0].eval> , I am getting the below error:

Failed to "?eval" string with this error: ---begin-message--- Syntax error in ?eval-ed string in line 1, column 55: Encountered "E9", but was expecting one of: ".." ".." "," "}" "." "[" "(" "?" "!" "??" "+" "-" "" "/" "%" "!=" "=" "==" ">=" ">" ---end-message--- The failing expression: ==> fields[0]?eval [in template "89-1070010335" at line 1, column 14] ---- FTL stack trace ("~" means nesting-related)

I want to do something like this:

<#assign j=fields[0]>
<#list j?sort_by("date") as i>
  ${i.date}: ${i.id}
</#list>

How can I convert the date field from scientific notation to a number in freemarker and then sort_by based on the value of this date field?

I would be glad if I can get some pointers or any particular reference from the Freemarker documentation note.

1

1 Answers

1
votes

As Data model contain nested lists, so we have to take nested list directives in the template also like below.

  <#list fields as field>
    <#list field?sort_by("date") as innerField>
      ${innerField.date?replace(",", "")} : ${innerField.id}
    </#list>
    </#list>

Below snippet converts the scientific notation to number.

<#assign scientificFormat = "1.563426177E9">
<#assign number = scientificFormat?number?replace(",", "")>
${number}

Source: https://freemarker.apache.org/docs/ref_builtins_sequence.html