0

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".

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Aziz R.
  • 17
  • 4
  • 1
    oops that was an accident, but later on I would like to have a field that takes the initial, rather than the name.. not right now though. it should be LITA as in the input. – Aziz R. Mar 21 '23 at 16:56

1 Answers1

0

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

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
  • Would it be possible to map the first initial of the name instead of the entire name as a separate map. E.g. a field MAIN-FORENM: JILL, and a new map MAIN-INITIAL: J – Aziz R. Mar 21 '23 at 17:33
  • You can add `"MAIN-INITIAL": "=substring(@(1,MAIN_FORENM),0,1)"` @AzizR. – Barbaros Özhan Mar 21 '23 at 17:47
  • If I wanted this to maintain the order of items, how can I retain 'TIME' within this. I.e. can I put in a parameter that will map TIME it's original value and keep it in it's original place without having to use another operation? – Aziz R. Mar 23 '23 at 15:50
  • In that case you would need one more spec to add : `{ "operation": "shift", "spec": { "MAIN-TOWN": "&", "MAIN_FORENM": "&", "MAIN_SURNAME": "&", "TIME": "&", "ALT-TOWN": "&", "ALT_FORENM": "&", "ALT_SURNAME": "&" } }` @AzizR. – Barbaros Özhan Mar 23 '23 at 18:20