json Jolt转换-将对象移动到数组

xghobddn  于 2022-11-26  发布在  其他
关注(0)|答案(2)|浏览(228)

我的结论是乔特超越了我。
使用此输入数据:-

{
  "cluster_id": "1",
  "data": {
    "id": 1,
    "types": [
      {
        "incident_id": 10,
        "incident_ref": "AAA",
        "incident_code": "123",
        "incident_date": "2010-11-15T00:01:00Z"
      },
      {
        "incident_id": 20,
        "incident_ref": "BBB",
        "incident_code": "456",
        "incident_date": "2020-11-15T00:01:00Z"
      }
    ]
  }
}

规格:-

[
  {
    "operation": "shift",
    "spec": {
      "cluster_id": "id",
      "data": {
        "types": {
          "*": {
            "incident_id": "incidents",
            "incident_ref": "incidents"
          }
        }
      }
    }
  }
]

提供:-

{
  "id" : "1",
  "incidents" : [ 10, "AAA", 20, "BBB" ]
}

如何获得以下结果:-

{
  "id" : "1",
  "incidents" : [
    {"id": 10, "ref": "AAA", "code": "123", date: "2010-11-15T00:01:00Z"},
    {"id": 20, "ref": "BBB", "code": "456", date: "2020-11-15T00:01:00Z"},
  ]
}

尝试了一堆排列,但一无所获!

6ljaweal

6ljaweal1#

可以使用此规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": "id",
      "data": {
        "types": {
          "*": {
            "incident_*": "incidents[&1].&(0,1)"
          }
        }
      }
    }
  }
]

要防止在规范中使用incident文本,可以使用以下规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": "id",
      "data": {
        "types": {
          "*": {
            "*_*": "&(0,1)[&1].&(0,2)"
          }
        }
      }
    }
  }
]
pod7payv

pod7payv2#

您可以通过从标记名称中提取子字符串来使用两级移位转换,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": "id",
      "data": {
        "types": {
          "*": "incidents"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "id": "&",
      "*": {
        "*": {
          "*_*": "&2[&1].&(0,2)"
        }
      }
    }
  }
]

相关问题