Change "null" to "0" in an array

Hi, I´ve an big array like this:

[{"from":"2024-09-16T22:00:00.000+02:00","cost":0.0014874048,"unitPrice":0.2479008,"consumption":0.006},{"from":"2024-09-16T23:00:00.000+02:00","cost":0.001255688,"unitPrice":0.2511376,"consumption":0.005},{"from":"2024-09-17T00:00:00.000+02:00","cost":0.001023638,"unitPrice":0.2559095,"consumption":0.004},{"from":"2024-09-17T01:00:00.000+02:00","cost":0.0007561617,"unitPrice":0.2520539,"consumption":0.003},{"from":"2024-09-17T02:00:00.000+02:00","cost":0.0009996952,"unitPrice":0.2499238,"consumption":0.004},{"from":"2024-09-17T03:00:00.000+02:00","cost":0.0007473795,"unitPrice":0.2491265,"consumption":0.003},{"from":"2024-09-17T04:00:00.000+02:00","cost":0.0007480935,"unitPrice":0.2493645,"consumption":0.003},{"from":"2024-09-17T05:00:00.000+02:00","cost":0.0010521028,"unitPrice":0.2630257,"consumption":0.004},{"from":"2024-09-17T06:00:00.000+02:00","cost":0.0014150885,"unitPrice":0.2830177,"consumption":0.005},{"from":"2024-09-17T07:00:00.000+02:00","cost":0.001881033,"unitPrice":0.3135055,"consumption":0.006},{"from":"2024-09-17T08:00:00.000+02:00","cost":0.0022243599,"unitPrice":0.3177657,"consumption":0.007},{"from":"2024-09-17T09:00:00.000+02:00","cost":0.0017023188,"unitPrice":0.2837198,"consumption":0.006},{"from":"2024-09-17T10:00:00.000+02:00","cost":0.0018828299,"unitPrice":0.2689757,"consumption":0.007},{"from":"2024-09-17T11:00:00.000+02:00","cost":0.001765127,"unitPrice":0.252161,"consumption":0.007},{"from":"2024-09-17T12:00:00.000+02:00","cost":0.0015770356,"unitPrice":0.2252908,"consumption":0.007},{"from":"2024-09-17T13:00:00.000+02:00","cost":0.0014267624,"unitPrice":0.2038232,"consumption":0.007},{"from":"2024-09-17T14:00:00.000+02:00","cost":0.000369257,"unitPrice":0.1846285,"consumption":0.002},{"from":"2024-09-17T15:00:00.000+02:00","cost":0.014076034,"unitPrice":0.2070005,"consumption":0.068},{"from":"2024-09-17T16:00:00.000+02:00","cost":0.024737244,"unitPrice":0.242522,"consumption":0.102},{"from":"2024-09-17T17:00:00.000+02:00","cost":0.005307638,"unitPrice":0.2653819,"consumption":0.02},{"from":"2024-09-17T18:00:00.000+02:00","cost":0,"unitPrice":0.288575,"consumption":0},{"from":"2024-09-17T19:00:00.000+02:00","cost":0.001559138,"unitPrice":0.3118276,"consumption":0.005},{"from":"2024-09-17T20:00:00.000+02:00","cost":0.001718955,"unitPrice":0.2864925,"consumption":0.006},{"from":"2024-09-17T21:00:00.000+02:00","cost":0.0010739988,"unitPrice":0.2684997,"consumption":0.004}]

visual:

sometimes I get a "null" somewhere.

I want to replace each "null" by "0".

Is this possible?

greetings

I think you can use the map function (I don't use it much, but getting more exposure to it latley)

// Function Node

const myArray = msg.payload;

const ToZero = myArray.map(el => {
  return {
    from: el.from,
    cost: el.cost ?? 0,
    unitPrice: el.unitPrice ?? 0,
    consumption: el.consumption ?? 0
  };
});
msg.payload = ToZero;
return msg;

I'm sure this can also be done using JSONata (Cough cough @E1cid :wink: )

2 Likes

possibly the transform function

$$.payload ~> |$|
    { 
        "cost": $.cost = null ? 0,
        "unitPrice": $.unitPrice  = null ? 0,
        "consumption": $.consumption = null ? 0
    }
|
2 Likes

or alternatively;

const input = msg.payload

const result = input.map(obj => {
    for (const key in obj) {
        if (obj[key] === null && key !== "from") {
            obj[key] = 0;
        }
    }
    return obj
})

msg.payload = result
1 Like

wow I'm speechless :star_struck: :+1:

so many roads lead to Rome, i picked E1cid´s because it is the shortest solution.

1 Like
for (const payload of msg.payload) {
    payload.cost ??= 0,
    payload.unitPrice ??= 0,
    payload.consumption ??= 0
}
return msg

My preference: Pure JS, faster, more readable & maintainable (as in there many more JS devs than JSONata devs)

PS: ??= means if property is null(ish), set it to 0.
See: Nullish coalescing assignment (??=)

2 Likes

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