Question

Add ID column to GeoJSON file


Hi!

 

I´m trying to add an ID column to a GeoJSON file. I use a reader to read data from an Oracle database and then write it down using to GeoJSON writer.

 

I need the ID not to be in the properties-section but as in the example below:

https://community.safe.com/s/article/tutorial-getting-started-with-json

 

Thankful for any suggestions on how to do this 😊


9 replies

Userlevel 5
Badge +25

What you can do is insert a Counter transformer before the writer, it will simply number each feature coming through with an incremental number and you can write that to an attribute. Don't forget to add the attribute to your GeoJSON writer though! (in the User Attributes tab of the writer feature type properties).

Thank you for the answer. It seems though that the attribute is added to the properties of the feature and not to the feature itself as in the example.

Userlevel 5
Badge +25

Would it be possible to share a small example of what you currently have and what you want to have?

Would it be possible to share a small example of what you currently have and what you want to have?

I have uploaded a sample GeoJSON😊. I would like the ID-attribute to be at the features-level.

Userlevel 5
Badge +25

I have uploaded a sample GeoJSON😊. I would like the ID-attribute to be at the features-level.

Ah, I see. I think you can't do that under the GeoJSON specs, from what I've been able to find out you can only have type, geometry and properties at the features level. The FME writer will add all attributes at the properties level.

Badge

Hi, I am new to this topic and have exactly the same problem. I would be interested to know if you have found a solution in the meantime? 

Hi, I am new to this topic and have exactly the same problem. I would be interested to know if you have found a solution in the meantime? 

Hi, I didnt manage to add the id where I wanted it. I ended up opening the geojson file in QGIS and exported it with an id that way, although this way it is difficult to automate.

Badge

Thanks, this is the way, that i will take too.

Hi,

I had the same problem and this is how I solved it with python:

Create the geojson file with a FeatureWriter. In the summary output port you get the output path as "_dataset" attribute. Connect a PythonCaller to the summary output port and add the following code in the input function:

import json
import os
[...]
def input(self, feature):
    filename = feature.getAttribute('_dataset')
    with open(filename, 'r', encoding='utf8') as file:
        data = json.load(file)
        for f in data["features"]:
            f["id"] = f["properties"]["id"]
 
    os.remove(filename)
    with open(filename, 'w', encoding='utf8') as f:
        json.dump(data, f, ensure_ascii=False)

 

 

Reply