json Jolt变换对阵列数据的滤波和展平

nwlls2ji  于 2023-05-30  发布在  其他
关注(0)|答案(3)|浏览(153)

我正在尝试编写一个jolt规范来转换输入JSON。我有以下输入:

[{
    "id": "11500887",
    "created": "2023-03-16T18:34:24.485+0200",
    "items": [{
        "field": "status",
        "fieldtype": "jira",
        "fieldId": "status",
        "from": "10000",
        "fromString": "In Definition",
        "to": "1",
        "toString": "Open"
    }]
}, {
    "id": "11500905",
    "created": "2023-03-16T18:35:05.552+0200",
    "items": [{
        "field": "status",
        "fieldtype": "jira",
        "fieldId": "status",
        "from": "1",
        "fromString": "Open",
        "to": "10001",
        "toString": "Committed"
    }]
}, {
    "id": "11500907",
    "created": "2023-03-16T18:35:11.634+0200",
    "items": [{
        "field": "status",
        "fieldtype": "jira",
        "fieldId": "status",
        "from": "10001",
        "fromString": "Committed",
        "to": "3",
        "toString": "In Progress"
    }]
}, {
    "id": "12223186",
    "created": "2023-05-03T00:55:00.323+0300",
    "items": [{
        "field": "resolution",
        "fieldtype": "jira",
        "fieldId": "resolution",
        "from": null,
        "fromString": null,
        "to": "10008",
        "toString": "Done"
    }, {
        "field": "status",
        "fieldtype": "jira",
        "fieldId": "status",
        "from": "3",
        "fromString": "In Progress",
        "to": "10018",
        "toString": "In Testing"
    }]
}]

jolt变换后的预期输出为:

[{
    "date": "2023-03-16T18:34:24.485+0200",
    "field": "status",
    "from": "In Definition",
    "to": "Open"
}, {
    "date": "2023-03-16T18:35:05.552+0200",
    "field": "status",
    "from": "Open",
    "to": "Committed"
}, {
    "date": "2023-03-16T18:35:11.634+0200",
    "field": "status",
    "from": "Committed",
    "to": "In Progress"
}, {
    "date": "2023-05-03T00:55:00.323+0300",
    "field": "status",
    "from": "In Progress",
    "to": "Testing"
}]

以下是我目前的spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "items": {
          "*": {
            "@2,created": "[&3].date",
            "field": "[&3].field",
            "fromString": "[&3].from",
            "toString": "[&3].to"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "field": {
          "status": {
            "@2": "tmp.[]"
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "tmp": []
    }
  },
  {
    "operation": "shift",
    "spec": {
      "tmp": [""]
    }
  }
]

我很接近了,但是我丢失了最后一个状态更改,因为它被嵌入到一个数组中,并且我的spec只在数组中有一个项目时才起作用。
有什么建议吗?

1qczuiv0

1qczuiv01#

你应该稍微改变一下你的规格。如以下JOLT规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "items": {
          "*": {
            "@2,created": "[&3][&1].date",
            "field": "[&3][&1].field",
            "fromString": "[&3][&1].from",
            "toString": "[&3][&1].to"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]
dddzy1tm

dddzy1tm2#

技术参数

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "created": "[&1].date",
        "items": {
          "*": {
            "field": "[&3].field",
            "fromString": "[&3].from",
            "toString": "[&3].to"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "field": "@(1,field[1])",
        "to": "@(1,to[1])"
      }
    }
  }
]
anauzrmj

anauzrmj3#

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "created": "[#2].date",
        "items": {
          "*": {
            "field": {
              "status": {
                "@2,field": "[&5].field",
                "@2,fromString": "[&5].from",
                "@2,toString": "[&5].to"
              }
            }
          }
        }
      }
    }
  }
]

相关问题