Module 4 of 52

Data representation and formats

How computers encode characters, numbers, images, and structured records, and why format and encoding choices have real consequences for data quality.

By the end of this module you will be able to:

  • Explain why UTF-8 dominates web encoding and how it differs from ASCII
  • Describe the IEEE 754 floating-point precision problem with a concrete financial example
  • Compare JSON, XML, and CSV for a given use case
  • Distinguish lossless from lossy compression and identify appropriate uses for each

Format boundaries: where CSV, JSON, Parquet, and RDF hit their limits

Read across the schema row and the four formats split: CSV leaves it implicit in the header and JSON makes it optional, while Parquet embeds it and Turtle carries vocabularies, so the two loose formats hand schema discovery to whoever reads the file next.

Format choice is a five-property decision: native shape, schema, validation, compression and typing, and fit for purpose. RFC 4180 CSV is row-shaped; RFC 8259 JSON is tree-shaped; Parquet adds columnar typing; W3C RDF Turtle is graph-shaped.

Format choice is a two-by-two, and schema is the hard boundary A two-by-two field of four format cards. Across the field, the left column is flat and tabular and the right column is nested and linked, captioned under a rule at the foot. Down the field, one hard red rule runs the full width, labelled the boundary that matters most, schema, with schema optional written above it and schema bound in below it. Above the rule sit CSV, flat rows, and JSON, trees. Below it sit Parquet, typed columns, and RDF Turtle, graph of triples. Each card names its format, tags its native shape on the right, and lists its schema in red, its validation, its compression and typing, and its best fit. Format boundaries, and where each shape hits its limit CSVSchemaImplicit, by headerValidationBy conventionCompression / typingNoneBest fitSpreadsheet exchangeFlat rows JSONSchemaOptional, JSON SchemaValidationTooling levelCompression / typingText + UTF-8Best fitWeb APIsTrees ParquetSchemaEmbedded + strictValidationBuilt-inCompression / typingColumnar + zstdBest fitAnalytics warehousesTyped columns RDF TurtleSchemaVocabularies + SHACLValidationSHACL shapesCompression / typingText + UTF-8Best fitLinked open dataGraph of triples schema optionalschema bound inThe boundary that matters most: schema Flat and tabularNested and linked

The same three records laid out for rows and laid out for columns

The same two-field query touches eighteen cells under row layout and six under columnar, because rows keep a record together and columns keep a field together, which is why wide scans go columnar and single-record writes stay in rows.

Row layout keeps a record together, so a two-field query still reads all six fields of every record. Columnar layout keeps a field together, so the same query reads only what it asked for, which is why scans go columnar and single-record writes stay in rows.

The same three records laid out for rows and laid out for columns A key names the three tints: read from storage, the two fields the query wants, and not read at all. A red band states the query, two of six fields, and an arrow drops into each lane. The left lane, Row layout, is headed with its verdict, reads all 18 cells to return 6, and draws the three records as three horizontal strips holding all six fields: id, time, kwh, site, rate, flag. Every cell is tinted and the two wanted ones carry a deep red border. The right lane, Columnar layout, is headed reads 6 cells to return 6 and draws the same records as six vertical field groups; only the time and kwh groups are tinted, and each is ringed in the same deep red. A red band closes with the trade. Same three records, two storage layouts read from storagethe two fields the query wantsnot read at all The query reads two of six fields reads all six fields of every record reads two fields only Row layoutone record stays togetherReads all 18 cells to return 6 r1idtimekwhsiterateflag r2idtimekwhsiterateflag r3idtimekwhsiterateflag Columnar layoutone field stays togetherReads 6 cells to return 6 idr1r2r3 timer1r2r3 kwhr1r2r3 siter1r2r3 rater1r2r3 flagr1r2r3 Row layout wins the single-record write, columnar wins the wide scan

How character encoding either preserves or corrupts text

One chain of character, code point and bytes serves both readings, and they part only at the receiver, so the corruption in the lower branch comes from an unstated encoding rather than from anything that happened to the data in transit.

Character encoding is the contract that lets the receiver read what the sender wrote. A mismatch turns valid bytes into mojibake. Unicode Standard 17.0, chapter 3, separates the character from the code point and the bytes; RFC 8259 §8 makes UTF-8 the JSON default.

Encoding agreements decide whether characters survive exchange One shared path that forks. Across the top, three cards joined by red arrows: Character, holding A or e-acute or a Chinese character; Code point, holding U+0041, U+00E9, U+4E2D; and Bytes, a UTF-8 encoded stream. Two red arrows drop from Bytes to two outcome cards. The left, eyebrow Success, encoding matches, is headed Receiver and reads Same character read. The right, eyebrow Failure, encoding mismatch, is emphasised in red, is headed Receiver expects ISO-8859-1, and shows e-acute arriving as A-tilde plus copyright sign and the Chinese character as a-umlaut, cedilla and an invisible soft hyphen. A filled red band closes the figure: state the encoding next to the file. Character encoding is the contract between sender and receiver 1CharacterA or é or 中 2Code pointU+0041, U+00E9, U+4E2D 3BytesUTF-8 encoded stream Success · encoding matchesReceiverSame character read Failure · encoding mismatchReceiver expects ISO-8859-1A or é or ä¸ plus an invisible soft hyphen State the encoding next to the fileUTF-8 is the only safe default. CSVW §5 and RFC 8259 §8 both make this explicit; an unstated encoding is a data-loss waitingto happen.

Character encoding: ASCII, UTF-8, and Unicode

A character encoding is a mapping between characters (letters, digits, symbols) and their binary representations. ASCII (American Standard Code for Information Interchange, standardised 1963) encodes 128 characters using 7 bits: the 26 uppercase and 26 lowercase Latin letters, digits 0-9, punctuation, and 33 control characters. It cannot represent accented characters, non-Latin scripts, or even the pound sign (£).

UTF-8 (Unicode Transformation Format, 8-bit) is a variable-width encoding of the Unicode character set. It is backward-compatible with ASCII for the first 128 characters but uses 2 to 4 bytes for characters outside that range. The pound sign £ (Unicode U+00A3) encodes as two bytes in UTF-8 (0xC2 0xA3). In the legacy Windows-1252 encoding, it encodes as a single byte (0xA3). A file saved as Windows-1252 and read as UTF-8 produces garbled output because 0xA3 in UTF-8 is a continuation byte in a multi-byte sequence, not a standalone character.

The web has largely converged on UTF-8 because it can represent the full Unicode character set while preserving ASCII compatibility for the first 128 code points. JSON exchanged outside a closed ecosystem must be encoded as UTF-8 under RFC 8259, so encoding is a contract requirement, not a display preference.

Text errors come from treating bytes as characters without the right encoding. Numeric errors come from treating approximations as exact values.

Numbers: IEEE 754 floating-point and decimal precision

Floating-point numbers use the IEEE 754-2019 standard. A 32-bit float uses 1 sign bit, 8 exponent bits, and 23 mantissa bits. Floating-point numbers cannot represent most decimal fractions exactly in binary: 0.1 is a repeating binary fraction. This means 0.1 + 0.2 in floating-point arithmetic does not equal exactly 0.3. For financial calculations, always use fixed-point decimal types (SQL DECIMAL, Python's decimal.Decimal), not floating-point.

Choosing a numeric type settles what a single value means. The next choice is the container that carries whole records between systems.

Structured data formats: CSV, JSON, XML, and binary alternatives

Three formats dominate structured data exchange:

  • CSV: rows of comma-separated values, human-readable, universally supported. Weaknesses: no type information, no nesting, ambiguous handling of commas within values.
  • JSON: supports nesting, arrays, and typed values (strings, numbers, booleans, null). The dominant REST API format. Required by RFC 8259 to be UTF-8 encoded.
  • XML: supports schemas (XSD), namespaces, and complex document structures. More verbose than JSON. Prevalent in legacy enterprise systems, financial messaging (SWIFT, ISO 20022), and healthcare (HL7 v2/v3).

Apache Parquet and Apache Avro are binary formats optimised for large-scale analytics. Not human-readable, but offer far better compression and query performance than text formats. Parquet is columnar, enabling efficient queries that access only specific columns in large datasets.

Once values are represented, systems still need to move and store them efficiently. Compression is safe only when the loss model matches the data.

JSON text exchanged between systems that are not part of a closed ecosystem MUST be encoded using UTF-8. Implementations MUST NOT add a byte order mark (U+FEFF) to the beginning of a networked-transmitted JSON text.
Paraphrased from RFC 8259 (IETF), Section 8.1: Character Encoding

Compression: lossless and lossy

Lossless compression allows perfect reconstruction of the original data from the compressed version. No information is discarded. Examples: DEFLATE (used in ZIP and PNG), LZ4, Zstandard. Appropriate for text, code, databases, and any data where integrity is essential.

Lossy compression permanently discards some information to achieve higher compression ratios. The original data cannot be fully reconstructed. Examples: JPEG (images), MP3 (audio), H.264 (video). Appropriate for media where small quality reductions are imperceptible to human senses.

A 24-megapixel RAW photograph might be 24 MB. As a high-quality JPEG it might be 4 MB (6:1 ratio) with no visible quality loss. As a low-quality JPEG it might be 400 KB (60:1 ratio) with visible artefacts. PNG lossless would be approximately 18 MB, preserving every pixel.

Never apply lossy compression to structured data. Applying JPEG compression to a CSV file would corrupt the data silently. Re-saving a JPEG introduces additional quality loss each time; this is why document archives should use lossless formats.

Common misconception

“JSON is just text, so there are no encoding issues as long as the JSON syntax is valid.”

RFC 8259 requires JSON to be encoded in UTF-8. Many real-world JSON producers emit Windows-1252 or ISO 8859-1 encoded files with a .json extension. Parsers that do not validate encoding before parsing will either fail on non-ASCII characters or produce incorrect character data silently. If a supplier sends JSON containing French accented characters (such as in city names) and it was saved in Windows-1252, your UTF-8 parser will raise an error or corrupt those characters. Always validate encoding before parsing, and require UTF-8 explicitly in API contracts.

Check your understanding

A financial system stores transaction amounts as 32-bit single-precision floats. A transaction for £99.99 is stored and then retrieved. The system displays £99.98999786376953. What is the root cause?

A data engineer receives a JSON file from a third-party supplier containing French city names with accented characters such as Nimes. The engineer's parser raises an error on several records. The file has a .json extension. What is the most likely cause?

Core distinctions

  • ASCII encodes 128 characters in 7 bits; UTF-8 extends this to the full Unicode character set using variable-width encoding and is backward-compatible with ASCII.
  • IEEE 754 floating-point cannot represent most decimal fractions exactly. Financial data must use fixed-point decimal types (SQL DECIMAL, Python's decimal.Decimal). The 0.1 + 0.2 != 0.3 problem is a direct consequence.
  • JSON, CSV, and XML have different trade-offs: CSV is compact and universal but lacks types and nesting; JSON supports nesting and is the dominant API format; XML is verbose but schema-capable, used in finance and healthcare.
  • Lossless compression (DEFLATE, LZ4) preserves data exactly. Lossy compression (JPEG, MP3) discards information permanently. Never apply lossy compression to structured data files.

Knowing how data is encoded and formatted prepares you for the next question: what does a value mean once it leaves the system that wrote it? The next module, Metadata and context, covers the descriptive, structural and administrative information that travels with data and tells a reader what it is, where it came from and how it may be used.

Standards and sources cited in this module

  1. RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format

    Section 8.1: UTF-8 encoding requirement for JSON. The formal specification of JSON syntax.

  2. IEEE 754-2019: Standard for Floating-Point Arithmetic

    Floating-point representation format: single precision (32-bit) and double precision (64-bit) specifications.

  3. Unicode Standard 17.0 (2025)

    Current Unicode standard version. Chapter 2 covers the general structure of Unicode encoding.

  4. WHATWG Encoding Standard

    Living standard for web encoding behaviour, including UTF-8 handling and legacy label compatibility.