1. jq-test|⇒ cat test.json| jq '.["foo"]'
  2. 42
  3. jq-test|⇒ cat test.json
  4. {"foo": 42, "bar": "less interesting data"}
  5. jq-test|⇒ cat test.json| jq .foo
  6. 42
  7. jq-test|⇒ cat test.json| jq .no
  8. null
  9. # 有特殊字符时的查看方法
  10. jq-test|⇒ cat test.json| jq '.["foo"]'
  11. 42
  12. jq-test|⇒ cat test.json
  13. {"foo": 42, "bar": "less interesting data","42":12}
  14. jq-test|⇒ cat test.json| jq '.["42"]'
  15. 12
  16. jq-test|⇒ cat test.json| jq '."42"'
  17. 12
  18. jq-test|⇒ cat test.json| jq '.foo?'
  19. 42
  20. jq-test|⇒ cat test.json| jq '.foo7?'
  21. null
  22. jq-test|⇒ cat test.json| jq '[.foo?]'
  23. [
  24. 42
  25. ]

If the key contains special characters or starts with a digit, you need to surround it with double quotes like this: ."foo$", or else .["foo$"].

数组

  1. jq-test|⇒ cat 2.json
  2. [{"name":"JSON", "good":true}, {"name":"XML", "good":false}]
  3. jq-test|⇒ cat 2.json | jq .
  4. [
  5. {
  6. "name": "JSON",
  7. "good": true
  8. },
  9. {
  10. "name": "XML",
  11. "good": false
  12. }
  13. ]
  14. jq-test|⇒ cat 2.json | jq '.[]'
  15. {
  16. "name": "JSON",
  17. "good": true
  18. }
  19. {
  20. "name": "XML",
  21. "good": false
  22. }
  23. jq-test|⇒ cat 2.json | jq '.[0]'
  24. {
  25. "name": "JSON",
  26. "good": true
  27. }
  28. jq-test|⇒ cat 2.json | jq '.[2]'
  29. null
  30. # 倒数第一个
  31. jq-test|⇒ cat 2.json | jq '.[-1]'
  32. {
  33. "name": "XML",
  34. "good": false
  35. }
  36. jq-test|⇒ cat 2.json | jq '.[-2]'
  37. {
  38. "name": "JSON",
  39. "good": true
  40. }
  41. 切片:
  42. jq-test|⇒ echo '["a","b","c","d","e"]' | jq '.[2:4]'
  43. [
  44. "c",
  45. "d"
  46. ]
  47. jq-test|⇒ echo '["a","b","c","d","e"]' | jq '.[:4]'
  48. [
  49. "a",
  50. "b",
  51. "c",
  52. "d"
  53. ]
  54. jq-test|⇒ echo '["a","b","c","d","e"]' | jq '.[-2:]'
  55. [
  56. "d",
  57. "e"
  58. ]
  59. jq-test|⇒ echo '"abcdefghi"' | jq '.[2:4]'
  60. "cd"
  61. # 返回所有的value
  62. jq-test|⇒ cat test.json
  63. {"foo": 42, "bar": "less interesting data","42":12}
  64. jq-test|⇒ cat test.json | jq '.[]'
  65. 42
  66. "less interesting data"
  67. 12