Skip to main content
Question

How can I prevent adding excess decimal places in the formatted JSON document?


takashi
Influencer

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?

5 replies

takashi
Influencer
  • Author
  • March 6, 2022

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

 


danilo_fme
Evangelist
Forum|alt.badge.img+43
  • Evangelist
  • March 6, 2022
takashi wrote:

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!


itay
Supporter
Forum|alt.badge.img+17
  • Supporter
  • April 6, 2025

3 years down the road and no improvement….


s.jager
Influencer
Forum|alt.badge.img+16
  • Influencer
  • April 7, 2025

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.


itay
Supporter
Forum|alt.badge.img+17
  • Supporter
  • April 7, 2025

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

 


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