Forum Discussion

matt_bostrom's avatar
matt_bostrom
New Contributor II
8 years ago

Map data to json structure

hello. i am developing a Zendesk integration to auto create tickets from our source platforms. for a PoC I am able to achieve this by using the JSON generator as such

[
{
“tickets”: [
{
“subject”: “Integration Test 1”,
“comment”: { “body”: “Testing 1.” },
“priority”: “urgent”
},
{
“subject”: “Integration Test 2”,
“comment”: { “body”: “Testing 2” },
“priority”: “normal”
}
]
}
]

but when I am getting data/documents how do I convert that to the JSON structure wrapped in the outer tickets node?

Here is the mapper snap that will contain many records that need to eventually become tickets:

4 Replies

  • eguo's avatar
    eguo
    Former Employee

    Looks like you are facing a transformation issue. Normal snaps can do the job.

    Assume your input data is flat like what’s in following picture.

    They would looks like this in a snap preview:

    [
      {
        "S": "1",
        "STATUSDESCRIPTION": "Testing 1.",
        "P": "urgent"
      },
      {
        "S": "2",
        "STATUSDESCRIPTION": "Testing 2.",
        "P": "normal"
      }
    ]
    

    A pipeline like this will produce the expected output:

    The Group By N snap can group individual input records into an array.

    The Mapper snap should looks like this:

    • matt_bostrom's avatar
      matt_bostrom
      New Contributor II

      this is perfect! on my health check call it was the same solution you provided. hopefully this helps others. @del I’m sure your solution works but this other one requires no code so good to know going forward!

      • del's avatar
        del
        Contributor III

        Yep, that’s good to know. I guess I missed the release of the Group By N snap. I wouldn’t have had to develop my own 🙂 .

  • del's avatar
    del
    Contributor III

    @matt.bostrom, The simplest way that I know of is to use a Script snap. I’m not a Python expert (so someone can improve on this), but code in the execute method would look something like this:

    def execute(self):
        tickets = []
        out = java.util.HashMap()
        while self.input.hasNext():
            try:
                in_doc = self.input.next()
                tickets.append(in_doc)
            except Exception as e:
                errWrapper = {
                    'errMsg' : str(e.args)
                }
                self.log.error("Error in python script")
                self.error.write(errWrapper)
        out["tickets"] = tickets
        self.output.write(out)
    

    I’ve run into similar needs before on a couple of occasions, so I have written a custom snap to achieve this same result. I can share the java code with you as well if you want to develop your own snap.