pp_01
7 years agoMentor
Reordering a nested JSON in freemarker.
Hey guys. Is there any way so that a nested JSON response can be reordered? I want to rearrange the position of objects from the way I'm getting to a different manner. Suppose my JSON and my requirement is something like what is written below. Is there any way to achieve this. Any code example will be of great help. Thanks in advance.
{ "alphabets": { "a": "1", "b": "2", "c": "3", "d": "4", "e": "5", "f": "6", "g": "7" }, "h": "8", "i": "9", "j": "10", "k": "11", "l": "12" } and I want the output (text) to be like - h: 8 i: 9 j: 10 k: 11 l: 12 alphabets: a: 1 b: 2 c: 3 d: 4 e: 5 f: 6 g: 7
pp_01 Yes, you can achieve this.
e.g You have stored all the json inside a variable. You need keys and reverse function to achieve the result.
<#assign json = {//your json here} />
<#list json?keys?reverse as key> ${key} - ${json[key]} </#list>