Home > Software engineering >  JOLT JSON transpose multiple elements
JOLT JSON transpose multiple elements

Time:11-24

I'm trying to do a (simple?) transpose using JOLT:

Input:

[
  {
    "AAA": "10000000000",
    "Value": 12345,
    "BBB": "BBBValue",
    "CCC": "CCCValue",
    "DDD": "DDDValue"
  }
]

Output:

{
  "DDDValue" : 12345,
  "CCC" : "CCCValue",
  "BBB" : "BBBValue"
}

Spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "AAANewName": "AAA",
        "@Value": "@DDD",
        "CCC": "CCC",
        "BBB": "BBB"
      }
    }
  }
]

Now, when I try and do this for 2 items in my input:

Input:

[
  {
    "AAA": "10000000000",
    "Value": 12345,
    "BBB": "BBBValue",
    "CCC": "CCCValue",
    "DDD": "DDDValue"
  },
  {
    "AAA": "10000000000",
    "Value": 123456,
    "BBB": "BBBValue",
    "CCC": "CCCValue",
    "DDD": "DDDValue"
  }
]

I get the following output:

{
  "DDDValue" : [ 12345, 123456 ],
  "CCC" : [ "CCCValue", "CCCValue" ],
  "BBB" : [ "BBBValue", "BBBValue" ]
}

But I don't want that, I want the items to "stay separate". Do I need another transform? Or can I just change the first one somehow to get the desired output like this:

[
  {
    "DDDValue": 12345,
    "CCC": "CCCValue",
    "BBB": "BBBValue"
  },
  {
    "DDDValue": 123456,
    "CCC": "CCCValue",
    "BBB": "BBBValue"
  }
]

CodePudding user response:

You can use square bracketed notations containing substitution operator(&) for them such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@Value": "[&].@DDD",
        "CCC": "[&1].&",
        "BBB": "[&1].&"
      }
    }
  }
]

enter image description here

  • Related