هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
curl -s https://api.example.com/v1/orders/42 | jq .
jq -r '.items[].name' orders.json
kubectl get pods -A -o json | jq -r '.items[] | select(.status.phase != "Running") | .metadata.namespace + "/" + .metadata.name'
aws ec2 describe-instances | jq -r '.Reservations[].Instances[] | [.InstanceId, .State.Name, .PrivateIpAddress] | @tsv'. = the whole input (pretty), -r = raw text (no quotes), | = pass the result on.
Step by step
- Pretty print and walk in:
- Lists:
.items[]gives every element; then pick fields:
jq -r '.items[] | .sku + " x" + (.qty|tostring)' order.json
A-100 x2- **Filter with
select:**
jq '.items[] | select(.qty > 1)' order.json
jq -r '.[] | select(.status == "error") | .id' events.json- Count, sort, unique:
jq '.items | length' order.json
jq -r '[.[] | .status] | group_by(.) | map({status: .[0], n: length})' events.json
jq -r '.[].country' visitors.json | sort | uniq -c | sort -rn- Tables for people and spreadsheets —
@tsv/@csvfrom arrays:
jq -r '.items[] | [.sku, .qty, .price] | @csv' order.json > items.csv- Use shell variables safely —
--arg(never paste variables into the filter):
ns=shop
kubectl get pods -A -o json | jq -r --arg ns "$ns" '.items[] | select(.metadata.namespace == $ns) | .metadata.name'- Change JSON (write to a new file, then move):
jq '.version = "1.4.3" | .features.newCheckout = true' config.json > config.json.new && mv config.json.new config.json- Missing fields —
//gives a default;?ignores errors:
jq -r '.items[] | .discount // 0' order.json- YAML? Use
yqwith the same style (yq '.spec.replicas' deploy.yaml).
Check that it worked
The command prints exactly the values you need, and jq empty FILE returns no error for a valid JSON file (good in CI):
Common problems
| Problem | Cause | Fix |
|---|---|---|
Cannot index array with "name" | You used .name on a list. | .[] first: .items[].name. |
parse error: Invalid numeric literal | The input is not JSON (an HTML error page, text). | Look at the raw input; check the HTTP status. |
| Values have quotes | No raw output. | Add -r. |
jq: error … string ("2") and number (1) cannot be added | Mixing strings and numbers. | tostring or tonumber. |
null everywhere | Wrong path or different case of keys. | jq 'keys' at each level. |
| A shell variable breaks the filter | Variable pasted into single quotes. | --arg name "$value" and $name inside. |
Go further
- Command pages: jq, yq, curl, kubectl, aws.
- Next how-tos: Test an HTTP endpoint with curl, Write a safe Bash script, Plan and apply Terraform safely.
- Learn it: jq in daily DevOps, yq.
- Practise: Lab: turn a CSV export into JSON and YAML reports.
1What does -r change in jq's output?
Strings are printed raw, without quotes — good for scripts.
2How do you use a shell variable inside a jq filter safely?
Pass it with --arg NAME "$value" and use $NAME in the filter.