javajsonjoltfixed-length-file

Can I use JOLT 'shift' to achieve this and how? Array to map (list to dict) using JOLT


I am transforming a large JSON into a fixed-length csv file, at this stage I've cut down the json with a JOLT Remove and I have used a JOLT Shift to put the remaining parameters into the input provided.

I have the output from a previous JOLT 'shift' resembling the following: This will form the input for a JOLT "shift"

{
  "TIME": "2023-02-27T16:26:25Z",
  "FORENM": [
    "JILL",
    "LITA"
  ],
  "SURNAME": [
    "JaCK",
    "HANCOCK"
  ],
  "TOWN": [
    "CHRISTCHURCH",
    "GLASGOW",
    "LAZYTOWN",
    null,
    "TELETUBBYHILL",
    "BALAMORY"
  ]
}

I want the output to resemble the following:

{
  "MAIN-TOWN": "CHRISTCHURCH",
  "MAIN_FORENM": "JILL",
  "MAIN_SURNAME": "JaCK",
  "TIME": "2023-02-27T16:26:25Z",
  "ALT-TOWN": "TELETUBBYHILL",
  "ALT_FORENM": "LITA",
  "ALT_SURNAME": "HANCOCK"
}

Is this possible? If so, can I do it with a shift transform? Could someone show me to I would be able to map a certain index from an array in the input. For example, from the town array I want to map "MAIN-TOWN" to index 0 of the "TOWN" array from the input and index 4 to "ALT-TOWN".


Solution

  • You had better using a modify-overwrite transformation and then get rid of the redundant attributes by remove spec such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "MAIN-TOWN": "=firstElement(@(1,TOWN))",
          "MAIN_FORENM": "=firstElement(@(1,FORENM))",
          "MAIN_SURNAME": "=firstElement(@(1,SURNAME))",
          "ALT-TOWN": "=elementAt(4,@(1,TOWN))",
          "ALT_FORENM": "=lastElement(@(1,FORENM))",
          "ALT_SURNAME": "=lastElement(@(1,SURNAME))"
        }
      },
      {
        "operation": "remove",
        "spec": {
          "FORENM|SURNAME|TOWN": ""
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is

    enter image description here