Jolt 规范,用于过滤数组中的 JSON 记录,该数组对于同一对象中的两个不同字段具有相同的值

问题描述 投票:0回答:1

输入 JSON :

[
  {
    "id": "1",
    "name": "a",
    "token": "x"
  },
  {
    "id": "2",
    "name": "b",
    "token": "xx"
  },
  {
    "id": "3",
    "name": "xxx",
    "token": "xxx"
  }
]

需要输出json:

[
  {
    "id": "3",
    "name": "xxx",
    "token": "xxx"
  }
]

期望是仅当名称和令牌具有相同值时才需要记录。请帮助为此过滤器形成一个震动规格文件

过滤后也是如此。是否可以按名称值降序对输出数组中的对象进行排序?

arrays json filter transformation jolt
1个回答
0
投票

您可以使用以下 shift 转换规范

[
  { // generate nodes by the values of "name" and "token" attributes
    "operation": "shift",
    "spec": {
      "*": {
        "@": "@name.@token"
      }
    }
  },
  { // match the values of the outer and inner keys of the objects
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@(2,&)": "[]" // traverse two levels up the tree
        }
      }
    }
  },
  { // get rid of the keys of the objects
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[]"
      }
    }
  }
]

可以通过在最后一个 shift 转换之前添加简单的 sort 转换来升序排序对象,例如

{ 
  "operation": "sort"
}

但不幸的是,没有直接的方法来降序排序。

网站 http://jolt-demo.appspot.com/ 上的 演示 是:

© www.soinside.com 2019 - 2024. All rights reserved.