JSONPath
Part of the Introduction to Kubernetes section of Coddy's Terminal journey. Lesson 36 of 45.
Often you want one value, not the whole object. -o jsonpath picks fields out of the object with a path expression, written between braces:
kubectl get pod web -o jsonpath='{.status.podIP}'This prints just the Pod's IP. The path follows the YAML structure: status, then podIP.
Lists are indexed with square brackets. The first container's image is:
kubectl get pod web -o jsonpath='{.spec.containers[0].image}'For a list of objects, .items[*] selects every item, so kubectl get pods -o jsonpath='{.items[*].metadata.name}' prints all the Pod names on one line.
jsonpath prints exactly what you ask for, without a newline at the end. Add {"\n"} inside the braces when you want one.
Challenge
EasyStart a Pod named web from the nginx image, then use jsonpath to print its IP address, and jsonpath again to print the image of its first container.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction to Kubernetes
Practice on your own: Terminal playground