I'm trying to define some user parameters using scripted parameters. I've written a python script that loads a JSON from a url into a dictionary then extracts the value for defined key. My Private Parameter is called classField.
It works fine when I run it from my Python IDE but not in workbench. What do I need to add/remove to make it work in workbench? I'm using FME 2012 and this is my code:
import simplejson
import urllib2
from pprint import pprint
import sys
import __main__
def URLjson2dictionary(url):
'''Reads json from url and loads it into a python dictionary'''
try:
# open the url (json string) and load into python dictionary
json_dict = simplejson.load(urllib2.urlopen(url))
except Exception as e:
print 'Could not load json from URL. Error: {0}'.format(e)
raise # reraises the exception
return json_dict
def dictValueFetcher(dict, key):
'''Returns the value for a specified key from the python dictionary'''
if key in dict:
return dict.get(key)
else:
print 'Key not in dictionary'
sys.exit()
if __name__ == '__main__':
# define input parameters
url = "http://www.ga.gov.au/gis/rest/services/topography/Australian_Topography/MapServer?f=pjson"
# load the json into a dictionary object
json_data = URLjson2dictionary(url)
# print dictionary derived from json in nice readable format
print 'json_data:'
pprint(json_data)
# Get the parameter value from the dictionary
FME_parameter_value = dictValueFetcher(json_data, "mapName")
print 'KeyFME_parameter_value: {0}'.format(FME_parameter_value)
This is the Log:
Windows command-line to run this workspace: fme.exe CIAP_SBAGDdataValidatorVerson3.fmw --SourceDataset_GEODATABASE_SDE sde --DestDataset_GEODATABASE_FILE C:\\CIAP\\FME_Server\\CIAPStagingDB.gdb --DestDataset_JSON C:\\CIAP\\FME_Server\\DataLoadValidation_working\\InvalidGeomFeatures.json Starting Translation ... INCLUDE -- failed to evaluate Python script `def ParamFunc(): import simplejson import urllib2 from pprint import pprint import sys import __main__ def URLjson2dictionary(url): '''Reads json from url and loads it into a python dictionary''' try: # open the url (json string) and load into python dictionary json_dict = simplejson.load(urllib2.urlopen(url)) except Exception as e: print 'Could not load json from URL. Error: {0}'.format(e) # send error message somewhere else # send_somewhere(traceback.format_exception(*sys.exc_info())) raise # reraises the exception return json_dict def dictValueFetcher(dict, key): '''Returns the value for a specified key from the python dictionary''' if key in dict: return dict.get(key) else: print 'Key not in dictionary' sys.exit() if __name__ == '__main__': # define input parameters url = "http://www.ga.gov.au/gis/rest/services/topography/Australian_Topography/MapServer?f=pjson" # load the json into a dictionary object json_data = URLjson2dictionary(url) # print dictionary derived from json in nice readable format print 'json_data:' pprint(json_data) # Get the parameter value from the dictionary FME_parameter_value = dictValueFetcher(json_data, "mapName") return print 'KeyFME_parameter_value: {0}'.format(FME_parameter_value) value = ParamFunc() macroName = 'classField' if value == None: return { macroName : '' } else: return { macroName : str(value) } ' Program Terminating Translation FAILED.
If the scripting path is not the best method, any suggested workarounds would be greatly appreciated too.
Thanks
Rob