Handling Data Transformation

1. Transforming Request Data

TransformationExample
Trim whitespace" alice ""alice"
Lowercase emails"Alice@X.COM""alice@x.com"
Normalize phone"(555) 123-4567""+15551234567"
Parse datesString → LocalDateTime/Instant
Decode base64String → byte[]

2. Transforming Response Data

TransformationPurpose
Entity → DTOHide internal fields, shape response
Enum → StringSerialize as readable name
BigDecimal → StringAvoid float precision loss
Lazy → eager loadingAvoid N+1 queries

3. Implementing Data Mapping

ToolLanguageApproach
MapStructJavaCompile-time annotation processor
ModelMapperJavaRuntime reflection
AutoMapper.NETConvention-based
class-transformerTypeScriptDecorator-based

4. Converting Date Formats

FormatExampleUse
ISO 86012026-05-15T10:30:00ZStandard for APIs
Unix timestamp1747304400Compact, no TZ ambiguity
Unix millis1747304400000JS Date compatibility
RFC 1123 (HTTP)Sat, 15 May 2026 10:30:00 GMTHTTP headers only

5. Handling Time Zones

Warning: Always store timestamps in UTC. Convert to user's TZ only at presentation layer.
StrategyNotes
UTC in APISuffix Z in ISO 8601
Include offset2026-05-15T10:30:00-05:00
IANA TZ name"timezone": "America/New_York" (separate field)
AvoidLocal time without offset, abbreviations (EST, PST)

6. Sanitizing User Input

ThreatMitigation
XSSHTML-escape before rendering; use CSP
SQL InjectionParameterized queries / ORMs
NoSQL InjectionValidate object shapes; reject operators
Command InjectionWhitelist; never pass to shell
Path TraversalReject ../, canonicalize paths
Mass AssignmentAllowlist fields (DTO pattern)

7. Implementing Case Conversion

ConversionUse
snake_case ↔ camelCaseJava backend ↔ JS frontend
Jackson PropertyNamingStrategy.SNAKE_CASEAuto-convert Java fields
JSON.parse with reviverJS-side conversion

8. Removing Sensitive Fields

FieldAction
Password hashesNever serialize (Jackson @JsonIgnore)
Internal IDsStrip in DTOs
PII (SSN, DOB)Mask or omit per role
Tokens / secretsNever log or return

Example: Field-Level Filtering (Java/Jackson)

public class User {
    private Long id;
    private String email;

    @JsonIgnore
    private String passwordHash;

    @JsonProperty(access = Access.WRITE_ONLY)
    private String currentPassword;
}

9. Implementing Data Enrichment

EnrichmentSource
Computed fieldsServer calculation (totals, age from DOB)
Resolved referencesEmbed related resources (?expand=author)
Localized labelsi18n service per Accept-Language
Geocoded addressesExternal lookup added to response

10. Using Transformation Libraries

LibraryLanguagePurpose
JacksonJavaJSON serialization, custom serializers
MapStructJavaDTO ↔ Entity mapping
LombokJavaBoilerplate reduction
class-transformerTS/NodePlain ↔ class instance
MarshmallowPythonSchema-based serialization
PydanticPythonModel validation + serialization