json JOLT规范-是否可以根据字典列表的键/值对值进行排序?

w6lpcovy  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(125)

JSON输入:

{
  "list": [
    {
      "tags": [
        {
          "scope": "",
          "tag": "TAG_VM"
        },
        {
          "scope": "",
          "tag": "TAG_HOST"
        },
        {
          "scope": "",
          "tag": "TAG_ROLE_DNS"
        },
        {
          "scope": "",
          "tag": "TAG_ROLE_AD"
        }
      ]
    }
  ],
  "result_count": 1,
  "sort_by": "name"
}

预期输出:

{
  "role1" : "DNS",
  "role2" : "AD"
}

(在最后一个"_"之后的最后一个"字符串"上排序值)
我已经试过了,但它不工作:

[
  {
    "operation": "shift",
    "spec": {
      "list": {
        "*": {
          "tags": {
            "*": {
              "tag": {
                "TAG_ROLE*": "role1"
              }
            }
          }
        }
      }
    }
  }
]

有人知道如何正确使用JOLT吗?我有点搞不懂它了

bvjxkvbb

bvjxkvbb1#

让我们以动态方式构建它,例如:

[
  { // extact literals from the asterisks followed by TAG_ROLE_ through going one level up and take the value of first asterisk(already exists one, but might be more than one) by using &(1 --> level up ,1 --> first asterisk)
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": {
                "TAG_ROLE_*": {
                  "#r": "&(1,1)"
                }
              }
            }
          }
        }
      }
    }
  },
  { // build an array, namely "r" by exchanging key-value pairs, while adding a fake first component to be used for extra increment of the index value, as indexes start from zero, but we need them to start from one in the upcoming spec 
    "operation": "shift",
    "spec": {
      "*": {
        "$": "@(0)"
      },
      "#0": "r"
    }
  },
  { // we tiled the key-value pairs as desired except for the one with zero key
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@": "role&"
        }
      }
    }
  },
  { // get rid of the first(fake) component
    "operation": "remove",
    "spec": {
      "*0": ""
    }
  }
]

相关问题