Skip to main content
Solved

How to setAttribute lists in PythonCaller

  • August 30, 2017
  • 3 replies
  • 176 views

Forum|alt.badge.img+1

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

 

Best answer by geosander

Hi @aquamarine,

If you want to do this in Python (presuming that "info" is a list of 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 the JSONFlattener (which is probably the one you need!), JSONExtractor and JSONFragmenter. Much cleaner.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

3 replies

danilo_fme
Celebrity
Forum|alt.badge.img+51
  • Celebrity
  • 2077 replies
  • August 30, 2017

Hi @aquamarine,

I posted a question here about JSON in Python:

JSON with Python

Thanks,

Danilo


geosander
Forum|alt.badge.img+7
  • 327 replies
  • Best Answer
  • August 31, 2017

Hi @aquamarine,

If you want to do this in Python (presuming that "info" is a list of 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 the JSONFlattener (which is probably the one you need!), JSONExtractor and JSONFragmenter. Much cleaner.


Forum|alt.badge.img+1
  • Author
  • 271 replies
  • August 31, 2017

Hi @aquamarine,

If you want to do this in Python (presuming that "info" is a list of 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 the JSONFlattener (which is probably the one you need!), JSONExtractor and JSONFragmenter. Much cleaner.

Thanks @sander_s

 

I tried your code and that worked perfectly.

 

 

Thanks also @takashi for your advice.  I will try those JSON transformers too as an alternative method.