Search code examples
jsontransformationjolt

Jolt trasform from array to object and extract value


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


Solution

  • Can do it in a single shift.

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