Question

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

  • 6 March 2022
  • 2 replies
  • 99 views

Userlevel 2
Badge +17

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?


2 replies

Userlevel 2
Badge +17

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

 

Userlevel 4
Badge +30

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!

Reply