JSON (JavaScript Object Notation) is a text format for exchanging structured data. A service can send a position, a research script can save configuration, and another program can recover the values. In quant work, the critical distinction is between recovering those values and knowing what they mean.
JSON is a form of data serialisation, often used in a REST API. It supplies a representation; it does not supply a trading or valuation contract.
One position, two kinds of information
This is an authored, offline fixture for fictional security ABC:
{"symbol": "ABC", "qty": 2, "active": true, "price": 100}Assume the producer and consumer have separately agreed that
qty counts shares, price is USD per share, and active decides whether to include this position. The included position is worth USD 200: two shares at USD 100 each. The text itself never specifies a currency. Another producer could use the same field names for lots or a price in cents.import json
text = '{"symbol": "ABC", "qty": 2, "active": true, "price": 100}'
position = json.loads(text)
value = position["qty"] * position["price"] if position["active"] else 0
print(f"{position['symbol']}: USD {value:.2f}")
print(json.dumps(position, allow_nan=False))
# ABC: USD 200.00
# {"symbol": "ABC", "qty": 2, "active": true, "price": 100}This calculation trusts the fixture and its agreed contract. A production consumer must validate incoming fields before applying that calculation.
Fictional position. Parsing recovers values; the contract supplies the units and interpretation needed for valuation.
What the format preserves
JSON supports objects, arrays, strings, numbers, booleans and
null. Object names are strings in double quotes. Arrays preserve element order; object-member order should not determine economic meaning. The literals true, false and null are lowercase. Comments and trailing commas are outside the JSON grammar. These rules come from RFC 8259.Python's default decoder maps objects to dictionaries, arrays to lists, booleans to
True/False, and null to None. A missing price member and "price": null are different inputs. Neither means a zero price unless the contract explicitly says so.A JSON number does not guarantee a particular machine precision. Python normally decodes fractional numbers as binary
float; floating-point arithmetic can approximate decimal prices. parse_float=Decimal, with Decimal imported from decimal, can preserve decimal input for decimal arithmetic. Encoding those values requires an agreed conversion policy. Identifiers such as "0012" belong in strings when their spelling matters.Decide the failure policy before ingestion
Three checks protect different boundaries:
| Boundary | Question for the ABC position |
|---|---|
| Parsing | Does the text follow the accepted JSON syntax? |
| Schema validation | Are required fields present, with the allowed types and ranges? |
| Financial validation | Are these the intended security, units, currency and observation time? |
JSON Schema is a separate language for declaring structural constraints. A schema validator enforces the rules supplied to it; it cannot establish that a quoted market price is correct. For example,
qty being numeric would be too weak if only whole shares are allowed.Two interoperability decisions deserve explicit policies:
- Nonfinite numbers. RFC 8259 forbids the tokens
NaNandInfinity. Python'sjsondefaults accept and emit them as extensions.allow_nan=Falsemakes the encoder reject nonfinite floats. On decoding, aparse_constantcallback can reject those tokens. This is separate from checking the range and finiteness of decoded numbers. - Duplicate member names. Names should be unique, but receivers differ when they repeat. Python's default decoder keeps the last value, so
{"qty": 2, "qty": 9}becomes a quantity of nine. Anobject_pairs_hookcallback can enforce a rejection policy before duplicates disappear into a dictionary. Do not assume every parser rejects them.
The Python json documentation specifies these options and defaults. The example above was executed with Python 3.14.4.
Continue with a checked response
The APIs lesson builds on this distinction with an offline quote fixture, numeric-price checks and an explicit currency check. The full lesson requires membership; a public preview is available.
Sources
- RFC 8259, sections 1–8: JSON values, grammar, ordering, duplicate names and numeric interoperability.
- Python 3.14: json encoder and decoder: conversion tables,
allow_nan,parse_float,parse_constantandobject_pairs_hook. - JSON Schema: What is JSON Schema?: schemas and validation as a separate layer.