Yawyaw1
Yawyaw1

Reputation: 105

Transform array to sub-values using jolt

I'm struggling to trans my input response to a specific output using jolt, below is my example

The input that describe my message

{
  "firstAttribute": true,
  "secondAttribute": "12",
  "data": {
    "propertyKey1": "1",
    "propertyKey2": [
      "a"
    ],
    "propertyKey3": "2",
    "propertyKey4": "3",
    "propertyKey_test": [
      "option1",
      "option2",
      "option3"
    ],
    "propertyKey5": "4",
    "propertyKey6": "87.0"
  },
  "Keytest1": "value1",
  "KeyTest2": "value2"
}  

The used jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "propertyKey2": {
          "0": "propertyKey2"
        },
        "*": {
          "@": "data.&"
        }
      },
      "*": {
        "@": "&"
      }
    }
  }
]

My actual output after a jolt transfomration


{
  "firstAttribute" : true,
  "secondAttribute" : "12",
  "data" : {
    "propertyKey1" : "1",
    "propertyKey3" : "2",
    "propertyKey4" : "3",
    "propertyKey_test" : [ "option1", "option2", "option3" ],
    "propertyKey5" : "4",
    "propertyKey6" : "87.0"
  },
  "propertyKey2" : "a",
  "Keytest1" : "value1",
  "KeyTest2" : "value2"
}

The desired output is to convert every array fields to separated fields such as propertyKey_test as below


{
  "firstAttribute" : true,
  "secondAttribute" : "12",
  "data" : {
    "propertyKey1" : "1",
    "propertyKey3" : "2",
    "propertyKey4" : "3",
    "propertyKey_test": "option1",
    "propertyKey5" : "4",
    "propertyKey6" : "87.0"
  },
  "propertyKey2" : "a",
  "Keytest1" : "value1",
  "KeyTest2" : "value2"
}

Any help would be appreciated. Thanks in advance

PS: we can receive a dynamic array fields (ex: today we have propertyKey_test:["option1","option2","option3"] tomorrow we could receive another array field for example new_field:["a","b","c"]. My goal is to check everytime if the field is an array and take just the first element as described above

Upvotes: 0

Views: 35

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65228

The current desired output is not a valid JSON value, since there may not exist more than one attribute with identical key within a common object. But, if you need to differentiate the components of the "propertyKey_test" array, then using the indexes of the array as below might be preferred such as

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "propertyKey2": {
          "0": "&1"
        },
        "*": {
          "@": "&2.&"
        },
        "propertyKey_*": {
          "*": {
            "@": "&3.&2&1"
          }
        }
      },
      "*": {
        "@": "&"
      }
    }
  }
]

in order to get

{
  "firstAttribute" : true,
  "secondAttribute" : "12",
  "data" : {
    "propertyKey1" : "1",
    "propertyKey3" : "2",
    "propertyKey4" : "3",
    "propertyKey_test0" : "option1",
    "propertyKey_test1" : "option2",
    "propertyKey_test2" : "option3",
    "propertyKey5" : "4",
    "propertyKey6" : "87.0"
  },
  "propertyKey2" : "a",
  "Keytest1" : "value1",
  "KeyTest2" : "value2"
}

as output.

Upvotes: 1

Related Questions