0

Could you please help me writing JOLT spec for the following?

Input:

{
    "title": [
        "vsnu",
        "anothervsnu"
    ],
    "anothertitle": [
        "vsnu",
        "anothervsnu"
    ]
}

Expected output:

{
Response : [
{
"head" : "title",
"name" : "vsnu"
},
{
"head" : "title",
"name" : "anothervsnu"
},
{
"head" : "anothertitle",
"name" : "vsnu"
},
{
"head" : "anothertitle",
"name" : "anothervsnu"
}
]
}

I am stuck in this for the last 3 days. Please help me on this. And I hope the above question explains the expectations, I am writing this only because StackOverflow shows the validation error message.

Thanks in advance.

vsnu
  • 27
  • 6

2 Answers2

0

You need only two iterators, one over the keys of the input and one over the array from the properties.

function buildObject(o) {
    var result = [];

    Object.keys(o).forEach(function (k) {
        o[k].forEach(function (a) {
            result.push({ head: k, name: a });
        });
    });
    return { Response: result };
}

var input = { "title": ["vsnu", "anothervsnu"], "anothertitle": ["vsnu", "anothervsnu"] },
    output = buildObject(input);

document.write('<pre>' + JSON.stringify(output, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Because Jolt "shift" processes one item at a time, it has trouble writing to output that is an Array of Maps.

It can be done, but it requires two shifts. The first one builds up parallel arrays of "head" and "name", and the second one pivots them into the Response array, using the index numbers from parallel arrays.

Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": { // title or anothertitle
        "*": { // array index
          "*": { // actual array value "vsnu"
            "$2": "head[]", // for each array value grab a copy of the "title"
            "$": "name[]"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "head": {
        "*": "Response[&].head"
      },
      "name": {
        "*": "Response[&].name"
      }
    }
  }
]
Milo S
  • 4,466
  • 1
  • 19
  • 22