Skip to main content

Hello,

In some cases, JSONFormatter writes a decimal point number with the full precision of floating point numbers.

Example:

Source

{ "number" : 1.954, "string" : "1.954" }

Formatted by JSONFormetter (FME 2021.2.2)

{

  "number" : 1.9540000000000002,

  "string" : "1.954"

}

 

I think this document is desired in many cases.

{

  "number" : 1.954,

  "string" : "1.954"

}

 

Is there any way to prevent adding the excess decimal places?

I found Python json module can be used to format JSON document, but hope that the JSONFormatter would be improved not to add excess decimal places to numeric values.

PythonCaller Script Example

import json
def jsonPrettyPrint(feature):
    d = json.loads(feature.getAttribute('_json_document'))
    feature.setAttribute('_json_document', json.dumps(d, indent=3))
 

 


I found Python json module can be used to format JSON document, but hope that the JSONFormatter would be improved not to add excess decimal places to numeric values.

PythonCaller Script Example

import json
def jsonPrettyPrint(feature):
    d = json.loads(feature.getAttribute('_json_document'))
    feature.setAttribute('_json_document', json.dumps(d, indent=3))
 

 

Nice!


3 years down the road and no improvement….


Why would there be the need for an improvement? You can already easily do this using the AttributeRounder and the CoordinateRounder.

 

Because how would FME know when to round attributes in JSon and how far to round them down? You may only need 3 decimals, but I might need 6, or 1, or 0. Using the AttributeRounder and the CoordinateRounder we can both easily achieve what we need.

And if it is unexpected behavior you’re seeing because you’re already using those two transformers: This thread would be a good read.


Why would there be the need for an improvement?  > read the initial post….😒