使用 JOLT 将字典转换为对象

Transform Dictionary to Object with JOLT

我想使用 JOLT 转换将字典转换为 JSON 对象。下面我用下面的例子来演示它。

根目录中有一个数组,其中包含来自不同计算机的结果。 “Computer1”、“Computer2”等。

这个结构应该保留。我删除了第二个和更多数组元素,它将以与 {...}.

相同的方式重新出现

给定对象:

[
  {
    "name": "Computer1",
    "events": {
      "counts": [
        {
          "countType": "CRITICAL",
          "count": 5
        },
        {
          "countType": "HIGH",
          "count": 12
        },
        {
          "countType": "LOW",
          "count": 40
        }
      ]
    },
    "processes": {
      "counts": [
        {
          "countType": "CRITICAL",
          "count": 0
        },
        {
          "countType": "HIGH",
          "count": 2
        },
        {
          "countType": "LOW",
          "count": 80
        }
      ]
    }
  },
{
  "name": "Computer2",
    "events": {...},...
}
]

期望的输出:

[
  {
    "name": "Computer1",
    "events": {
      "CRITICAL": 5,
      "HIGH": 12,
      "LOW": 40
    },
    "processes": {
      "CRITICAL": 0,
      "HIGH": 2,
      "LOW": 80
    }
  }
, {
    "name": "Computer2", 
    "events": {...},
    ...
  }
]

请帮助确定正确的 JOLT 规格。

提前致谢 马库斯

您可以使用这样的 shift 转换规范

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "name": "&1.&",
        "events|processes": { // pipe represents "OR" logic
          "counts": {
            "*": {
              "@count": "&4.&3.@countType" // "&4" and "&3" represent going four and three level up the tree to grab the indices of the outermost level list and the key name of the objects("events" and "processes") respectively
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]