In addition to the defaults “name” and “type”, I would like to add a third, custom member named “data_owner” at the start of a GeoJSON file I am creating using the GeoJSON Writer (see example below). Is there an easy way to do this - I can’t find anything in the writer parameters - or do I need to use the JSONTemplater or another transformer? (FME 2023.2.0.0)
I don't think GeoJSON writer supports to write "foreign members" into the destination GeoJSON document.
A possible way I can think of is, after writing a starndard GeoJSON document with FeatureWriter, update the document with JSONUpdater transformer and then overwite the updated document to the same file with AttributeFileWriter or Text File writer, etc.
This screenshot illustrates a workflow example.
Just be aware that you cannot control the order of objects with JSONUpdater. If you want to insert “data_owner” object into a specific position in the document - e.g. before “features” object, you will have to consider another solution.
Hi @takashi, the above works perfectly except that, and as you alluded, it inserts the “data_owner” name-value pair at the bottom of the document rather than with the “name” and “type” members at the top. So syntactically correct since “data_owner” is still within the root brackets but not ideal since all the members are not together at the top.
Extract and delete "features" object, insert "data_owner" object, and insert again the extracted "features" object.
It's a kind of hack then... Usually the order of objects in a JSON document is not important, so I don't recommend you to adopt the hack if there is no specific reason for arranging the order.
I want to maintain the structure of the GeoJSON (provided by a contractor) so I created my own hack. It’s a kludge but it works. See screenshot.
Since JSON document is compatible with Python dictionary and dictionary in Python 3.7+ keeps the order of its keys, a short Python script could also be a solution. An example shown below.
import fme import fmeobjects, json
class FeatureProcessor(object): def __init__(self): pass
def input(self, feature: fmeobjects.FMEFeature): path = feature.getAttribute('_dataset') with open(path, 'r', encoding='utf-8') as f: # Read GeoJSON document as a Python dictionary. d = json.load(f)
with open(path, 'w', encoding='utf-8') as f: # Insert 'data_owner' into the dictionary. d['data_owner'] = feature.getAttribute('_data_owner')
# Renew the dictionary with the required key order. keys = [ 'name', 'type', 'data_owner', 'features' ] d = {k : d.get(k, '') for k in keys}
# Write the dictionary as a JSON document. json.dump(d, f, indent='\t')