skip to Main Content

I am trying to write a jolt specification to transform an input JSON. I have the following input:

[{
    "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"
    }]
}]

The expected output after the jolt transform is:

[{
    "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"
}]

Here is my current 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": [""]
    }
  }
]

I’m close, but I’m losing the last status change because it’s embedded in an array and my spec is only working when there is one item in the array.

Any suggestions on how to fix it?

2

Answers


  1. You should just change your spec a little bit. Like the following JOLT spec:

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

    [
      {
        "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])"
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search