Solved

Set attribute JSON with Python

  • 17 December 2015
  • 4 replies
  • 37 views

Userlevel 5
Badge +30

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

icon

Best answer by david_r 17 December 2015, 14:37

View original

4 replies

Userlevel 5

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

Userlevel 5
Badge +30

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

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

Thanks in Advance,

Danilo de Lima

Userlevel 5
Badge +30

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.

Userlevel 5

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

 

 

Reply