0

For a specific function that I am building, I need to parse my JSON and have in some cases the attribute, instead of the value itself, be used as the value for the attribute. But how do I manage that with JOLT?

Let's say this is my input:

{
  "Results": [
    {
      "FirstName": "John",
      "LastName": "Doe"
    },
    {
      "FirstName": "Mary",
      "LastName": "Joe"
    },
    {
      "FirstName": "Thomas",
      "LastName": "Edison"
    }
  ]
}

And this should be the outcome:

{
  "Results": [
    {
      "Name": "FirstName",
      "Value": "John"
    },
    {
      "Name": "FirstName",
      "Value": "Mary"
    },
    {
      "Name": "FirstName",
      "Value": "Thomas"
    },
    {
      "Name": "LastName",
      "Value": "Doe"
    },
    {
      "Name": "LastName",
      "Value": "Doe"
    },
    {
      "Name": "LastName",
      "Value": "Edison"
    },
  ]
}

For those interested.. I'm building a JSON to Excel export functionality in Mendix and it has to be completely dynamic, regardless of the input. To accomplish this, I need an array where each attribute (equal to a column in Excel) has to be it's own object with a column name and a value. If each column data is it's own object, I can simply say "create column for each object with the same "Name". Little bit difficult to explain, but it 'should' work.

Sander Schaeffer
  • 2,757
  • 7
  • 28
  • 58

1 Answers1

2

Arrays and Jolt, are not the best. Basically there are 3 ways to deal with arrays in Shift.

  1. you explicitly assign data to an array position. Aka foo[0] and foo[1]
  2. you reference a "number" that exists in the input data. Aka foo[&2] and foo[&3]
  3. you "accumulate" data into a list. Aka foo[].

Your input data is array of size 3. Your desired output is an array of size 6. You want this to be flexible and be able to handle variable inputs.

This means option 3. So you have to "fix" / process your data into it "final form", while maintaining the original input Json structure (of a list with 3 items), and then accumulate all the "built" items into a list.

This means that you are buildling a list of lists, and then finally "squashing" it down to a single list.

Spec

[
  {
    // Step 1 : Pivot the data into parallel lists of keys and values
    //  maintaining the original outer input list structure.
    "operation": "shift",
    "spec": {
      "Results": {
        "*": { // results index
          "*": { // FirstName / Lastname
            "$": "temp[&2].keys[]",
            "@": "temp[&2].values[]"
          }
        }
      }
    }
  },
  {
    // Step 2 : Un-pivot the data into the desired
    //  Name/Value pairs, using the inner array index to 
    //  keep things organized/separated.
    "operation": "shift",
    "spec": {
      "temp": {
        "*": { // temp index
          "keys": {
            "*": "temp[&2].[&].Name"
          },
          "values": {
            "*": "temp[&2].[&].Value"
          }
        }
      }
    }
  },
  {
    // Step 3 : Accumulate the "finished" Name/Value pairs
    //  into the final "one big list" output.
    "operation": "shift",
    "spec": {
      "temp": {
        "*": { // outer array
          "*": "Results[]"
        }
      }
    }
  }
]
Milo S
  • 4,466
  • 1
  • 19
  • 22