Jmespath 的使用

  1. {
  2. "data": [
  3. {
  4. "last_reply_at": "2021-10-07T05:40:14.376Z",
  5. "visit_count": 1,
  6. "tab": "ask",
  7. "top": false,
  8. "author": {
  9. "avatar_url": "\/public\/images\/userlogo.png",
  10. "loginname": "zhangdandan"
  11. },
  12. "id": "615e883df7759804f43dd3df",
  13. "author_id": "6153d8beabc8d9ccc9ca36c9",
  14. "title": "重阳节到了",
  15. "reply_count": 1,
  16. "create_at": "2021-10-07T05:40:13.558Z",
  17. "good": false,
  18. "content": "<div class=\"markdown-text\"><p>是不是又可以放假了,哈哈哈哈哈哈哈哈哈哈哈哈<\/p>\n<\/div>"
  19. },
  20. {
  21. "last_reply_at": "2021-10-07T05:40:09.496Z",
  22. "visit_count": 1,
  23. "tab": "ask",
  24. "top": false,
  25. "author": {
  26. "avatar_url": "\/public\/images\/userlogo.png",
  27. "loginname": "zhangdandan"
  28. },
  29. "id": "615e8839f7759804f43dd3db",
  30. "author_id": "6153d8beabc8d9ccc9ca36c9",
  31. "title": "重阳节到了",
  32. "reply_count": 1,
  33. "create_at": "2021-10-07T05:40:09.380Z",
  34. "good": false,
  35. "content": "<div class=\"markdown-text\"><p>是不是又可以放假了,哈哈哈哈哈哈哈哈哈哈哈哈<\/p>\n<\/div>"
  36. },
  37. {
  38. "last_reply_at": "2021-10-07T05:40:01.509Z",
  39. "visit_count": 1,
  40. "tab": "ask",
  41. "top": false,
  42. "author": {
  43. "avatar_url": "\/public\/images\/userlogo.png",
  44. "loginname": "a592518"
  45. },
  46. "id": "615e8831f7759804f43dd3d7",
  47. "author_id": "6153d783abc8d9ccc9ca368e",
  48. "title": "fgdgdgdfgfdg",
  49. "reply_count": 1,
  50. "create_at": "2021-10-07T05:40:01.396Z",
  51. "good": false,
  52. "content": "<div class=\"markdown-text\"><p>表示的是从服务器中返回的 对应 topic_id 字段的值。<\/p>\n<\/div>"
  53. },
  54. {
  55. "last_reply_at": "2021-10-07T05:39:48.512Z",
  56. "visit_count": 1,
  57. "tab": "ask",
  58. "top": false,
  59. "author": {
  60. "avatar_url": "\/public\/images\/userlogo.png",
  61. "loginname": "test2"
  62. },
  63. "id": "615e8824f7759804f43dd3d3",
  64. "author_id": "60e04cade14a422006d945f5",
  65. "title": "这么近,那么远",
  66. "reply_count": 1,
  67. "create_at": "2021-10-07T05:39:48.398Z",
  68. "good": false,
  69. "content": "<div class=\"markdown-text\"><p>啦啦啦啦啦啦<\/p>\n<\/div>"
  70. },
  71. {
  72. "last_reply_at": "2021-10-07T05:39:48.380Z",
  73. "visit_count": 0,
  74. "tab": "ask",
  75. "top": false,
  76. "author": {
  77. "avatar_url": "\/public\/images\/userlogo.png",
  78. "loginname": "test2"
  79. },
  80. "id": "615e8824f7759804f43dd3d2",
  81. "author_id": "60e04cade14a422006d945f5",
  82. "title": "666888999",
  83. "reply_count": 0,
  84. "create_at": "2021-10-07T05:39:48.380Z",
  85. "good": false,
  86. "content": "<div class=\"markdown-text\"><p>0123456789<\/p>\n<\/div>"
  87. },
  88. {
  89. "last_reply_at": "2021-10-07T05:39:40.624Z",
  90. "visit_count": 1,
  91. "tab": "ask",
  92. "top": false,
  93. "author": {
  94. "avatar_url": "\/public\/images\/userlogo.png",
  95. "loginname": "test2"
  96. },
  97. "id": "615e881cf7759804f43dd3ce",
  98. "author_id": "60e04cade14a422006d945f5",
  99. "title": "这么近,那么远",
  100. "reply_count": 1,
  101. "create_at": "2021-10-07T05:39:40.408Z",
  102. "good": false,
  103. "content": "<div class=\"markdown-text\"><p>啦啦啦啦啦啦<\/p>\n<\/div>"
  104. },
  105. {
  106. "last_reply_at": "2021-10-07T05:39:40.392Z",
  107. "visit_count": 0,
  108. "tab": "ask",
  109. "top": false,
  110. "author": {
  111. "avatar_url": "\/public\/images\/userlogo.png",
  112. "loginname": "test2"
  113. },
  114. "id": "615e881cf7759804f43dd3cd",
  115. "author_id": "60e04cade14a422006d945f5",
  116. "title": "666888999",
  117. "reply_count": 0,
  118. "create_at": "2021-10-07T05:39:40.392Z",
  119. "good": false,
  120. "content": "<div class=\"markdown-text\"><p>0123456789<\/p>\n<\/div>"
  121. },
  122. {
  123. "last_reply_at": "2021-10-07T05:39:26.420Z",
  124. "visit_count": 1,
  125. "tab": "ask",
  126. "top": false,
  127. "author": {
  128. "avatar_url": "\/public\/images\/userlogo.png",
  129. "loginname": "zhanglanling"
  130. },
  131. "id": "615e583ff7759804f43dc46a",
  132. "author_id": "61540250f7759804f43d4974",
  133. "title": "321321321",
  134. "reply_count": 16,
  135. "create_at": "2021-10-07T02:15:27.389Z",
  136. "good": false,
  137. "content": "<div class=\"markdown-text\"><p>098765432100<\/p>\n<\/div>"
  138. },
  139. {
  140. "last_reply_at": "2021-10-07T05:39:25.369Z",
  141. "visit_count": 1,
  142. "tab": "ask",
  143. "top": false,
  144. "author": {
  145. "avatar_url": "\/public\/images\/userlogo.png",
  146. "loginname": "abtester"
  147. },
  148. "id": "615e880df7759804f43dd3c9",
  149. "author_id": "6153dd8df7759804f43d3fd2",
  150. "title": "好家伙多过所晕过多所多付付付付",
  151. "reply_count": 0,
  152. "create_at": "2021-10-07T05:39:25.369Z",
  153. "good": false,
  154. "content": "<div class=\"markdown-text\"><p>广告设计所军军军军军军<\/p>\n<\/div>"
  155. },
  156. {
  157. "last_reply_at": "2021-10-07T05:39:24.698Z",
  158. "visit_count": 0,
  159. "tab": "ask",
  160. "top": false,
  161. "author": {
  162. "avatar_url": "\/public\/images\/userlogo.png",
  163. "loginname": "guiying"
  164. },
  165. "id": "615e880cf7759804f43dd3c8",
  166. "author_id": "6153d726abc8d9ccc9ca3687",
  167. "title": "我又来酷酷酷酷酷酷酷酷酷酷",
  168. "reply_count": 0,
  169. "create_at": "2021-10-07T05:39:24.698Z",
  170. "good": false,
  171. "content": "<div class=\"markdown-text\"><p>手术室的回归方法范冰冰和经济<\/p>\n<\/div>"
  172. }
  173. ],
  174. "success": true
  175. }

上面是服务器返回的结果。在这个结果提取变量。

比如要提取 reply_count值大于 5 的所有话题的id。
使用原来的提取方式已经不再适用,因为返回的结果中数据位置已经不再是确定的,有可能第一条数据符合上述要求,也有可能第二条数据符合要求。

符号 说明
== 等于
> 大于
>= 大于等于
< 小于
<= 小于等于
!= 不等于

这就需要我们使用灵活的提取方式了。

  1. 查找结果中回复数大于5的话题id。
    1. data[?reply_count > `5`].id
  • ? 表示查询条件
  • 表示大于

  • 5 表示数字5 这里需要注意⚠️: 当条件中有数字或者bool类型的值(true,false) 需要添加 ``
  1. 查询 top值为 false 的话题id

    1. data[?top==`false`].id
  2. 查询tab值为 ask 的话题id

    1. data[?tab=='ask'].id
  • 'ask' 因为ask的值在 json中是字符串 "ask"

总结

jmespath 适用的场景:

  1. 当数据有很多条,需要根据条件来进行过滤的时候进行查找。
  2. 也可以直接查找。

可以官网上的例子 进行高效 学习。 https://jmespath.org/tutorial.html
image.png