Skip to content

Inspecting kubectl JSON Output With fx ​

kubectl -o jsonpath gets hard to read fast. Pipe kubectl ... -o json into fx and write plain JavaScript instead, or browse the output interactively. Install fx first.

Browse kubectl JSON Output Interactively ​

Pipe any -o json output into fx with no arguments to open the interactive viewer:

bash
kubectl get pods -o json | fx

Use arrow keys or hjkl to move, @ to fuzzy-search paths (try containerStatuses), / to search values with a regex, and P to print the selected value to stdout. See key bindings for the full list. To pretty-print without opening the viewer, run fx . instead.

The examples below use pods.json, a saved kubectl get pods -n shop -o json with five pods. Every command works the same if you replace fx pods.json with kubectl get pods -o json | fx.

List Pod Names and Statuses ​

Pods live in .items. Use @ to map over them:

bash
fx pods.json '.items' '@.metadata.name'
json
[
  "api-7d9f8b6c5-x2kqp",
  "api-7d9f8b6c5-m8vzt",
  "worker-5c6b7d8f9-q4wln",
  "migrate-db-28491530-7hz2k",
  "redis-0"
]

Add list to print plain lines for xargs or while read loops. To show the phase next to each name, build a tab-separated string per pod and align the columns with column -t:

bash
fx pods.json '.items' '@(x.metadata.name + "\t" + x.status.phase)' list | column -t
api-7d9f8b6c5-x2kqp        Running
api-7d9f8b6c5-m8vzt        Running
worker-5c6b7d8f9-q4wln     Running
migrate-db-28491530-7hz2k  Pending
redis-0                    Failed

Find Pods That Are Not Running ​

Filter with ? before mapping:

bash
fx pods.json '.items' '?.status.phase !== "Running"' '@{name: x.metadata.name, phase: x.status.phase, reason: x.status.reason}'
json
[
  {
    "name": "migrate-db-28491530-7hz2k",
    "phase": "Pending",
    "reason": null
  },
  {
    "name": "redis-0",
    "phase": "Failed",
    "reason": "Evicted"
  }
]

Find Containers With Restarts ​

A pod in CrashLoopBackOff still reports phase Running, so check restartCount on each container. Flatten pods into one row per container, keep the ones that restarted, and sort by restart count:

bash
fx pods.json '.items' '.flatMap(p => (p.status.containerStatuses || []).map(c => ({pod: p.metadata.name, container: c.name, restarts: c.restartCount})))' '?.restarts > 0' 'sortBy(x => -x.restarts)'
json
[
  {
    "pod": "worker-5c6b7d8f9-q4wln",
    "container": "worker",
    "restarts": 12
  },
  {
    "pod": "api-7d9f8b6c5-m8vzt",
    "container": "api",
    "restarts": 3
  }
]

The || [] handles Pending pods, which have no containerStatuses yet.

List All Container Images in a Namespace ​

.[] flattens nested arrays, so .items[].spec.containers[].image collects every image across all pods. Remove duplicates with uniq:

bash
fx pods.json '.items[].spec.containers[].image' uniq sort list
envoyproxy/envoy:v1.29.1
ghcr.io/acme/api:1.4.2
ghcr.io/acme/worker:2.0.0
redis:7.2-alpine

Show Resource Requests and Limits as a Table ​

Build one row per container, join the cells with tabs, and align them with column -t:

bash
fx pods.json 'x => [
  ["POD", "CONTAINER", "CPU_REQ", "CPU_LIM", "MEM_REQ", "MEM_LIM"],
  ...x.items.flatMap(p => p.spec.containers.map(c => {
    const req = c.resources.requests ?? {}, lim = c.resources.limits ?? {}
    return [p.metadata.name, c.name, req.cpu, lim.cpu, req.memory, lim.memory]
  }))
].map(row => row.map(v => v ?? "-").join("\t"))' list | column -t
POD                        CONTAINER  CPU_REQ  CPU_LIM  MEM_REQ  MEM_LIM
api-7d9f8b6c5-x2kqp        api        250m     500m     256Mi    512Mi
api-7d9f8b6c5-x2kqp        envoy      100m     -        64Mi     128Mi
api-7d9f8b6c5-m8vzt        api        250m     500m     256Mi    512Mi
api-7d9f8b6c5-m8vzt        envoy      100m     -        64Mi     128Mi
worker-5c6b7d8f9-q4wln     worker     500m     1        1Gi      2Gi
migrate-db-28491530-7hz2k  migrate    -        -        -        -
redis-0                    redis      100m     -        128Mi    256Mi

A - means the value is not set. The migrate container has no requests or limits at all.

Decode a Kubernetes Secret ​

Secret values in .data are base64-encoded. Decode one key with fromBase64:

bash
kubectl get secret db-credentials -o json | fx '.data.DB_PASSWORD' fromBase64
s3cr3t-p4ssw0rd

Decode every key at once:

bash
kubectl get secret db-credentials -o json | fx '.data' 'x => Object.fromEntries(Object.entries(x).map(([k, v]) => [k, fromBase64(v)]))'
json
{
  "DB_HOST": "postgres.shop.svc",
  "DB_USER": "shop",
  "DB_PASSWORD": "s3cr3t-p4ssw0rd"
}

WARNING

Decoded values are printed in plain text and stay in your terminal scrollback.

Inspect Node Labels and Allocatable Capacity ​

Label keys contain dots and slashes, so use bracket notation:

bash
kubectl get nodes -o json | fx '.items' '@{name: x.metadata.name, zone: x.metadata.labels["topology.kubernetes.io/zone"], arch: x.metadata.labels["kubernetes.io/arch"]}'
json
[
  {
    "name": "node-a",
    "zone": "eu-west-1a",
    "arch": "amd64"
  },
  {
    "name": "node-b",
    "zone": "eu-west-1b",
    "arch": "arm64"
  }
]

Spread status.allocatable to see what the scheduler can hand out on each node:

bash
kubectl get nodes -o json | fx '.items' '@{name: x.metadata.name, ...x.status.allocatable}'
json
[
  {
    "name": "node-a",
    "cpu": "1930m",
    "memory": "7246512Ki",
    "pods": "29"
  },
  {
    "name": "node-b",
    "cpu": "3920m",
    "memory": "15354172Ki",
    "pods": "58"
  }
]

Use kubectl -o yaml Output With fx ​

fx reads YAML from stdin with --yaml, and the same expressions work. Finish with YAML.stringify to keep the result as YAML:

bash
kubectl get pods -o yaml | fx --yaml '.items' '?.status.phase === "Failed"' YAML.stringify
yaml
- apiVersion: v1
  kind: Pod
  metadata:
    name: redis-0
    namespace: shop
  status:
    phase: Failed

See Also ​