Extracting geometry values from json to create geojson files in FME Form
Hello Forum,
I’ve been provided with a set of json files (note, not geojson, but json). Within each json file I know there are geometry coordinates pairs. See this screenshot of the json in Notepad++…
How can utilise FME to open each json file and pull out the geojson elements to create a geojson showing the polygons?
I’ve attached an example project json file. Note that I’ve changed the extension from .json to .txt to get through the Forum blocker not allowing json files.
Thanks,
Page 1 / 1
It depends a little on what other parts of the file you’d like to use as well, but here’s an example using the JSONExtractor.
Here’m I’m just going down the tree and creading the geometry as an attribute which can be directly fed into the geomtry replacer.
You can also use a JSON reader directly and use the reader parameters to help automatically pull out the geometry
You can also use the JSONFragmenter:
JSON Query: json["property"]["boundary"]["site"]
Thanks for that, @virtualcitymatt . In your first screenshot, is that a Reader? If so, what format did you chose? I see there are around ten readers with .json extension. I have read in my json via a FeatureReder using the topojson format. But when I then read it in it goes into the Initiator port…
Thanks for that, @virtualcitymatt . In your first screenshot, is that a Reader? If so, what format did you chose? I see there are around ten readers with .json extension. I have read in my json via a FeatureReder using the topojson format. But when I then read it in it goes into the Initiator port…
Ah sorry, it was just a text file reader, reading the whole file at once
Thanks @virtualcitymatt . Would you mind posting a screenshot of your geometryReplacer transformer settings, please. Thanks
Thanks @geomancer . In the Fragmenter if I choose Data from the JSON attribute I have four options. In your screenshot you went with just data, but it looks like I have to choose one of the four options.
Should be this.
Note, in your original json there are two geometries, one for property and one for proposal. Do you need both?
Thanks @geomancer . In the Fragmenter if I choose Data from the JSON attribute I have four options. In your screenshot you went with just data, but it looks like I have to choose one of the four options.
It looks like you are trying to use the JSON Fragmenter with the JSON reader. If you using the fragmenter you want to read the json as text
Thanks @geomancer . In the Fragmenter if I choose Data from the JSON attribute I have four options. In your screenshot you went with just data, but it looks like I have to choose one of the four options.
Good point.
I had set Flatten Nested JSON Values into Attributes to 'No’. Forgot to menion that...
Thanks @ebygomm. Will I need both geometries? I’m not sure at the moment. I’d need to see examples of them first to assess. But yes, maybe.
In my geometryReplacer it’s not accepting @Value(JSON). Unsurprising as there isn't an attribute called JSON…
@geomancer I’m getting no geometry in the test output geojson file with this…
Assuming you are getting a single json file for each record, I’d go with a workflow like this
Did you set Fragment as Format to GEOJSON?
Did you add json in front of the JSON Query?
See the attached workspace (FME 2024.1, I don't have access to version 2023 at the moment).
@geomancer Yes, I did…
I’ll try your workspace. Thanks.
I suppose ["data”] in your JSON Query is superfluous, as you are already inside “data”.
@geomancer Yes, I did…
I’ll try your workspace. Thanks.
Yeah you should turn off the Flatten Query Results into Attributes - this should help. If you flatten it you end up with all these ugly list attributes.
What you want to end up with is just an attribute containing the just GeoJSON encoded geometry. In this case its the “geometry” object(s) in your json file. You just want to have this sitting as an attribtue.
in the geometry replacer you need to set the geometryencoding to GeoJSON.
@ebygomm One the test json I’m using the features are going to the Rejected port…
@ebygomm One the test json I’m using the features are going to the Rejected port…
You need to make sure to read the Whole file at once in the text reader. You should be getting just a single feature per input file.
@ebygomm Thanks. That’s working for me now…
I can now use an AttributeExposer to pull out the attributes I need. Thankfully, out of the 163 attributes in the data I’ll only need a handful of them.
If you only need a handful, you can probably just add them to the json extractor rather than flatten the json at the end
Hi @scarter ,
The syntax of "data/property/boundary/site" object in your example json text completely matches the syntax of "feature" object (geometry and properties) in GeoJSON format specification. I therefore think the following approach is also possible, if your requirement is to retrieve the feature object and write it into a GeoJSON file.
Read the whole source json text (Text File reader or Data File reader).
JSONExtractor : Extract "data/property/boundary/site" object from the json text.
JSONTemplater : Build a full GeoJSON text with the folowing template expression.
Write the resulting GeoJSON text out with Text File writer or Data File writer.
JSONTemplater : Root Template expression example { "type" : "FeatureCollection", "name" : "A feature type name as your preference", "features" : [ fme:get-json-attribute("_feature") ] }
Applying the method, you can also add "data/proposal/boundry/site" object to the GeoJSON text as the second feature if necessary. If you would like to add/remove a property to/from the "properties" in the feature, JSONUpdater would help you.