# jq keys_unsorted is not an ordering contract

Use keys_unsorted for convenient inspection; use an array when the order is part of the data.

- URL: https://www.vedant.me/notebook/jq-keys-unsorted-is-not-an-ordering-contract/
- Published: 2026-09-10
- Updated: 2026-09-10
- Tags: jq, CLI
- Author: Vedant Andhale

`jq` has two convenient ways to inspect an object's keys:

```sh
printf '%s\n' '{"b":2,"a":1}' | jq 'keys'
# ["a", "b"]

printf '%s\n' '{"b":2,"a":1}' | jq 'keys_unsorted'
# Typically ["b", "a"]
```

`keys` sorts the result. The jq manual describes `keys_unsorted` as returning keys roughly in insertion order. That makes it useful when inspecting a document, but it does not guarantee that every transformation will preserve an intended order.

If order matters to the application, put it in an array:

```json
[
  {"name": "b", "value": 2},
  {"name": "a", "value": 1}
]
```

Now `jq '.[].name'` follows an explicit sequence. A consumer does not have to infer a display order from the way an object happened to be written or reconstructed.

I would use `keys_unsorted` for a quick look at familiar input. For a report, API contract or reproducible export, I would define the ordering directly or sort by an explicit field.

Reference: [jq manual: keys and keys_unsorted](https://jqlang.org/manual/).
