Chris X
Chris X

Reputation: 67

Jolt transform Json with list within list

I have a output that I want to jolt transform but I'm having a difficult time doing so

Json Example:

{
  "message": [
    [
      "2019",
      "DATE"
    ],
    [
      "second",
      "ORDINAL"
    ],
    [
      "Local",
      "PERSON"
    ],
    [
      "2019",
      "DATE"
    ],
    [
      "ISO",
      "ORG"
    ],
    [
      "Ubuntu",
      "PERSON"
    ]
  ]
}

I want to make the output look like this

{
  "DATE": "2019",
  "ORDINAL": "second",
  "PERSON": "Local",
  "DATE": "2019",
  "ORG":"ISO",
  "PERSON": "ubuntu"
}

The transformation should start with what displayed below but I get confused because of the list and the changing key and values. The original shows the keys as the capitalized values while the values are the lower cased values, they are backwards in the original.

{
    "operation": "shift",
    "spec": {
     ....

Upvotes: 1

Views: 236

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65408

The current desired output is not a valid JSON as having duplicate DATE and PERSON keys. What you want might be

{
  "DATE" : [ "2019", "2019" ],
  "ORDINAL" : "second",
  "PERSON" : [ "Local", "Ubuntu" ],
  "ORG" : "ISO"
}

Then, you can use the following spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "0": "@(2,[&1][1])"
        }
      }
    }
  }
]

where we walk the indexes of the messages array through use of consecutive "*" wildcards, and that forms such a JSON

{
  "0" : [ "2019", "DATE" ],
  "1" : [ "second", "ORDINAL" ],
  "2" : [ "Local", "PERSON" ],
  "3" : [ "2019", "DATE" ],
  "4" : [ "ISO", "ORG" ],
  "5" : [ "Ubuntu", "PERSON" ]
}

and then "0" is used to get the second components, and "@(2,[&1][1])" is used to get the first components of each arrays from that JSON value.

enter image description here

Upvotes: 1

Related Questions