0

I am new to Jolt.

Please how do I sort the following array in descending alphabetical order

Input :

[
  {
    "status": [
      "ERROR",
      "ERROR",
      "REJECT",
      "ERROR",
      "ERROR",
      "ERROR",
      "ERROR"
    ]
  }
]

I want status REJECT to be the first object, so the output is:

[
  {
    "status": [
      "REJECT",
      "ERROR",
      "ERROR",
      "ERROR",
      "ERROR",
      "ERROR",
      "ERROR"
    ]
  }
]

I have seen the jolt sort operation:

{
  "operation": "sort"
}

But, how to I point it at the status array and also specify I need a descending sort please?

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Mega
  • 1,304
  • 5
  • 22
  • 37

2 Answers2

0

XPath 3.1 can do that:

[ 
 map { 'lender-status' : array { sort(?1?status?*) => reverse() } }
]

Or with

[ 
 map { 'lender-status' : array:sort(?1?status) => array:reverse() }
]

Of course any XPath 3.1 solution can also be used in XSLT 3.0 or XQuery 3.1. I see you removed the xslt tag while I wrote the answer but I will post it as an alternative anyway.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
0

If ascending order was the case, then you can easily match each components of the array with theirselves to make "ERROR" : "ERROR" and "REJECT" : "REJECT" pairs within a shift transformation, and apply sort transformation.

But, unfortunately there's no direct method to get the descending order whereas a workaround might be given through reversely indexing the components of the array such as

[
  { // reform subarrays with repeating components and having keys identical to those repeated components
    // each subarray will be sorted in alphabetical order spontaneously 
    "operation": "shift",
    "spec": {
      "*": {
        "status": {
          "*": {
            "*": {
              "$": "&3.&[]"
            }
          }
        }
      }
    }
  },
  { // convert arrays to attributes by matching each common key with each component 
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "&2.&1&"
        }
      }
    }
  },
  { // convert whole content to a unique array back, named "status" 
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@": "&2"
        }
      }
    }
  },
  { // prepare "key" and "val" arrays to be used within the upcoming specs
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "$": "&2.key.&",
          "@": "&2.val"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "sz__": "=size(@(1,key))",
        "sz_": "=divide(@(1,sz__),-1)",
        "sz": "=toInteger(@(1,sz_))",
        "key": {
          "*": "=intSum(@(1,&),@(2,sz))"
        }
      }
    }
  },
  { // get "key" and "val" arrays
    "operation": "shift",
    "spec": {
      "*": {
        "key": {
          "*": {
            "@": "&3.&2"
          }
        },
        "*": "&1.&"
      }
    }
  },
  { // match each components of those arrays among them
    "operation": "shift",
    "spec": {
      "*": {
        "key": {
          "*": {
            "@(2,val[&])": "&3.@(3,key[&])"
          }
        }
      }
    }
  },
  { // sort by keys
    "operation": "sort"
  },
  { // convert attributes back to a unique array 
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@": "&2"
        }
      }
    }
  }
]

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
  • 1
    Barbaros. Thanks so much for this. Its appreciated. I will share some details soon. – Mega May 23 '23 at 06:34