Docs / Learn mapping / Mapper Values
Build typed JSON from X12
Select business records, project the fields your receiving system needs, and serialize the result with toJson. Build dictionaries and lists directly so numbers, booleans, nested records, and null keep their types.
Project a record for each order line
lines = forEach(line in ST->PO1+ => {
"itemNumber": requiredQualifierValue(line, "IN", 06),
"quantity": line(02),
"unit": line(03),
"unitPrice": line(04),
"priceBasis": line(05)
})
toJson({
"purchaseOrderNumber": ST->BEG(03),
"lines": lines,
"lineCount": countOf(line in lines)
})
The projection arrow => computes one value per selected line. Here that value is a dictionary, so the result is a list of records. toJson handles quotes, escaping, commas, and numeric formatting.
X12 element definitions carry types. Quantities and prices become JSON numbers, including a transmitted price such as .73, which becomes 0.73. Item IDs, UPCs, line identifiers, and dates stay text. Use toNumber when you intentionally need numeric text as a number, such as a line-number sort key; keep the original identifier in the output.
Keep each quantity with its unit. The transmitted unit price is numeric; keep its price-basis qualifier too, because calculating a line amount also requires interpreting its price basis. See price bases and calculated amounts before multiplying quantity by price.
Choose a stable output shape when child segments repeat. The richer order example trims each populated PID description and joins them in source order with one space, so description stays a string. It omits the field when every description is absent or blank. If your receiving system needs separate descriptions, project a list and give that field an array schema instead.
Name date fields for their qualifier's meaning. In the purchase-order demos, DTM01=037 supplies shipNotBeforeDate and 038 supplies shipNoLaterDate: the bounds of a shipping window. 010 is Requested Ship, a separate date. A latest shipping date does not itself instruct the receiver to cancel an order.
Keep the currency with monetary values
The complete JSON order demo includes currencyCode from CUR02, alongside numeric prices and amounts. It uses firstNonEmpty(ST->CUR?(02), "USD"), matching the printable demo. USD is an explicit demo convention when CUR is absent, not a universal X12 default. Agree on the default with your partner or require a supplied currency. Selecting EUR or GBP preserves that code without converting any amount.
Keep an absent reported amount as null
reportedTotal = ST->CTT?->AMT[01 == "TT"]?(02)
toJson({
"totalAmount": isEmpty(reportedTotal) ? null : reportedTotal
})
An absent amount produces {"totalAmount":null}. A supplied zero produces {"totalAmount":0}. A reported total and a total calculated from lines are different business facts; choose deliberately which one your output represents.
null is a native scalar literal, not the string "null". It survives assignments, projections, dictionary fields, list entries, indexing, and JSON serialization. For example, toJson({"values":[null,0,false,"null"]}) produces {"values":[null,0,false,"null"]}.
Dictionary construction omits empty text, empty selections, and empty ordinary lists. Explicit null keeps the field. Missing X12 selections still have their existing empty-selection behavior; use a ternary when you want to translate absence into null.
Keep a required array field when there are no matches
Dictionary construction omits a field whose value is an empty ordinary list. This is a construction rule, not a rule that toJson removes empty arrays from existing dictionaries:
toJson({"lines":[]})
// {}
The same applies to an empty projection, such as toJson({"lines":forEach(n in [] => n)}), or a filter with no matches, such as toJson({"lines":forEach(n in [1,2] : n > 2 => n)}). Both produce {}. A one-entry ordinary list containing empty text also normalizes away: toJson({"references":[""]}) produces {}.
If the receiving schema requires an array property even when it has no entries, assign the field by index after constructing the dictionary:
result = {}
result["lines"] = []
toJson(result)
// {"lines":[]}
Assign a computed list the same way: result["lines"] = forEach(n in [1,2] : n > 2 => n) retains "lines":[] when nothing matches. This differs from toJson({"lines":null}), which produces {"lines":null}: an empty array means there are no entries; null represents the absence you chose to report.
A nonempty outer list retains its structure even when its entries are empty lists or empty text lists. For example, toJson({"rows":[[]]}) produces {"rows":[[]]}, and toJson({"rows":[[""]]}) produces {"rows":[[""]]}.
Choose defaults without losing zero or false
isEmpty(null) is true, firstNonEmpty skips null, and required(null, "Total required") reports E2801. Zero and false are populated values. A nonempty list such as [null] is also present.
If every candidate is empty, firstNonEmpty returns empty text. Use a ternary for a null fallback; firstNonEmpty(value, null) does not retain null. Only supply zero before arithmetic when zero has the intended business meaning.
Null has no implicit numeric or Boolean conversion. toNumber(null) reports E4602, numeric arithmetic such as null * 2 reports E2402, and null ? 1 : 2 reports E2504. Supply an explicit default before calculations or sorting.
Compare absence explicitly
null == null is true. Null differs from empty text, empty selections, zero, false, and the text "null". Use isEmpty when you want to check the broader notion of absence.
Text conversion renders null as null. Concatenation with +, dictionary keys, and groupBy keys use that text. The text comparison operators retain their text rules: null %== "NULL" is true. Use ordinary == or != to distinguish null from text.
Practice one mapping pattern
Practice these rules in Write reliable incoming maps, or apply typed projections to nested shipment data in Map an 856 with multiple orders and cartons. For the complete learning path, start with Your first X12 map.
Use grouping and ordering, comparisons, and the Mapper Built-ins reference for their specific rules. Save representative variations as mapping test cases.