bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.56k stars 329 forks source link

Sort wont work on an Array of complex Objects #1254

Open samer1977 opened 6 months ago

samer1977 commented 6 months ago

Hi ,

The sort spect or the the list sort function in the modify overwrite beta spec wont sort if the list is complex objects.

Example:

Input:

{
  "items": [
    {
      "2": "2 value"
    },
    {
      "1": "1 value"
    }
  ]
}

Sort Spec:

[
  {
    "operation": "sort"
  }
]

Output:


{
  "items" : [ {
    "2" : "2 value"
  }, {
    "1" : "1 value"
  } ]
}

Expected:


{
  "items" : [ {
    "1" : "1 value"
  }, {
    "2" : "2 value"
  } ]
}

Also if you try the sort function in the modify-overwrite-beta it wont work as well:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "items": "=sort"
    }
  }
]

If the items array was simple array of ["2","1"] both spec will work. If items changed from an array to complex objects the sort spec will work! . Why only when its array of complex object it doesnt work. to me this is a bug.

barbaros74 commented 6 months ago

Hi, Samer. Yes, sort is such a problematic, lacking operation, but yet a workaround might be given for some cases through use of some extra shift transformation spec for your current case such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "@": ".&3.&1.&"
          }
        }
      }
    }
  },
  {
    "operation": "sort"
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1[]"
      }
    }
  }
]