Skip to main content
Solved

Set attribute JSON with Python

  • December 17, 2015
  • 5 replies
  • 132 views

danilo_fme
Evangelist
Forum|alt.badge.img+41

Hello all,

I have a Workspace that makes the reverse geocoding using PythonCaller.

import fme

import fmeobjects

import json ,urllib2 #

def processFeature(feature):

att = urllib2.urlopen('http://maps.googleapis.com/maps/api/geocode/json?latlng=-23.2186725,-45.9056706')

data = json.load(att)

print "o json é ", data

When i run my workspace i see the results on the log file because i put the Print:

How can i get this result with a new attribute?

Thanks in Advance,

Danilo de Lima

Best answer by david_r

Try something like

feature.setAttribute('myJSON', json.dumps(data))

You can then use the regular FME JSON transformers to work on the new attribute "myJSON".

David

View original
Did this help you find an answer to your question?

5 replies

david_r
Evangelist
  • Best Answer
  • December 17, 2015

Try something like

feature.setAttribute('myJSON', json.dumps(data))

You can then use the regular FME JSON transformers to work on the new attribute "myJSON".

David


danilo_fme
Evangelist
Forum|alt.badge.img+41
  • Author
  • Evangelist
  • December 17, 2015

hello @david_r thanks your help. Its works!!!

Amazing. :) The secrets is the json.dumps, other?

Thanks in Advance,

Danilo de Lima


danilo_fme
Evangelist
Forum|alt.badge.img+41
  • Author
  • Evangelist
  • December 17, 2015
danilo_fme wrote:

hello @david_r thanks your help. Its works!!!

Amazing. :) The secrets is the json.dumps, other?

Thanks in Advance,

Danilo de Lima

@david_r Thanks the explanation and the link.


david_r
Evangelist
  • June 6, 2018
danilo_fme wrote:

hello @david_r thanks your help. Its works!!!

Amazing. :) The secrets is the json.dumps, other?

Thanks in Advance,

Danilo de Lima

Look in the Python json module documentation. Basically:

 

.dumps() transforms a string into a Python dictionary

 

.loads() transforms a Python dictionary into a string

 

 


gedgg
Contributor
Forum|alt.badge.img+1
  • Contributor
  • March 11, 2025
david_r wrote:

Try something like

feature.setAttribute('myJSON', json.dumps(data))

You can then use the regular FME JSON transformers to work on the new attribute "myJSON".

David

Hi David, 

in 2024.2 I have json that I dump into text as you suggested.

feature.setAttribute("output_result", json.dumps(results))

Then I have attribute exposed, but not sure why it keeps coming out as <missing> . Also not sure what data type to use since simple text option does not exist.

 


I am successfully printing data, just no luck outputting into attribute:

 



EDIT: my error was self.pyoutput(feature, output_tag="PYOUTPUT") placement. Moving it to the bottom of input function solved all the problems…. 🤓


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings