liveBook · Manning

Attribute

As explained in The Design of Web APIs, Second Edition

A property is a named data element belonging to a JSON object that describes one of its values or characteristics.

An attribute, or property, is a named piece of data belonging to an object. In JSON, its quoted name is followed by a colon and a value. In an API, properties describe a resource without necessarily being resources themselves: a product has a name, but that name cannot exist independently. A price might instead warrant its own resource if consumers need to track its history. section 5.1.2 section 3.3.1

Describing a product

A product can have an integer productReference, a string name, a numeric price, a Boolean isProductAvailable, an array of keywords, and a supplier object. The supplier can in turn have supplierCode and name properties. Include related data when consumers need it: a supplier name may be useful alongside its code, while the code can identify the related supplier resource. section 5.2.3 section 5.2.2

To model properties without getting stuck on naming debates:

  • List the data consumers need, including an identifier. Check ideas with subject matter experts; treat an existing UI or database as input, not an unquestioned specification. section 5.2.2
  • Group related elements into objects or arrays. Then choose portable types, consistent names, and which properties are required; remove elements that do not make sense. section 5.2.5 section 5.2.3
  • Add a description when name and type are insufficient—for example, to state that a price uses US dollars or a date string uses YYYY-MM-DD. section 5.2.3
Listing 7.6 An inner object
...
Product:
  type: object          #1
  properties:
    ...
    supplier:
      type: object     #1
      properties:           #2
        supplierCode:
          type: string
          description: Unique identifier
        name:
          type: string

In an OpenAPI JSON Schema, properties maps names to schemas. The supplier property has type object and its own properties map for supplierCode and name. This example does not yet mark either nested property as required. section 7.4.4

Keep essential subject matter in the resource data even if metadata also conveys it: a transfer’s cancellable flag tells consumers something important about the transfer. As a check on a schema, try specifying a rule that every 201 response defines a required string or integer id property. section 11.4.5 section 18.15

How The Design of Web APIs, Second Edition covers it

How deep it goes
Works through selection, names, types, and nested schemas; defers marking properties required in the nested-object schema example.
Examples
Uses product fields, a nested supplier schema, a transfer’s cancellable flag, and a 201-response id-rule exercise.
What it assumes
Introduces JSON property syntax and distinguishes resource properties from independently manipulable resources.
What you can do after
Build it from scratch by selecting consumer-relevant properties, then describing their names, types, and nested structure in JSON Schema.
How it connects
Connects properties to resources, resource identifiers, JSON objects, JSON Schema, and related-resource data.

Also explained in

Compare how 28 books explain attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Design of Web APIs, Second Edition. The section links point to the parts of the book it draws on.