Skip to main content
Question

Data to JSON with schema


mathiku
Supporter
Forum|alt.badge.img+7

I have a JSON-schema. A part of it looks like this: 

    "dct_license_sm": {
      "type""array",
      "items": {
        "type""string"
      }
    }

 I feed data into a featurewriter pointed at the schema-file. The corresponding data from the input looks like this:

dct_license_sm (string: UTF-8): https://creativecommons.org/licenses/by/4.0/

So - a string, but in the schema it is declared that this is to be an array with string-items. Then I would expect the output to be like:

  "dct_license_sm": [
    "https://creativecommons.org/licenses/by/4.0/"
  ],

Obviously declared as an array containing a single string. But my results in FME via the FeatureWriter comes out like this:

"dct_license_sm" : "https://creativecommons.org/licenses/by/4.0/",

In the FeatureWriter I can't change the datatype.

What would be a good way around this?

 

 

 

9 replies

david_r
Celebrity
  • May 17, 2023

You could generate the list of strings in your workspace and then define the output attribute as a pre-defined json block:

imageIn you case, dct_license_sm could contain e.g. the following valid JSON list:

["https://creativecommons.org/licenses/by/4.0/"]

 


mathiku
Supporter
Forum|alt.badge.img+7
  • Author
  • Supporter
  • May 17, 2023
david_r wrote:

You could generate the list of strings in your workspace and then define the output attribute as a pre-defined json block:

imageIn you case, dct_license_sm could contain e.g. the following valid JSON list:

["https://creativecommons.org/licenses/by/4.0/"]

 

Isn't that a bit complicated?  Do you know why FME doesn't respect the datatype? 


david_r
Celebrity
  • May 17, 2023
mathiku wrote:

Isn't that a bit complicated?  Do you know why FME doesn't respect the datatype? 

Strictly speaking, an array of strings is comprised of two data types ;-)

But you can also do something like this:

imageIn my case {0} = 'abc' and {1} = 'def'.

Result:

[
	{
		"json_featuretype" : "NewFeatureType",
		"dct_license_sm" : "abc""def" ]
	}
]

 


sar_copp
Contributor
Forum|alt.badge.img+4
  • Contributor
  • February 14, 2025

Hello, would anyone have a sample workbench that they could upload to illustrate this please? Im having terrible trouble getting a postgis table to export into a pre-defined json schema. I dont understand it at all - especially the part about putting some data into a list.
Worked example would be fab if poss?  xx


crystalatsafe
Safer
Forum|alt.badge.img+18

Hi ​@sar_copp 

We have a JSON tutorial series that may help: Tutorial: Getting Started with JSON the tutorial series includes some sample workspaces. 

We also have a webinar on this topic Optimizing XML & JSON Workflows for Seamless Data Integration with a related demo in the Transformer Comparison section starting at around minute 18. 

Hope this helps! 


sar_copp
Contributor
Forum|alt.badge.img+4
  • Contributor
  • February 26, 2025

Hello ​@crystalatsafe Ive watched the webinar you linked which was helpful and I need to try out the json templater in more depth. I didnt find the getting started tutorial super helpful because I have zero understanding about json- why cant everything just be in a structured table format (haha). The issue is that whilst we have a json schema to follow, the outputs are quite dynamic, dependent on the type of record in the table (all the same table). Does the json templater allow for this?

 

e.g. 1)  No Waiting At Any Time Traffic Restriction (i.e. double yellow lines) will have to have certain items in the json output file.

  1. Weight Restriction for a road (e.g. 3 tonnes limit on a road) will need to produce a completely different set of items in the json output for that record.

 

Ive attached 2 examples in the Zip file, is this even possible what Im trying to achieve?


danilo_fme
Evangelist
Forum|alt.badge.img+45
  • Evangelist
  • February 26, 2025
crystalatsafe wrote:

Hi ​@sar_copp 

We have a JSON tutorial series that may help: Tutorial: Getting Started with JSON the tutorial series includes some sample workspaces. 

We also have a webinar on this topic Optimizing XML & JSON Workflows for Seamless Data Integration with a related demo in the Transformer Comparison section starting at around minute 18. 

Hope this helps! 

Great tutorial ​@crystalatsafe 


sar_copp
Contributor
Forum|alt.badge.img+4
  • Contributor
  • February 26, 2025

Anyone else got any wisdom, this is proving frustratingly difficult.


crystalatsafe
Safer
Forum|alt.badge.img+18

Hi ​@sar_copp 

Can you create a separate post with a sample of the input feature for both cases you mentioned and the expected output from those sample features? Creating a new post will increase visibility in the community so more people will be able to see it.

Generally, it is possible to use conditionals with a JSONTemplater. It can be done within the XQuery in the JSONTemplater using an if-then-else statement (example here) or possibly using a Tester/TestFilter with subtemplates in the JSONTemplater. 


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