Suppressing scientific/exponential notation in JSLT

Viewed 97

I am transforming a Json document that contains a number field with a value of 30000000.00.

I am transforming within Java using the following spec (I have truncated after the relevant lines):

    StringBuilder jsltSpec = new StringBuilder();

    jsltSpec.append("import \"http://jslt.schibsted.com/2018/experimental\" as exp ");

        jsltSpec.append("def timeVal(obj) " +
                "if ($obj) " +
                "if (size($obj) == 5) " +
                "format-time(parse-time(join($obj, \"-\"), \"yyyy-M-d-HH-mm\"), \"yyyy-MM-dd'T'HH:mm:ss.SSSX\") " +
                "else " +
                "format-time(parse-time(join($obj, \"-\"), \"yyyy-M-d-HH-mm-ss\"), \"yyyy-MM-dd'T'HH:mm:ss.SSSX\") " +
                "else " +
                "null ");

        jsltSpec.append("def dateVal(obj) " +
                "if ($obj) " +
                "format-time(parse-time(join($obj, \"-\"), \"yyyy-M-d\"), \"yyyy-MM-dd'T'HH:mm:ss.SSSX\") " +
                "else " +
                "null ");

        jsltSpec.append("let nestedTran = " +
                "[ " +
                "for (.responseBody.eelaccountsList) " +
                "let sa = " +
                "{ " +
                "\"Transactions\" : " +
                "[ " +
                "for (.transactions) " +
                "let st = " +
                "{ " +
                "\"@type\" : .\"@type\"," +
                "\"accountId\" : .accountId, " +
                "\"amount\" : .amount, " +
                "\"category\" : .category, " +

The amount field that is produced is 3.0E7 which is not acceptable to the downstream consumer.

Does anyone know if there is a formatting function available that can be used to suppress the exponential notation?

Thanks,

David

1 Answers

This is actually not a JSLT issue. Since your transform says:

  "amount" : .amount, 

JSLT is not changing the amount at all. The same JsonNode object that was passed to JSLT will be in the output. Literally the same object -- they would test equal with ==.

So this is about how Jackson is serializing your decimal literal.

Note that 3.0E7 and 30000000.00 are the same number, so this is purely a syntax issue. The actual underlying number is the same.

You can try setting this feature in your ObjectMapper to tell Jackson not to use scientific notation.

Related