Certainly! Here are some basic examples of how to use jq to process JSON data:
1. Basic JSON Output
If you have a JSON file (e.g., data.json), you can pretty-print it with:
jq . data.json
2. Extracting Specific Fields
To extract a specific field from a JSON object:
jq '.fieldName' data.json
3. Filtering Arrays
To filter an array of objects based on a condition:
jq '.items[] | select(.status == "active")' data.json
4. Combining Fields
To create a new object with selected fields:
jq '.items[] | {name: .name, id: .id}' data.json
5. Using Multiple Conditions
To filter based on multiple conditions:
jq '.items[] | select(.status == "active" and .type == "admin")' data.json
6. Counting Elements
To count the number of elements in an array:
jq '.items | length' data.json
7. Nested Objects
To access nested fields:
jq '.user.address.city' data.json
8. Working with Kubernetes Output
For example, to filter Kubernetes pod information:
kubectl get pods -o json | jq '.items[] | {name: .metadata.name, containers: .spec.containers[].name}'
These examples should give you a good starting point for using jq to manipulate and query JSON data. If you have specific JSON data you want to work with or need more examples, feel free to ask!
