Skip to content

JSON

JSON represents structured data as text. Decode a fictional position, then separate syntax, field rules and the units needed to value it.

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.
In Python, loads decodes text and dumps encodes a value as text:
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 JSON position ABC has qty 2, active true and price 100. Parsing produces Python values; a separate contract specifies shares, USD per share and inclusion, giving a USD 200 position value.
Fictional JSON position ABC has qty 2, active true and price 100. Parsing produces Python values; a separate contract specifies shares, USD per share and inclusion, giving a USD 200 position value.
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:

BoundaryQuestion for the ABC position
ParsingDoes the text follow the accepted JSON syntax?
Schema validationAre required fields present, with the allowed types and ranges?
Financial validationAre 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 NaN and Infinity. Python's json defaults accept and emit them as extensions. allow_nan=False makes the encoder reject nonfinite floats. On decoding, a parse_constant callback 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. An object_pairs_hook callback 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

Last updated
  • json
  • data serialisation
  • python
  • data contracts
← Back to the glossaryBack to top ↑