json 如何在Jolt中按字母降序对数组进行排序

cgvd09ve  于 2023-05-23  发布在  其他
关注(0)|答案(2)|浏览(135)

我是Jolt的新手。
请问我如何 * 排序 * 以下数组在***降序***字母顺序

输入

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

我希望status REJECT是第一个对象,因此输出为:

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

我看过joltsort操作:

{
  "operation": "sort"
}

但是,我如何将它指向状态数组,并指定我需要一个降序排序?

41zrol4v

41zrol4v1#

XPath 3.1可以执行that

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

或者与

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

当然,XSLT3.0或XQuery3.1中也可以使用任何XPath3.1解决方案。我看到你删除了xslt标记,而我写的答案,但我会张贴它作为一个替代无论如何。

jgwigjjp

jgwigjjp2#

如果是升序*的情况,那么您可以轻松地将数组的每个组件与它们自己匹配,以在shift转换中创建"ERROR" : "ERROR""REJECT" : "REJECT"对,并应用sort转换。
但是,不幸的是,没有直接的方法来获得
降序*,而可以通过反向索引数组的组成部分来给出一种变通方法,例如

[
  { // 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"
        }
      }
    }
  }
]

相关问题