Sorting and grouping json objects


#1

Hello,

I have got a JSON data structure with

{
    "a long key 1" : {
                                  "group" : "keyprefix1"
                                  "description" : "a text"
   },
  "another key 2" : {
                                  "group" : "keyprefix2"
                                  "description" : "a text"
  },
  ...
}

I I would like to create a HTML list:

<ul>
     <li> keyprefix 1
           <ul>
                // here all elements with keyprefix1
           </ul> 
    </li>
     <li> keyprefix 2
           <ul>
                // here all elements with keyprefix2
           </ul> 
    </li>

How can I group all JSON objects with equal attribute “group”?

Thanks


#2

Have you tried using the sort function? If sort doesn’t support your use case, we can look at extending it.

{{ range sort .Data.whatever "group" }}

#3

I have used sort, but sort removes the key value of the json document. The sort function replaces the key with a number:

 range $key, $value := .Data.document

In this case I get in $key, the json key values but in

range $key, $value := sort .Data.document "group"

I get in $key a number in [0, number of objects), so I lost the key. I have put inside the $value the key also and so I can solve the problem