cancel
Showing results for 
Search instead for 
Did you mean: 

Map data to json structure

matt_bostrom
New Contributor II

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:

ff4d4f7268377b33fcdf39b26a281ff1b604a9ae.png

4 REPLIES 4

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.

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.

ac72a37a4292a9fc28e629db1ea8095372631904.png

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:
c70ea782489a3287a41640efdd1dc284d16c1e34.jpg

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

The Mapper snap should looks like this:
fc64697f5379a0f382a57cf41e421e0566d66f52.png

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!

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 🙂 .