bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.55k stars 330 forks source link

concat elements of array #1256

Closed smartrics closed 4 months ago

smartrics commented 4 months ago

Hi - I have this array

{
  "ID": "1",
  "Make": "Toyota",
  "Model": "Camry"
}

and I want to transform it into

{
"properties":
  [
    { "key": "ID", "value": "1" },
    { "key": "Make", "value": "Toyota" },
    { "key": "Model", "value": "Camry" },
    { "key": "label", "value": "Toyota Camry 1" }
  ]
}

I have this spec:

[
  {
    "operation": "shift",
    "spec": {
      "ID": "properties[0].value",
      "Make": "properties[1].value",
      "Model": "properties[2].value"
    }
  },
  {
    "operation": "default",
    "spec": {
      "properties[]": {
        "0": {
          "key": "ID"
        },
        "1": {
          "key": "Make"
        },
        "2": {
          "key": "Model"
        },
        "3": {
          "key": "label"
        }
      }
    }
  }, 
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "properties": {
        "[3]": {
          "value": "=concat(@(1,properties[1].value), ' ', @(1,properties[2].value), ' ', @(1,properties[0].value))"
        }
      }
    }
  }
]

and I can't see what I am doing wrong as the label isn't being populated with what I expect. any hints is much appreciated cheers

barbaros74 commented 4 months ago

Hi smartrics. You can start with using a modify spec, then group by wilcards $ vs. @ within a shift such as

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "label": "=concat(@(1,Make),' ',@(1,Model),' ',@(1,ID))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "properties[#2].key",
        "@": "properties[#2].value"
      }
    }
  }
]
smartrics commented 4 months ago

Awesome! thanks for the tip

barbaros74 commented 4 months ago

You can follow me on SO , have a nice day!