Rounding numbers in array with jsonata through change node

Hi all. I need to round certain variables in a data feed that's coming in the following format:

image

For instance the "gap" variable to 2 decimal places. It seems this can be done simply using jsonata's $round function through the change node. I can't figure out how to address individual variables to process the entire array and get it out in the same format. Thanks so much!

[{"ticker":"NASDAQ:LASE","name":"LASE","close":7.93,"gap":-3.7037037037,"relative_volume_10d_calc":20.8748866128,"change":83.5648148148,"premarket_change":-1.8518518519,"float_shares_outstanding":3858570.0383889996,"change_from_open":90.625},{"ticker":"NASDAQ:OTRK","name":"OTRK","close":2.76,"gap":64.8351648352,"relative_volume_10d_calc":461.2613085129,"change":51.6483516484,"premarket_change":65.3846153846,"float_shares_outstanding":1248683.6766000001,"change_from_open":-8},{"ticker":"AMEX:BURU","name":"BURU","close":1.01,"gap":33.4824424066,"relative_volume_10d_calc":58.3945386478,"change":125.90024603,"premarket_change":29.9485573697,"float_shares_outstanding":3556713.4752,"change_from_open":69.235924933},{"ticker":"NASDAQ:BPTH","name":"BPTH","close":1.19,"gap":50.3582755785,"relative_volume_10d_calc":739.1336072032,"change":39.7862093269,"premarket_change":50.3582755785,"float_shares_outstanding":2550487.68927,"change_from_open":-7.03125},{"ticker":"AMEX:RHE","name":"RHE","close":2.48,"gap":12.6373626374,"relative_volume_10d_calc":1352.2586177328,"change":36.2637362637,"premarket_change":14.2857142857,"float_shares_outstanding":1699040.22617,"change_from_open":20.9756097561},{"ticker":"NASDAQ:XRTX","name":"XRTX","close":1.8,"gap":-0.6535947712,"relative_volume_10d_calc":7.2865541922,"change":17.6470588235,"premarket_change":null,"float_shares_outstanding":2698095.96538,"change_from_open":18.4210526316},{"ticker":"OTC:RLFTF","name":"RLFTF","close":4.91,"gap":25.1445086705,"relative_volume_10d_calc":7.2145669291,"change":41.9075144509,"premarket_change":null,"float_shares_outstanding":8255497.121424,"change_from_open":13.3949191686}]

You could use an expression like:

$$.payload.{
      
    "ticker": $.ticker,
    "name": $.name,
    "close": $.close,
    "gap": $round($.gap,2),
    "relative_volume_10d_calc": $round($.relative_volume_10d_calc,2),
    "change": $round($.change,2),
    "premarket_change": $.premarket_change ? $round($.premarket_change,2) : 0,
    "float_shares_outstanding": $round($.float_shares_outstanding,2),
    "change_from_open": $round($.change_from_open,2)
  
}

note that the 'premarket_change' null values will be changed to 0.
pretty sure that the jsonata wizards may have better solutions.

See https://try.jsonata.org/h1ECOlq3A

This is not going to work as the expression is mapping the $$ (msg) property. The issue is you did not set the JSONate exerciser input object correct. It needed to be

{
  "payload": [
    {...},
    {...}
  ]
}

Then the expression would
start
$$.payload.{....

I explicitly mentioned you :wink:

So what would you advise? This is the format that's currently working with my table config? Do I need to transform the data prior to passing it to the change node? Should it just be done through a function?

Use @bakman2 expression just add payload ( or the correct property as your image obsures this information) to the beginning.

Or
depending on the table node you could use the table to round the information it receives

I have modified my answer.

Perfect, thanks! Just one minor issue; I have a variable called "relative_volume_10d_calc|5" ... getting an error because of the "|".

P.S. originally I was doing the rounding in the table, but I needed to apply other formatting as well and tabulator doesn't seem to allow multiple types of formatting (only via custom functions).

suround it with backticks

`relative_volume_10d_calc|5`

Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.