0
votes

Trying to make a Jolt script that will put in a single number line, then a array of strings in one single array and a tag on the end of that array. These are the values that I have been working with.

JSON INPUT

[
{
"foo": "111",
"bar": "222",
"sun": "333",
"ListofStrings": [
  "Dog",
  "Train"
],
"ID": "BLAH"
},
{
"foo": "999",
"bar": "222",
"sun": "777",
"ListofStrings": [
  "CAT",
  "PLANE"
 ],
"ID": "HAHA"
}
]

JOLT SPEC This is what I have been working with that prints out the ListofStrings but this is the one that works stably.

[
 {
"operation": "shift",
"spec": {
  "*": {
    "foo": "input[].number",
    "bar": "input[].number",
    "sun": "input[].number",
    "ListofStrings": "input[].List"
  }
}
},
{
"operation": "default",
"spec": {
  "app_id": "test",
  "input": {
    "*": {
      "app_id": "test"
    }
  }
  }
 }
]

CURRENT OUTPUT

{
"input" : [ {
"number" : "111"
 }, {
"number" : "222"
}, {
"number" : "333"
}, {
"List" : [ "Dog", "Train" ]
}, {
"number" : "999"
}, {
"number" : "222"
}, {
"number" : "777"
}, {
"List" : [ "Cat", "Car" ]
} ],
"app_id" : "test"
}

DESIRED OUTPUT

{
"input" : [ {
"number" : "111"
"List" : [ "Dog", "Train" ]
"ID": "BLAH_foo"
}, {
"number" : "222"
"List" : [ "Dog", "Train" ]
"ID": "BLAH_bar"
}, {
"number" : "333"
"List" : [ "Dog", "Train" ]
"ID": "BLAH_sun"
},  {
"number" : "999"
"List" : [ "Cat", "Car" ]
"ID": "HAHA_foo"
}, {
"number" : "222"
"List" : [ "Cat", "Car" ]
"ID": "HAHA_bar"
}, {
"number" : "777"
"List" : [ "Cat", "Car" ]
"ID": "HAHA_sun"
} ],
"app_id" : "test"
}
1
jolt is mandatory? what about script? - daggett
unless there is a way to do this in a nifi processor - Sleepyfalcon

1 Answers

1
votes

Check this spec

[
  //Converting list to Map
  {
    "operation": "shift",
    "spec": {
      "*": {
        "ListofStrings": null,
        "*": {
          "@": "@1.number",
          "@(1,ListofStrings)": "@1.list"
        }
      }
    }
  },
  //Shift the number and list to the input array
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "input[#2].number",
        "@(0,list)": "input[#2].List"
      }
    }
  }, {
    "operation": "default",
    "spec": {
      "app_id": "test",
      "input": {
        "*": {
          "app_id": "test"
        }
      }
    }
 }
]

Edit 1

Add the ID node to the map using first shift operation "ID": null, and "@(1,ID)": "@1.ID". Then shift the ID node to the input array in the second shift operation "@(0,ID)": "input[#2].ID".

[
  //Converting list to Map
  {
    "operation": "shift",
    "spec": {
      "*": {
        "ListofStrings": null,
        "ID": null,
        "*": {
          "@": "@1.number",
          "@(1,ListofStrings)": "@1.list",
          "@(1,ID)": "@1.ID"
        }
      }
    }
  },
  //Shift the number and list to the input array
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "input[#2].number",
        "@(0,list)": "input[#2].List",
        "@(0,ID)": "input[#2].ID"
      }
    }
  }, {
    "operation": "default",
    "spec": {
      "app_id": "test",
      "input": {
        "*": {
          "app_id": "test"
        }
      }
    }
 }
]