Keep Phone Numbers as Strings in SMS JSON Data

Published: · Updated:
Share

A phone number identifies an address rather than a quantity. Preserve the API contract’s string representation throughout input, storage and output so numeric conversion does not discard useful characters.

Valid JSON and a valid address are different checks

JSON has distinct number and string types. Its number syntax does not allow an initial plus sign or arbitrary leading zeroes in a multi-digit integer. A string can retain those characters. The receiving API must still define whether it wants a complete international address or separate country and national components.

For a new internal model, you might define a destination string, a message identifier and a numeric count. Those are design examples, not field names promised by any SMS provider. When reading an existing API, follow its published types rather than running every digit-looking value through the same numeric conversion helper.

Phone addresses keep their schema type

Follow the value through every boundary

BoundaryCheck
Input controlThe address is not treated as arithmetic input
Application objectNo automatic integer or floating-point conversion
Persistent storageThe intended text representation survives
API outputThe documented type is returned consistently

A matching visual display does not establish a matching internal type. Use non-sensitive fixtures to compare the exact parsed value and its type in the client languages you support. Review normalisation separately: removing presentation separators according to a documented address rule is not the same operation as coercing the whole value into a number.

The same concern applies to record identifiers that happen to contain digits. Decide their types from the schema, not their appearance. Keep count fields numeric where arithmetic is intended; making all fields strings is no more precise than making them all numbers.

Keep an extension separate if the product models one separately. Appending it to a phone-address field without checking the contract can change the value the receiving system tries to interpret. The goal is faithful representation of the documented address model, not preserving arbitrary formatting in a single field.

Do not repair lost prefixes by guessing

If an old database contains only a numeric version, recover the address from an authorised resource listing, original import or another reliable source. Adding one country code to every record is not a safe reconstruction rule. Isolate unresolved rows instead of writing a plausible-looking destination into live configuration.

Repair legacy values using reliable sources

During migration, retain the source value and a traceable conversion rule for each input group. Test the international and national representations the contract actually permits, as well as absent and invalid input. Reject an unsupported representation clearly rather than deleting characters until it passes validation.

Finally, check lookup and deduplication rules. A storage fix can still leave records split between old and new keys if queries use a different canonical form. Verify that incoming events attach to the intended authorised resource and that historical associations remain understandable after the change. This tests the purpose of the field, beyond whether a JSON parser accepted the payload.

Reference: RFC 8259: JSON numbers and strings.

Need help?

Click here and start chatting with us!