span8
span4
span8
span4
Hi,
I want to output a list from JSON using the PythonCaller.
I have tried the following syntax but it doesn't work.
info = json.loads(data)
for item in info:
feature.setAttribute("_list{}.value",item["time"])
Does anyone have any suggestions?
I am using FME Desktop 2017.0
Thank you
Hi@aquamarine,
If you want to do this in Python (presuming that "info" is alistof dicts,but not a dict itself!),you have to set each list attribute individually,like so:
info = json.loads(data)num_items = len(info)for i in range(num_items):time = info[i]["time"]feature.setAttribute("_list{{{}}}.value".format(i),item["time"])
Because Python's format() function replaces curly braces with function parameters,but FME needs these curly braces so it knows it's a list,you'll have to do e.g."_list{{{}}}".format(3)which will return_list{3}.
But if your JSON input string comes from an FME attribute and you only need to explode it into a list,I'd recommend@takashi's approach and not use any Python at all,but to resort to transformers like theJSONFlattener(which is probably the one you need!),JSONExtractorandJSONFragmenter.Much cleaner.
PythonCaller input loop2 Answers
Transfer python dictionnary between 2 Transformers3 Answers
Failed to create unique list Python2 Answers
Python setAttribute list of dict attributes3 Answers
Running Python in FME6 Answers
© 2019 亚搏在线Safe Software Inc |Legal