JSON Objects, Arrays, and Their Roles

JSON arrays store ordered values. JSON objects associate string names with member values under the format's object rules.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Read one nested value
  2. Require unique names in practice
  3. Choose container shape according to what order means
  4. Test an empty collection as well as a reordered one

Read one nested value

A settings object might have a names member whose value is the array ["Ada","Bo"]. Swapping array elements changes their positions. Changing object member order does not normally change the association. Sets and sequences describe why positional order matters in one part.

Require unique names in practice

JSON's interoperability guidance recommends unique object member names. Duplicate names can be handled differently by readers. Design a record contract that states how to handle input violating that expectation.[1]

Choose container shape according to what order means

A recipe can describe its steps with an array where mixing happens before baking. An object keyed by step name gives named access. Consumers should not rely on object ordering for sequence semantics across implementations. See dictionary insertion order for a local language behavior distinct from an exchange contract.

Ordered steps: ["mix","bake"]
Named facts: {"mix_minutes":3,"bake_minutes":20}
Read by position for steps and by member name for facts

Test an empty collection as well as a reordered one

Empty object and empty array represent different JSON structures. A parser should not accept one for the other merely because both contain no entries. For object members, state how duplicate names are handled or reject them before interpreting records.

References

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