Hi, I'm using Python to access the API for Anaplan - this provides a csv output. I've seen some other posts about how to add the results to a grid query but I am getting an error I'm unable to progress

java.lang.ClassCastException: java.lang.ClassCastException: java.lang.String cannot be cast to org.python.core.PyList in <script> at line number 33

 

Hi @joshua.stockwell

I think you need to wrap the viewResponse in square brackets and then append, similar to something below, as the error is complaining about it being a string not a list. The below is an example where I am storing three values in a row and then appending to a grid variable:

_date_list = []

_date_list.append([

_batch_start_date.strftime('%Y-%m-%d'),

_batch_end_date.strftime('%Y-%m-%d'),

_batch_load_strategy

])

context.updateGridVariable('date_list', _date_list)

Hope that helps!

Hi @joshua.stockwell​ ,

As @ELTuser77​ mentioned you need to wrap the list results with square brackets. Effectively, wrapping the square brackets around the results is creating an array of rows of values that match up to the columns in your grid variable. The important thing to note here is that order matters. Meaning if the order of your column values need be in the same order that exists in your grid variable columns. @MichaelBoucht​ posted a very simplified answer to how this is done a while back which I thought was very simple and easy to understand example of how to do this. You can check that out here: https://matillioncommunity.discourse.group/t/grid-variable-updating-in-python-script/1200

I hope this helps.

Thanks both for the response. In the end I used the viewResponse.content and parsed the results as a csv. Interestingly, switching from the Jython compiler to Python 2 also prevented the initial error.

 

# get contents of view

viewResponse = requests.request("GET", viewUrl, headers=auth_header, data=payload)

print(viewResponse.content)

# convert to csv

responseCSV = viewResponse.content.decode('utf-8')

print(responseCSV)

readcs = csv.reader(responseCSV.splitlines(), delimiter=",")

 

# put results in python list

gridVarValues = list(readcs)

 

# Update grid variable 

context.updateGridVariable('Adjustments',gridVarValues)

 

Hey @joshua.stockwell​,

I didn't think to offer the tidbit about changing the interpreter. The rule of thumb I follow and I think it's generally known but not well documented is that you should always use Python 2 or 3 instead of Jython. I have heard the same from Matillion people as well. In older versions of Matillion the default interpreter was Jython. They have recently change the default to Python 2 or 3.

Here is the very simple rule that I follow when determining if I need to use Jython... If you have to query Snowflake directly from within a script, then you would use Jython. For everything else I use Python. To be honest I have only had to truly use Jython once. There are many nuances and little differences between Jython and Python. Take the "print" function for example. In Jython you can use it like this: print "My print statement". In Python, that fails and requires you to format the function like this: print("My print statement")

Sorry for missing that piece of information. I honestly forget Jython is still there as an interpreter because I almost never use it.

I'm glad you got it figured out. Have a great weekend!