1
votes

I need to perform a Jolt transformation on the below example json:

Input

[
  {
    "DOCUMENT_HEADER_ID": "29120",
    "Descrizione": "GAS",
    "PrezzoTotale": "8.51"
  },
  {
    "DOCUMENT_HEADER_ID": "29120",
    "Descrizione": "IMPOSTE",
    "PrezzoTotale": "7.25"
  },
  ...
]

I need to trasform to

{
  "DOCUMENT_HEADER_ID" : "29120",
  "invoice" : [ 
  {
    "DOCUMENT_HEADER_ID" : "29120",
    "Descrizione" : "GAS",
    "PrezzoTotale" : "8.51"
  }, {
    "DOCUMENT_HEADER_ID" : "29120",
    "Descrizione" : "IMPOSTE",
    "PrezzoTotale" : "7.25"
  },
  ...
  ]
}

Using this spec

[
  {
    "operation": "shift",
    "spec": {
      "@": "invoice"
    }
  }
]

I have this output:

{
  "invoice" : [ {
    "DOCUMENT_HEADER_ID" : "29120",
    "Descrizione" : "GAS",
    "PrezzoTotale" : "8.51"
  }, {
    "DOCUMENT_HEADER_ID" : "29120",
    "Descrizione" : "IMPOSTE",
    "PrezzoTotale" : "7.25"
  },
  ...
  ]
}

but I can not figure out how to extract the value of DOCUMENT_HEADER_ID from the first element of original array

3

3 Answers

1
votes

Can do it in a single shift.

[
  {
    "operation": "shift",
    "spec": {
      "@": "invoice",
      "0": {
        "DOCUMENT_HEADER_ID": "DOCUMENT_HEADER_ID"
      }
    }
  }
]
0
votes

I found the correct spec

[
  {
    "operation": "shift",
    "spec": {
      "@": "invoice"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "invoice": {
        "0": {
          "DOCUMENT_HEADER_ID": "DOCUMENT_HEADER_ID"
        },
        "@": "invoice"
      }
    }
  }
]
0
votes

If having to retrieve single element, data like :

"array_field": ["array_single_element"]

can use:

{
"operation": "modify-overwrite-beta",
"spec": {
  "array_field": "=concat(@(1,array_field))",
  "*": "=concat(@(1,&))"
}
}