Convert raw amounts to Decimal128
Load the practice orders. Open keplergrid_transform_lab.orders in KeplerGrid on a Read only connection. Select Aggregation → JSON, paste the pipeline, and choose Run pipeline.
[
{ "$sort": { "_id": 1 } },
{ "$set": {
"normalizedAmount": {
"$convert": {
"input": "$amountRaw",
"to": "decimal",
"onError": "invalid",
"onNull": null
}
}
} },
{ "$set": {
"amountCheck": {
"$switch": {
"branches": [
{ "case": { "$eq": ["$normalizedAmount", "invalid"] }, "then": "invalid" },
{ "case": { "$eq": ["$normalizedAmount", null] }, "then": "null or missing" }
],
"default": "converted"
}
}
} }
]
onError supplies the sentinel string when the conversion fails. onNull applies when the source is null or absent. The final stage makes the outcome explicit alongside the original amountRaw value.
Check the converted values in KeplerGrid
In Stages, select stage 2 to compare raw inputs with normalizedAmount, then select stage 3 to see the labels. The two successfully converted values are Decimal128. The raw values stay available for checking.
| Order | Normalized amount | Amount check |
|---|---|---|
| ORD-101 | 12.50 | converted |
| ORD-102 | "invalid" | invalid |
| ORD-103 | null | null or missing |
| ORD-201 | 10.00 | converted |
| ORD-202 | null | null or missing |
The result field can contain a number, a string, or null in this diagnostic pipeline. Filter for amountCheck: "converted" before calculating totals from it. Audit the BSON types on your own collection first if you need to limit which source types qualify for conversion.
See how to audit stored BSON types or how to validate future writes.