Skip to main content
Question

Extracting geometry values from json to create geojson files in FME Form


scarter
Contributor
Forum|alt.badge.img+9

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,

  

21 replies

virtualcitymatt
Celebrity
Forum|alt.badge.img+35

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

 

 


geomancer
Evangelist
Forum|alt.badge.img+47
  • Evangelist
  • April 10, 2025

You can also use the JSONFragmenter:

 

JSON Query: json["property"]["boundary"]["site"]


scarter
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • April 10, 2025

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…

 


virtualcitymatt
Celebrity
Forum|alt.badge.img+35
scarter wrote:

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


scarter
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • April 10, 2025

Thanks ​@virtualcitymatt . Would you mind posting a screenshot of your geometryReplacer transformer settings, please. Thanks 


scarter
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • April 10, 2025

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.

 

 


ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • April 10, 2025

Should be this.

Note, in your original json there are two geometries, one for property and one for proposal. Do you need both?


ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • April 10, 2025
scarter wrote:

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


geomancer
Evangelist
Forum|alt.badge.img+47
  • Evangelist
  • April 10, 2025
scarter wrote:

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...

 


scarter
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • April 10, 2025

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…

 


scarter
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • April 10, 2025

@geomancer I’m getting no geometry in the test output geojson file with this…

 


ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • April 10, 2025

Assuming you are getting a single json file for each record, I’d go with a workflow like this


geomancer
Evangelist
Forum|alt.badge.img+47
  • Evangelist
  • April 10, 2025

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).


scarter
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • April 10, 2025

@geomancer Yes, I did…

 

I’ll try your workspace. Thanks.


geomancer
Evangelist
Forum|alt.badge.img+47
  • Evangelist
  • April 10, 2025

I suppose ["data”] in your JSON Query is superfluous, as you are already inside “data”.

 


virtualcitymatt
Celebrity
Forum|alt.badge.img+35
scarter wrote:

@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. 

 


scarter
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • April 10, 2025

@ebygomm One the test json I’m using the features are going to the Rejected port…

 


virtualcitymatt
Celebrity
Forum|alt.badge.img+35
scarter wrote:

@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. 


scarter
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • April 10, 2025

 

@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.


ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • April 10, 2025

If you only need a handful, you can probably just add them to the json extractor rather than flatten the json at the end


takashi
Influencer
  • April 11, 2025

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.

  1. Read the whole source json text (Text File reader or Data File reader).
  2. JSONExtractor : Extract "data/property/boundary/site" object from the json text.
    • Target Attribute : _feature (for example)
    • JSON Query : json["data"]["property"]["boundary"]["site"]
  3. JSONTemplater : Build a full GeoJSON text with the folowing template expression.
  4. 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.


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