Step: Inspect: keys, length, has

Inspect: keys, length, has

jq ships built-ins that describe a value:

  • keys — sorted array of an object's keys (or array indices)
  • length — size of an array/object/string, or absolute value of a number
  • has("k") — whether a key/index exists
{"b":1,"a":2} | keys      # => ["a","b"]
[10,20,30]    | length    # => 3

Your turn: list the top-level keys of the input (they come back sorted).

Input:
{
  "firstName": "Charles",
  "lastName": "Doe",
  "age": 41,
  "location": {
    "city": "San Francisco",
    "postal-code": "94103"
  },
  "hobbies": [
    "chess",
    "netflix"
  ],
  "pets": [
    {
      "name": "Bear",
      "kind": "dog"
    },
    {
      "name": "Jerry",
      "kind": "cat"
    }
  ]
}
Expected:
[
  "age",
  "firstName",
  "hobbies",
  "lastName",
  "location",
  "pets"
]
Step 16 of 27