Sorting data in multiple objects using an id

Hi all,

I have multiple objects which I need to sort according to an id. Here is an example of my data

{
  "data": {
     "0": {
      "GTIN" : 14235
      "id" : 1
   }
     "1": {
      "GTIN" : 14241
      "id" : 4
   }
     "2": {
      "GTIN" : 14243
      "id" : 2
   }
 }
}

I need to sort the entire data object based on the id fields inside the nested objects. So this should be my outcome:

{
  "data": {
     "0": {
      "GTIN" : 14235
      "id" : 1
   }
     "2": {
      "GTIN" : 14243
      "id" : 2
   }
     "1": {
      "GTIN" : 14241
      "id" : 4
  }
 }
}

I tried several options using JMESpath (sort_by, sort etc.) but can’t get it to work. Is there a way to do this?

Hey @j.wiegmann
JmesPath sorting works only on arrays of data as i last used it.
https://jmespath.org/examples.html#sort-by

Doesn’t quite agree with working directly with objects all the time on functions…
In that case, i would turn the “data” object into an array, and sort it.
Turn it back to an Objects afterwards.
I’ve used the reverse function just as an extra example(remove it if not needed)
export_20240513215321.ndjson (1001 Bytes)

1 Like

This works. Thank you!