متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 93Build · Shell

How to read JSON in the shell with jq

APIs, kubectl -o json, aws, az, docker inspect, Terraform plans — most tools speak JSON. jq turns a wall of JSON into exactly the values you need: one field, a filtered list, a table, or a changed file. This how-to shows the 10 jq patterns you use every day with real examples from cloud and Kubernetes work.

  • 5 الدروس
  • 9 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

93.1

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.

93.2

Step by step

  1. Pretty print and walk in:
jq . order.json
jq '.customer.email' order.json
jq '.items[0]' order.json
jq 'keys' order.json
  1. Lists: .items[] gives every element; then pick fields:
jq -r '.items[] | .sku + " x" + (.qty|tostring)' order.json
A-100 x2
  1. **Filter with select:**
jq '.items[] | select(.qty > 1)' order.json
jq -r '.[] | select(.status == "error") | .id' events.json
  1. 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
  1. Tables for people and spreadsheets — @tsv / @csv from arrays:
jq -r '.items[] | [.sku, .qty, .price] | @csv' order.json > items.csv
  1. 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'
  1. 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
  1. Missing fields — // gives a default; ? ignores errors:
jq -r '.items[] | .discount // 0' order.json
  1. YAML? Use yq with the same style (yq '.spec.replicas' deploy.yaml).

93.3

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):

jq empty config.json && echo "valid JSON"

الأوامر في هذا الدرس

93.4

Common problems

ProblemCauseFix
Cannot index array with "name"You used .name on a list..[] first: .items[].name.
parse error: Invalid numeric literalThe input is not JSON (an HTML error page, text).Look at the raw input; check the HTTP status.
Values have quotesNo raw output.Add -r.
jq: error … string ("2") and number (1) cannot be addedMixing strings and numbers.tostring or tonumber.
null everywhereWrong path or different case of keys.jq 'keys' at each level.
A shell variable breaks the filterVariable pasted into single quotes.--arg name "$value" and $name inside.

الأوامر في هذا الدرس

93.5

Go further

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.

كل الأوامر في هذا الفصل