# Ord JSON mapping Version 0.8.0-draft, 2026-12-01. A companion spec to [Ord specification](Ord_Specification.md), of the same version. The Spec's conventions (S1 to S6) and versioning (S10) apply here, and rule IDs that start with `C`, `L`, `D` or `T` refer to the Spec and the [Test format](Test_format.md). ## 1. Scope - **J17.** This spec maps JSON to the Core data model (Spec, section 1) and back. It keeps the hierarchy: what is under a node in Ord is under its label in JSON, and simple Ord is simple JSON. JSON → Ord → JSON gives the same JSON value; Ord → JSON → Ord keeps what JSON has a place for, and section 6 lists what it doesn't. - **J1a.** The mapping is syntactic. It doesn't read label syntax (Spec Part 2), resolve references or check reserved names ([Semantics](Semantics.md) M92), so a reference is the text written, e.g. `client:: acme` gives `"client": "acme"`, and a key that starts with `_` is a key like any other, both ways: `_id:: acme` gives `"_id": "acme"`, and back. It reads only these: numbers (L22) and booleans (L27) among a field's items, which JSON has types for; `[]` and `{}`, an empty array and object (J4, J7); index keys (J20); the root field `_json` (J19); the JSON member `_ord` (J22); and the text tests of J23. - **J18.** A **JSON mapper** reads and writes JSON as this spec says, and passes the tests with the `from-json` and `to-json` options (T4). ## 2. Shapes These shapes are the same in both directions. - **J21.** A node's **node array** is what is under the node, as a JSON array: first, when the node has fields, one object of them, its **fields object**, each field a member (J5, J7a); then its child nodes, in order (J3, J4). A node with a label and children is an object with one member, its label, whose value is its node array (J3): `{"Kickoff": [{"date": "2026-12-01"}, "Book room"]}` is `Kickoff` with `date:: 2026-12-01` and `Book room` under it. The root is its node array, unless it has only fields (J2). - **J20.** An **index node** is a node, the root, a node with a label or a field's anonymous node (C13), whose children are only field lines, at least one, with unquoted keys `0`, `1`, `2` and so on, in file order: each key is its line's position, in digits without a leading zero. It is the JSON array of its fields' values (J5), in order, in place of its node array or object. So the root `0:: a`, then `1::` with `b:: 2` under it, is `["a", {"b": 2}]`, and `Acme` with `0:: x` and `1:: 5` under it is `{"Acme": ["x", 5]}`. A quoted key (`"0":: a`) or a key out of order makes an ordinary field. Readers write an array as index fields where no other rule maps it (J10, J11, J15), and quote the keys of an object that would read as an index node: `{"0": "a"}` gives `"0":: a`. - **J19.** A root whose only line is a field line with the unquoted key `_json` is the JSON value of that field (J5): `_json:: 5` is `5`, `_json:: "6"` is `"6"`, `_json::` is `null` and `_json:: []` is `[]`. A `_json` field anywhere else, or on a root with other lines, is the warning `J19.1`, and is an ordinary field. A root object whose only member is `_json` is written with the key quoted, `"_json":: 5`, and the quoted key is an ordinary field. - **J22.** The member `_ord`, with the value `"fields"` or `"node"`, marks an object that J26 reads with its other members: `"fields"` says they are fields, and `"node"` that the object is a node. It is a marker only in an object that, without it, has one member, whose value is a non-empty array, where J26 reads one; it is never a field. Anywhere else, or with another value, `_ord` is an ordinary member. Writers write it only where J26 would read the object as the other one (J24). - **J23.** Two text tests guess, where the structure doesn't say (J26), whether text is a field's or a node's: - A key **looks like a key** when it starts with a lowercase letter (Unicode `Ll`) or `_` and has no spaces or tabs: `attendees`, `due_date`, `_tags`. `Planning`, `Book room` and `Q4` don't. - A string **is prose** when it has three or more words (runs of characters other than spaces and tabs), or ends with `.`, `!` or `?`: `Welcome and introductions`, `Went well.`. `Anna Smith`, `acme.com` and `9.5` aren't. ## 3. Writing JSON - **J2.** The root becomes: - the value of its `_json` field, when that is its only line (J19) - an array, when it is an index node (J20) - an object of its fields, when it has only fields (J5, J7a) - `{}`, when it has no lines - otherwise its node array (J21). - **J3.** A node with children becomes an object with one member: the key is its label, as text, and the value its node array (J21), or, for an index node, its array (J20). An empty label is the key `""`. - **J4.** A node without children becomes its label as a string, whatever it reads as: labels are text, so a line `2026` is `"2026"`. The exceptions are an unquoted `{}` or `[]`, an empty object or array. An empty label, `::` or `""`, is `""`. - **J5.** A field's JSON value is: - `null`, if it has no items and isn't a list - the item itself, if it has one item and isn't a list - an array of its items, if it has several, or is a list (C16): `a;` gives `["a"]` and `;` gives `[]`. Its items, in order (C38), are its text items (J6, J7), its nodes (J3, J4) and its anonymous node, which is an object of its fields, or an array when it is an index node (J20). - **J6.** A text item becomes a string, unless J7 says otherwise. - **J7.** An unquoted text item that is a number (L22) becomes a JSON number, written as it is written in Ord, and one that is a boolean (L27) `true` or `false`; an unquoted `[]` becomes an empty array and `{}` an empty object: `n:: 1.50; 1e3; TRUE; []` gives `"n": [1.50, 1e3, true, []]`. A number keeps all its digits, whatever a JSON reader can hold. Every other value is a string: quoted text (`"3"`), other value forms such as dates, links and formulas as written. - **J7a.** Fields with the same key on one node become one member. Their items are concatenated in file order, and the result is always an array: `tag:: a` and `tag:: b` give `"tag": ["a", "b"]`. So a field line per record is an array of objects: `rows::` with `a:: 1` under it, then `rows::` with `a:: 3` under it, gives `"rows": [{"a": 1}, {"a": 3}]`. - **J24.** Where J26 would read an object of the JSON as a node and it is fields, the writer adds `"_ord": "fields"` to it, and where it would read fields and it is a node, `"_ord": "node"` (J22). So `Notes` with `todo` under it, and `Book room` under that, gives `{"Notes": [{"todo": ["Book room"], "_ord": "node"}]}`, since `todo` looks like a key. - **J25.** An Ord field `_ord` on an object that J22 would read as a marker gives a warning. It is written as the member it is. ```example to-json J2 J3 J4 J21 Acme →Call Anna →Workshop →→hours:: 6 →Planning →→Book room →→Send agenda . [ { "Acme": [ "Call Anna", { "Workshop": [{ "hours": 6 }] }, { "Planning": ["Book room", "Send agenda"] } ] } ] ``` ```example to-json J2 J3 J21 J24 client:: Acme Kickoff →attendees:: Anna; Erik →Book room Notes →todo →→Book room Plan →Steps:: a; →Call Retro →date:: 2026-12-01 →Planning →→Milk . [ { "client": "Acme" }, { "Kickoff": [{ "attendees": ["Anna", "Erik"] }, "Book room"] }, { "Notes": [{ "todo": ["Book room"], "_ord": "node" }] }, { "Plan": [{ "Steps": ["a"], "_ord": "fields" }, "Call"] }, { "Retro": [{ "date": "2026-12-01" }, { "Planning": ["Milk"] }] } ] ``` ```example to-json J4 J21 Call Anna 2026 {} [] "{}" :: "" . ["Call Anna", "2026", {}, [], "{}", "", ""] ``` ```example to-json J1a J2 J5 J6 J7 J7a a:: b:: "" c:: x; d:: [] e:: {} f:: ; g:: "{}" n:: 1.50; -2; TRUE; 1e3 s:: "3" dt:: 2026-10-15 v:: 007 tag:: a tag:: b contact:: →primary:: anna people:: →Anna →Erik k:: x →Anna →primary:: anna client:: [[Acme Corp]] _id:: acme . { "a": null, "b": "", "c": ["x"], "d": [], "e": {}, "f": [], "g": "{}", "n": [1.50, -2, true, 1e3], "s": "3", "dt": "2026-10-15", "v": "007", "tag": ["a", "b"], "contact": { "primary": "anna" }, "people": ["Anna", "Erik"], "k": ["x", "Anna", { "primary": "anna" }], "client": "[[Acme Corp]]", "_id": "acme" } ``` ```example to-json J7a rows:: x →a:: 1 →n:: 2 rows:: →a:: 3 rows:: y . { "rows": ["x", { "a": 1, "n": 2 }, { "a": 3 }, "y"] } ``` ```example to-json J2 J20 rows:: →0:: →→a:: 1 →1:: x; y →2:: same:: →"0":: a →1:: b Acme →0:: x →1:: 5 . [ { "rows": [{ "a": 1 }, ["x", "y"], null], "same": { "0": "a", "1": "b" } }, { "Acme": ["x", 5] } ] ``` ```example to-json J2 J20 0:: a 1:: →0:: b →1:: [] . ["a", ["b", []]] ``` ```example to-json J2 J19 _json:: "6" . "6" ``` ```example to-json J2 J19 _json:: 1e3 . 1e3 ``` ```example to-json J19 "_json":: 5 . { "_json": 5 } ``` ## 4. Reading JSON JSON is read by writing it as Ord, as these rules say, and reading the result (Spec, Part 1). - **J15.** The root becomes: - for an object, its members as the root's fields (J8), and nothing for `{}` - for an array, the root's lines as J11 reads a node array, except that an array whose only element J11 would read as the fields becomes index fields (J20), since fields alone are an object (J2) - for a scalar or `[]`, the field `_json` with the value, as J9 and J10 write a field's (J19): `5` gives `_json:: 5` and `[]` gives `_json:: []`. - **J8.** An object read as fields becomes a field line per member, with the member's key (J12), in order, and the value as J9 and J10 say; a marker `_ord` (J22) is left out. An object is read as fields as the root (J15), as the first element of a node array (J11, J26), as a field's value (J9, J26) and as an element of a field's array (J10, J26). - **J9.** A field's value: - A string becomes the field's item, quoted (C26) when it is a number (L22), a boolean (L27), `[]` or `{}` as written, so that it stays text: `"6"` gives `code:: "6"`. - A number becomes its text as written in the JSON, and a boolean `true` or `false`, so each reads back as its value form: `6`, `9.50`, `1e21` and `true` give `hours:: 6`, `9.50`, `1e21` and `true`. - `null` becomes no items, `key::`. - `{}` becomes `{}`. Any other object becomes the field's field children (J8), or, when J26 reads it as a node, its one node item (J11). - An array follows J10. - **J10.** A field's array: - `[]` becomes `key:: []`. - An array that holds `null`, or an array that isn't empty, becomes index fields (J20), under the field as its field children, each element an index field's value (J9, J10): `"e": [null, "y"]` gives `e::` with `0::` and `1:: y` under it, and `"m": [["a", "b"], ["c"]]` gives `m::` with `0:: a; b` and `1:: c;` under it. - Any other array becomes the field's items, in order, on one field line or more (J7a). Strings that aren't prose (J23), numbers, booleans, `[]` and `{}` are its inline items (J9); prose strings, and objects that J26 reads as nodes, are node lines under it (J11), so `"agenda": ["Welcome and introductions", "Budget"]` gives `agenda::` with the two lines under it; and an object read as fields (J26) is its field children (J8). A field line holds them in the order C38 allows: inline items, then node lines, of which a string after a node line is one too, then at most one object as its field children. A new field line with the same key starts where the next element doesn't fit: so a field line per record (J7a), and an inline item after a node line on a field line of its own. - An array on one field line with one item is a list (C16), with a `;` after its items: `key:: a;`, and `key:: ;` with the object's fields, or the node line, under it for one object or prose string. - **J11.** A **node array**, a root array (J15) or the array under a label (J3), becomes the node's lines (J21) when it isn't empty, its first element is a string, an object, `[]` or `{}`, and every other element is a string, `[]`, `{}` or a **node object**, an object with one member whose value is a non-empty array. Then: - A first element that is an object other than `{}` becomes the node's fields (J8), unless it is a node object that J26 reads as a node. - A string becomes a node line, its label the string, quoted (C26) when it is `[]` or `{}` as written, and `""` when it is empty (C42); a line is never read as a number or boolean, so `"6"` gives `6`. - `[]` and `{}` become the lines `[]` and `{}`. - A node object becomes a node line with the key as its label, quoted as a string is, and the lines under it that this rule reads from its array. Any other array, one with a number, a boolean, `null`, an array that isn't empty, or an object other than a node object after the first element, becomes index fields (J20), each element an index field's value (J9, J10): `["Call Anna", {"a": 1}]` gives `0:: Call Anna`, then `1::` with `a:: 1` under it. - **J26.** A node object (J11) can also be fields where it is the first element of a node array (J11), a field's value (J9) or an element of a field's array (J10): `{"attendees": ["Anna", "Erik"]}` is the field `attendees:: Anna; Erik` or a node `attendees` with two lines under it. It is read, in turn: 1. as its marker says (J22) 2. as fields when J11 wouldn't read its array as a node's lines and the array holds no `null` and no array that isn't empty, so that only fields need no index fields: `{"scores": [1, 2]}` is fields 3. as fields when its key looks like a key and none of the strings in its array is prose (J23), and otherwise as a node. So `{"attendees": ["Anna Smith", "Erik"]}` is fields, and `{"Planning": ["Milk", "Eggs"]}` and `{"notes": ["Ask about the budget.", "Check"]}` are nodes. Elsewhere, a node object is a node (J11) or fields (J15). - **J12.** Text is quoted according to C26: keys, labels and items. - **J28.** An object with the same key twice, which JSON allows and many JSON readers reduce to the last, keeps both, as a field line each, in order, and gives a warning: `{"a": 1, "a": 2}` gives `a:: 1` and `a:: 2`, which write back as `"a": [1, 2]` (J7a). ```example from-json J8 J9 J10 { "client": "Acme Corp", "hours": 6, "rate": 9.50, "billable": true, "code": "6", "flag": "true", "big": 1e21, "none": null, "tags": ["design", "review"], "one": ["x"], "address": { "street": "Main 1", "city": "Lund" } } . root field "client" "Acme Corp" field "hours" "6" field "rate" "9.50" field "billable" "true" field "code" quoted "6" field "flag" quoted "true" field "big" "1e21" field "none" field "tags" "design" "review" field "one" list "x" field "address" node field "street" "Main 1" field "city" "Lund" ``` ```example from-json J8 J11 J15 J21 [ { "client": "Acme" }, { "Kickoff": [{ "date": "2026-12-01", "room": "B2" }, "Book room", "Send agenda"] }, { "Planning": ["Book room", { "Workshop": [{ "hours": 6 }] }] }, "Follow up" ] . root field "client" "Acme" node "Kickoff" field "date" "2026-12-01" field "room" "B2" node "Book room" node "Send agenda" node "Planning" node "Book room" node "Workshop" field "hours" "6" node "Follow up" ``` ```example from-json J11 J22 J23 J26 [ { "Kickoff": [{ "attendees": ["Anna Smith", "Erik"] }, "Book room"] }, { "Acme": [{ "Planning": ["Milk", "Eggs"] }] }, { "Notes": [{ "todo": ["Book room", "Send agenda"], "_ord": "node" }] }, { "Q4": [{ "scores": [1, 2] }] }, { "Retro": [{ "tags": ["Went well.", "x"] }] }, { "Plan": [{ "Steps": ["a"], "_ord": "fields" }] } ] . root node "Kickoff" field "attendees" "Anna Smith" "Erik" node "Book room" node "Acme" node "Planning" node "Milk" node "Eggs" node "Notes" node "todo" node "Book room" node "Send agenda" node "Q4" field "scores" "1" "2" node "Retro" node "tags" node "Went well." node "x" node "Plan" field "Steps" list "a" ``` ```example from-json J4 J11 J26 [{ "Planning": [] }, "", {}, [], "{}", "6"] . root field "Planning" "[]" node quoted "" node "{}" node "[]" node quoted "{}" node "6" ``` ```example from-json J7a J10 { "lines": [ { "name": "Workshop", "hours": 6 }, { "name": "Spec review", "hours": 9.5 } ], "rows": ["x", { "a": 1 }, { "a": 3 }], "single": [{ "a": 1 }] } . root field "lines" node field "name" "Workshop" field "hours" "6" field "lines" node field "name" "Spec review" field "hours" "9.5" field "rows" "x" node field "a" "1" field "rows" node field "a" "3" field "single" list node field "a" "1" ``` ```example from-json J10 J23 { "tags": ["design", "review"], "agenda": ["Welcome and introductions", "Budget"], "notes": ["Ask about the budget.", "Check the room", 5], "note": ["Ask about the budget."] } . root field "tags" "design" "review" field "agenda" node "Welcome and introductions" node "Budget" field "notes" node "Ask about the budget." node "Check the room" field "notes" "5" field "note" list node "Ask about the budget." ``` ```example from-json J10 J20 { "matrix": [["a", "b"], ["c"]], "mixed": ["x", [], "y"], "e": [null, "y"] } . root field "matrix" node field "0" "a" "b" field "1" list "c" field "mixed" "x" "[]" "y" field "e" node field "0" field "1" "y" ``` ```example from-json J11 J15 J20 [ { "name": "Workshop", "hours": 6 }, { "name": "Spec review" } ] . root field "0" node field "name" "Workshop" field "hours" "6" field "1" node field "name" "Spec review" ``` ```example from-json J11 J20 ["Call Anna", { "Acme": [{ "a": 1 }, { "a": 2 }] }] . root node "Call Anna" node "Acme" field "0" node field "a" "1" field "1" node field "a" "2" ``` ```example from-json J11 J15 J20 [1, true, null] . root field "0" "1" field "1" "true" field "2" ``` ```example from-json J15 J20 [{ "a": 1 }] . root field "0" node field "a" "1" ``` ```example from-json J15 J19 5 . root field "_json" "5" ``` ```example from-json J15 J19 "6" . root field "_json" quoted "6" ``` ```example from-json J15 J19 null . root field "_json" ``` ```example from-json J15 J19 [] . root field "_json" "[]" ``` ```example from-json J1a J28 {"_id": "acme", "a": 1, "a": 2} . root field "_id" "acme" field "a" "1" field "a" "2" ! J28 1:25 ``` ## 5. Shapes compared How each common Ord shape looks in JSON, as a summary of the rules above: | Ord | JSON | |---|---| | fields on the root | `{"client": "Acme", "hours": 6}` | | lines on the root | `["Call Anna", "Book room"]` | | a node with fields | `{"Workshop": [{"hours": 6}]}` | | a node with lines | `{"Planning": ["Book room", "Send agenda"]}` | | a node with fields and lines | `{"Kickoff": [{"date": "2026-12-01"}, "Book room"]}` | | fields and lines on the root | `[{"client": "Acme"}, "Follow up"]` | | `tags:: a; b`, or `a` and `b` as lines under `tags::` | `"tags": ["a", "b"]` | | `address::` with `city:: Lund` under it | `"address": {"city": "Lund"}` | | a field line per record | `"rows": [{"a": 1}, {"a": 3}]` | | index fields `0::`, `1::` | `["a", {"b": 2}]` | | `_json:: 5` | `5` | ## 6. Known gaps JSON read as Ord and written back is the same JSON value, every number as written. What Ord → JSON → Ord loses is Ord's layout, which JSON has no place for: - The order between a node's fields and its child nodes. They come back fields first. - How a field's items were written, inline or as lines (J10 chooses), and in how many field lines with the same key (J7a). - Index fields whose array J11 reads as a node's lines come back as lines: `0:: a` and `1:: b` give `["a", "b"]`, which is the lines `a` and `b`. - A quoted label comes back unquoted, unless it must be quoted: the line `"6"` gives `"6"`, which is the line `6`, and `::` comes back as `""`. `TRUE` comes back `true`. - An Ord field `_ord` that would read as a marker comes back as one (J25). - An unquoted `[]` or `{}`, as a label or an item, is an empty array or object (J4, J7), even where it was written as text. - Keys that look like index keys come back quoted (J20). ## 7. Diagnostics Codes and severities work as in the Spec (C35 to C37). Positions are in the input: the JSON text when reading JSON, the Ord file when writing JSON. Lines and columns are counted as in the Spec (D9), and a JSON key's position is its opening `"`. | Code | Severity | Given when | Position | |---|---|---|---| | `J19.1` | warning | A field `_json` isn't the root's only line | the field's key | | `J25` | warning | An Ord field `_ord` would read back as a marker (J22) | the field's key | | `J28` | warning | A JSON object has the same key twice | the later key |