@port-labs/jq-node-bindings 1.0.4 → 1.1.0-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (352) hide show
  1. package/.claude/settings.local.json +12 -0
  2. package/configure +6 -5
  3. package/deps/jq/.claude/settings.local.json +7 -0
  4. package/deps/jq/.gitattributes +1 -3
  5. package/deps/jq/AUTHORS +55 -1
  6. package/deps/jq/COPYING +40 -0
  7. package/deps/jq/Makefile.am +40 -36
  8. package/deps/jq/NEWS.md +277 -2
  9. package/deps/jq/README.md +23 -5
  10. package/deps/jq/compile-ios.sh +1 -1
  11. package/deps/jq/config/m4/check-math-func.m4 +2 -2
  12. package/deps/jq/config/m4/find-func-no-libs2.m4 +3 -3
  13. package/deps/jq/configure.ac +10 -18
  14. package/deps/jq/docs/Pipfile.lock +368 -313
  15. package/deps/jq/docs/README.md +3 -4
  16. package/deps/jq/docs/build_manpage.py +1 -1
  17. package/deps/jq/docs/build_mantests.py +1 -1
  18. package/deps/jq/docs/build_website.py +1 -8
  19. package/deps/jq/docs/content/download/default.yml +49 -9
  20. package/deps/jq/docs/content/index.yml +9 -0
  21. package/deps/jq/docs/content/manual/{manual.yml → dev/manual.yml} +218 -61
  22. package/deps/jq/docs/content/manual/v1.3/manual.yml +0 -5
  23. package/deps/jq/docs/content/manual/v1.4/manual.yml +0 -5
  24. package/deps/jq/docs/content/manual/v1.5/manual.yml +3 -8
  25. package/deps/jq/docs/content/manual/v1.6/manual.yml +3 -8
  26. package/deps/jq/docs/content/manual/v1.7/manual.yml +499 -500
  27. package/deps/jq/docs/content/manual/v1.8/manual.yml +3858 -0
  28. package/deps/jq/docs/content/tutorial/default.yml +3 -3
  29. package/deps/jq/docs/manual_schema.yml +0 -3
  30. package/deps/jq/docs/public/CNAME +1 -0
  31. package/deps/jq/docs/templates/index.html.j2 +10 -10
  32. package/deps/jq/docs/templates/manual.html.j2 +14 -3
  33. package/deps/jq/docs/templates/shared/_head.html.j2 +8 -3
  34. package/deps/jq/docs/templates/shared/_navbar.html.j2 +4 -4
  35. package/deps/jq/jq.1.prebuilt +219 -44
  36. package/deps/jq/jq.spec +2 -2
  37. package/deps/jq/libjq.pc.in +1 -1
  38. package/deps/jq/scripts/version +1 -1
  39. package/deps/jq/sig/v1.7.1/jq-1.7.1.tar.gz.asc +16 -0
  40. package/deps/jq/sig/v1.7.1/jq-1.7.1.zip.asc +16 -0
  41. package/deps/jq/sig/v1.7.1/jq-linux-amd64.asc +16 -0
  42. package/deps/jq/sig/v1.7.1/jq-linux-arm64.asc +16 -0
  43. package/deps/jq/sig/v1.7.1/jq-linux-armel.asc +16 -0
  44. package/deps/jq/sig/v1.7.1/jq-linux-armhf.asc +16 -0
  45. package/deps/jq/sig/v1.7.1/jq-linux-i386.asc +16 -0
  46. package/deps/jq/sig/v1.7.1/jq-linux-mips.asc +16 -0
  47. package/deps/jq/sig/v1.7.1/jq-linux-mips64.asc +16 -0
  48. package/deps/jq/sig/v1.7.1/jq-linux-mips64el.asc +16 -0
  49. package/deps/jq/sig/v1.7.1/jq-linux-mips64r6.asc +16 -0
  50. package/deps/jq/sig/v1.7.1/jq-linux-mips64r6el.asc +16 -0
  51. package/deps/jq/sig/v1.7.1/jq-linux-mipsel.asc +16 -0
  52. package/deps/jq/sig/v1.7.1/jq-linux-mipsr6.asc +16 -0
  53. package/deps/jq/sig/v1.7.1/jq-linux-mipsr6el.asc +16 -0
  54. package/deps/jq/sig/v1.7.1/jq-linux-powerpc.asc +16 -0
  55. package/deps/jq/sig/v1.7.1/jq-linux-ppc64el.asc +16 -0
  56. package/deps/jq/sig/v1.7.1/jq-linux-riscv64.asc +16 -0
  57. package/deps/jq/sig/v1.7.1/jq-linux-s390x.asc +16 -0
  58. package/deps/jq/sig/v1.7.1/jq-linux64.asc +16 -0
  59. package/deps/jq/sig/v1.7.1/jq-macos-amd64.asc +16 -0
  60. package/deps/jq/sig/v1.7.1/jq-macos-arm64.asc +16 -0
  61. package/deps/jq/sig/v1.7.1/jq-osx-amd64.asc +16 -0
  62. package/deps/jq/sig/v1.7.1/jq-win64.exe.asc +16 -0
  63. package/deps/jq/sig/v1.7.1/jq-windows-amd64.exe.asc +16 -0
  64. package/deps/jq/sig/v1.7.1/jq-windows-i386.exe.asc +16 -0
  65. package/deps/jq/sig/v1.7.1/sha256sum.txt +26 -0
  66. package/deps/jq/sig/v1.8.0/jq-1.8.0.tar.gz.asc +16 -0
  67. package/deps/jq/sig/v1.8.0/jq-1.8.0.zip.asc +16 -0
  68. package/deps/jq/sig/v1.8.0/jq-linux-amd64.asc +16 -0
  69. package/deps/jq/sig/v1.8.0/jq-linux-arm64.asc +16 -0
  70. package/deps/jq/sig/v1.8.0/jq-linux-armel.asc +16 -0
  71. package/deps/jq/sig/v1.8.0/jq-linux-armhf.asc +16 -0
  72. package/deps/jq/sig/v1.8.0/jq-linux-i386.asc +16 -0
  73. package/deps/jq/sig/v1.8.0/jq-linux-mips.asc +16 -0
  74. package/deps/jq/sig/v1.8.0/jq-linux-mips64.asc +16 -0
  75. package/deps/jq/sig/v1.8.0/jq-linux-mips64el.asc +16 -0
  76. package/deps/jq/sig/v1.8.0/jq-linux-mips64r6.asc +16 -0
  77. package/deps/jq/sig/v1.8.0/jq-linux-mips64r6el.asc +16 -0
  78. package/deps/jq/sig/v1.8.0/jq-linux-mipsel.asc +16 -0
  79. package/deps/jq/sig/v1.8.0/jq-linux-mipsr6.asc +16 -0
  80. package/deps/jq/sig/v1.8.0/jq-linux-mipsr6el.asc +16 -0
  81. package/deps/jq/sig/v1.8.0/jq-linux-powerpc.asc +16 -0
  82. package/deps/jq/sig/v1.8.0/jq-linux-ppc64el.asc +16 -0
  83. package/deps/jq/sig/v1.8.0/jq-linux-riscv64.asc +16 -0
  84. package/deps/jq/sig/v1.8.0/jq-linux-s390x.asc +16 -0
  85. package/deps/jq/sig/v1.8.0/jq-linux64.asc +16 -0
  86. package/deps/jq/sig/v1.8.0/jq-macos-amd64.asc +16 -0
  87. package/deps/jq/sig/v1.8.0/jq-macos-arm64.asc +16 -0
  88. package/deps/jq/sig/v1.8.0/jq-osx-amd64.asc +16 -0
  89. package/deps/jq/sig/v1.8.0/jq-win64.exe.asc +16 -0
  90. package/deps/jq/sig/v1.8.0/jq-windows-amd64.exe.asc +16 -0
  91. package/deps/jq/sig/v1.8.0/jq-windows-i386.exe.asc +16 -0
  92. package/deps/jq/sig/v1.8.0/sha256sum.txt +26 -0
  93. package/deps/jq/src/builtin.c +446 -271
  94. package/deps/jq/src/builtin.jq +25 -60
  95. package/deps/jq/src/bytecode.h +8 -3
  96. package/deps/jq/src/compile.c +22 -43
  97. package/deps/jq/src/compile.h +1 -1
  98. package/deps/jq/src/execute.c +20 -25
  99. package/deps/jq/src/jq_test.c +113 -44
  100. package/deps/jq/src/jv.c +134 -42
  101. package/deps/jq/src/jv.h +10 -3
  102. package/deps/jq/src/jv_alloc.c +8 -5
  103. package/deps/jq/src/jv_alloc.h +0 -1
  104. package/deps/jq/src/jv_aux.c +41 -8
  105. package/deps/jq/src/jv_dtoa.c +4 -12
  106. package/deps/jq/src/jv_dtoa_tsd.c +5 -4
  107. package/deps/jq/src/jv_dtoa_tsd.h +1 -1
  108. package/deps/jq/src/jv_parse.c +5 -3
  109. package/deps/jq/src/jv_print.c +87 -65
  110. package/deps/jq/src/jv_unicode.c +18 -0
  111. package/deps/jq/src/jv_unicode.h +2 -0
  112. package/deps/jq/src/lexer.c +303 -278
  113. package/deps/jq/src/lexer.h +5 -4
  114. package/deps/jq/src/lexer.l +8 -2
  115. package/deps/jq/src/libm.h +15 -5
  116. package/deps/jq/src/linker.c +6 -4
  117. package/deps/jq/src/locfile.c +12 -12
  118. package/deps/jq/src/main.c +204 -270
  119. package/deps/jq/src/parser.c +1105 -1273
  120. package/deps/jq/src/parser.y +149 -181
  121. package/deps/jq/src/util.c +46 -48
  122. package/deps/jq/src/util.h +1 -1
  123. package/deps/jq/tests/base64.test +12 -0
  124. package/deps/jq/tests/jq.test +429 -37
  125. package/deps/jq/tests/jq_fuzz_load_file.c +1 -0
  126. package/deps/jq/tests/man.test +46 -13
  127. package/deps/jq/tests/manonig.test +13 -0
  128. package/deps/jq/tests/onig.test +40 -2
  129. package/deps/jq/tests/optional.test +4 -12
  130. package/deps/jq/tests/setup +5 -1
  131. package/deps/jq/tests/shtest +358 -130
  132. package/deps/jq/tests/uri.test +38 -0
  133. package/deps/jq/tests/uritest +5 -0
  134. package/deps/jq/{src → vendor}/decNumber/decBasic.c +1 -1
  135. package/deps/jq/{src → vendor}/decNumber/decCommon.c +1 -1
  136. package/deps/jq/{src → vendor}/decNumber/decNumber.c +4 -4
  137. package/deps/jq/{modules → vendor}/oniguruma/CMakeLists.txt +5 -3
  138. package/deps/jq/{modules → vendor}/oniguruma/HISTORY +45 -7
  139. package/deps/jq/{modules → vendor}/oniguruma/Makefile.am +1 -1
  140. package/deps/jq/{modules → vendor}/oniguruma/README.md +10 -23
  141. package/deps/jq/{modules → vendor}/oniguruma/compile +29 -13
  142. package/deps/jq/{modules → vendor}/oniguruma/config.guess +91 -27
  143. package/deps/jq/{modules → vendor}/oniguruma/config.sub +716 -242
  144. package/deps/jq/{modules → vendor}/oniguruma/configure.ac +2 -2
  145. package/deps/jq/{modules → vendor}/oniguruma/depcomp +10 -9
  146. package/deps/jq/{modules → vendor}/oniguruma/doc/API +3 -1
  147. package/deps/jq/{modules → vendor}/oniguruma/doc/API.ja +3 -1
  148. package/deps/jq/{modules → vendor}/oniguruma/doc/CALLOUTS.BUILTIN +10 -1
  149. package/deps/jq/{modules → vendor}/oniguruma/doc/CALLOUTS.BUILTIN.ja +9 -1
  150. package/deps/jq/{modules → vendor}/oniguruma/doc/RE +12 -13
  151. package/deps/jq/{modules → vendor}/oniguruma/doc/RE.ja +12 -13
  152. package/deps/jq/{modules → vendor}/oniguruma/doc/SYNTAX.md +202 -167
  153. package/deps/jq/{modules → vendor}/oniguruma/doc/UNICODE_PROPERTIES +33 -1
  154. package/deps/jq/vendor/oniguruma/doc/onig_syn_md.c +667 -0
  155. package/deps/jq/{modules → vendor}/oniguruma/harnesses/base.c +1 -1
  156. package/deps/jq/{modules → vendor}/oniguruma/install-sh +9 -9
  157. package/deps/jq/{modules → vendor}/oniguruma/missing +50 -29
  158. package/deps/jq/{modules → vendor}/oniguruma/sample/callback_each_match.c +1 -3
  159. package/deps/jq/vendor/oniguruma/sbom.cdx.json +44 -0
  160. package/deps/jq/{modules → vendor}/oniguruma/src/ascii.c +4 -1
  161. package/deps/jq/{modules → vendor}/oniguruma/src/gperf_fold_key_conv.py +4 -4
  162. package/deps/jq/{modules → vendor}/oniguruma/src/gperf_unfold_key_conv.py +4 -4
  163. package/deps/jq/vendor/oniguruma/src/make_unicode_egcb.sh +7 -0
  164. package/deps/jq/{modules → vendor}/oniguruma/src/make_unicode_egcb_data.py +20 -19
  165. package/deps/jq/{modules → vendor}/oniguruma/src/make_unicode_fold.sh +5 -5
  166. package/deps/jq/{modules → vendor}/oniguruma/src/make_unicode_fold_data.py +37 -37
  167. package/deps/jq/{modules → vendor}/oniguruma/src/make_unicode_property.sh +2 -2
  168. package/deps/jq/{modules → vendor}/oniguruma/src/make_unicode_property_data.py +66 -65
  169. package/deps/jq/vendor/oniguruma/src/make_unicode_wb.sh +7 -0
  170. package/deps/jq/{modules → vendor}/oniguruma/src/make_unicode_wb_data.py +20 -19
  171. package/deps/jq/{modules → vendor}/oniguruma/src/oniguruma.h +6 -3
  172. package/deps/jq/{modules → vendor}/oniguruma/src/regcomp.c +14 -6
  173. package/deps/jq/{modules → vendor}/oniguruma/src/regerror.c +2 -2
  174. package/deps/jq/{modules → vendor}/oniguruma/src/regexec.c +85 -14
  175. package/deps/jq/{modules → vendor}/oniguruma/src/regint.h +17 -4
  176. package/deps/jq/{modules → vendor}/oniguruma/src/regparse.c +115 -27
  177. package/deps/jq/{modules → vendor}/oniguruma/src/regsyntax.c +10 -7
  178. package/deps/jq/{modules → vendor}/oniguruma/src/st.c +49 -75
  179. package/deps/jq/{modules → vendor}/oniguruma/src/st.h +3 -10
  180. package/deps/jq/{modules → vendor}/oniguruma/src/unicode_egcb_data.c +54 -49
  181. package/deps/jq/{modules → vendor}/oniguruma/src/unicode_fold1_key.c +1567 -1507
  182. package/deps/jq/{modules → vendor}/oniguruma/src/unicode_fold2_key.c +1 -1
  183. package/deps/jq/{modules → vendor}/oniguruma/src/unicode_fold3_key.c +1 -1
  184. package/deps/jq/vendor/oniguruma/src/unicode_fold_data.c +1619 -0
  185. package/deps/jq/{modules → vendor}/oniguruma/src/unicode_property_data.c +5520 -4178
  186. package/deps/jq/{modules → vendor}/oniguruma/src/unicode_property_data_posix.c +239 -83
  187. package/deps/jq/vendor/oniguruma/src/unicode_unfold_key.c +3497 -0
  188. package/deps/jq/{modules → vendor}/oniguruma/src/unicode_wb_data.c +51 -19
  189. package/deps/jq/{modules → vendor}/oniguruma/src/utf16_be.c +3 -2
  190. package/deps/jq/{modules → vendor}/oniguruma/src/utf16_le.c +3 -2
  191. package/deps/jq/{modules → vendor}/oniguruma/test/test_back.c +3 -3
  192. package/deps/jq/{modules → vendor}/oniguruma/test/test_options.c +2 -2
  193. package/deps/jq/{modules → vendor}/oniguruma/test/test_syntax.c +54 -2
  194. package/deps/jq/{modules → vendor}/oniguruma/test/test_utf8.c +19 -5
  195. package/deps/jq/{modules → vendor}/oniguruma/test/testc.c +1 -1
  196. package/deps/jq/{modules → vendor}/oniguruma/test/testp.c +1 -1
  197. package/deps/jq/{modules → vendor}/oniguruma/test-driver +13 -6
  198. package/deps/jq/{modules → vendor}/oniguruma/windows/CMakeLists.txt +1 -1
  199. package/index.d.ts +2 -1
  200. package/lib/index.js +1 -0
  201. package/lib/jq.js +2 -1
  202. package/package.json +4 -3
  203. package/test/santiy-async.test.js +4 -4
  204. package/test/santiy.test.js +4 -4
  205. package/test/template.test.js +6 -6
  206. package/deps/jq/modules/oniguruma/src/make_unicode_egcb.sh +0 -7
  207. package/deps/jq/modules/oniguruma/src/make_unicode_wb.sh +0 -7
  208. package/deps/jq/modules/oniguruma/src/unicode_fold_data.c +0 -1592
  209. package/deps/jq/modules/oniguruma/src/unicode_unfold_key.c +0 -3394
  210. package/deps/jq/scripts/update-website +0 -30
  211. /package/deps/jq/{src → vendor}/decNumber/ICU-license.html +0 -0
  212. /package/deps/jq/{src → vendor}/decNumber/decContext.c +0 -0
  213. /package/deps/jq/{src → vendor}/decNumber/decContext.h +0 -0
  214. /package/deps/jq/{src → vendor}/decNumber/decDPD.h +0 -0
  215. /package/deps/jq/{src → vendor}/decNumber/decDouble.c +0 -0
  216. /package/deps/jq/{src → vendor}/decNumber/decDouble.h +0 -0
  217. /package/deps/jq/{src → vendor}/decNumber/decNumber.h +0 -0
  218. /package/deps/jq/{src → vendor}/decNumber/decNumberLocal.h +0 -0
  219. /package/deps/jq/{src → vendor}/decNumber/decPacked.c +0 -0
  220. /package/deps/jq/{src → vendor}/decNumber/decPacked.h +0 -0
  221. /package/deps/jq/{src → vendor}/decNumber/decQuad.c +0 -0
  222. /package/deps/jq/{src → vendor}/decNumber/decQuad.h +0 -0
  223. /package/deps/jq/{src → vendor}/decNumber/decSingle.c +0 -0
  224. /package/deps/jq/{src → vendor}/decNumber/decSingle.h +0 -0
  225. /package/deps/jq/{src → vendor}/decNumber/decimal128.c +0 -0
  226. /package/deps/jq/{src → vendor}/decNumber/decimal128.h +0 -0
  227. /package/deps/jq/{src → vendor}/decNumber/decimal32.c +0 -0
  228. /package/deps/jq/{src → vendor}/decNumber/decimal32.h +0 -0
  229. /package/deps/jq/{src → vendor}/decNumber/decimal64.c +0 -0
  230. /package/deps/jq/{src → vendor}/decNumber/decimal64.h +0 -0
  231. /package/deps/jq/{src → vendor}/decNumber/decnumber.pdf +0 -0
  232. /package/deps/jq/{src → vendor}/decNumber/example1.c +0 -0
  233. /package/deps/jq/{src → vendor}/decNumber/example2.c +0 -0
  234. /package/deps/jq/{src → vendor}/decNumber/example3.c +0 -0
  235. /package/deps/jq/{src → vendor}/decNumber/example4.c +0 -0
  236. /package/deps/jq/{src → vendor}/decNumber/example5.c +0 -0
  237. /package/deps/jq/{src → vendor}/decNumber/example6.c +0 -0
  238. /package/deps/jq/{src → vendor}/decNumber/example7.c +0 -0
  239. /package/deps/jq/{src → vendor}/decNumber/example8.c +0 -0
  240. /package/deps/jq/{src → vendor}/decNumber/readme.txt +0 -0
  241. /package/deps/jq/{modules → vendor}/oniguruma/.travis.yml +0 -0
  242. /package/deps/jq/{modules → vendor}/oniguruma/AUTHORS +0 -0
  243. /package/deps/jq/{modules → vendor}/oniguruma/COPYING +0 -0
  244. /package/deps/jq/{modules → vendor}/oniguruma/ChangeLog +0 -0
  245. /package/deps/jq/{modules → vendor}/oniguruma/NEWS +0 -0
  246. /package/deps/jq/{modules → vendor}/oniguruma/README +0 -0
  247. /package/deps/jq/{modules → vendor}/oniguruma/README_japanese +0 -0
  248. /package/deps/jq/{modules → vendor}/oniguruma/autogen.sh +0 -0
  249. /package/deps/jq/{modules → vendor}/oniguruma/cmake/Config.cmake.in +0 -0
  250. /package/deps/jq/{modules → vendor}/oniguruma/doc/CALLOUTS.API +0 -0
  251. /package/deps/jq/{modules → vendor}/oniguruma/doc/CALLOUTS.API.ja +0 -0
  252. /package/deps/jq/{modules → vendor}/oniguruma/doc/FAQ +0 -0
  253. /package/deps/jq/{modules → vendor}/oniguruma/doc/FAQ.ja +0 -0
  254. /package/deps/jq/{modules → vendor}/oniguruma/harnesses/ascii_compatible.dict +0 -0
  255. /package/deps/jq/{modules → vendor}/oniguruma/harnesses/deluxe.c +0 -0
  256. /package/deps/jq/{modules → vendor}/oniguruma/harnesses/dict_conv.py +0 -0
  257. /package/deps/jq/{modules → vendor}/oniguruma/harnesses/fuzzer.options +0 -0
  258. /package/deps/jq/{modules → vendor}/oniguruma/harnesses/libfuzzer-onig.cpp +0 -0
  259. /package/deps/jq/{modules → vendor}/oniguruma/harnesses/regset.c +0 -0
  260. /package/deps/jq/{modules → vendor}/oniguruma/index.html +0 -0
  261. /package/deps/jq/{modules → vendor}/oniguruma/index_ja.html +0 -0
  262. /package/deps/jq/{modules → vendor}/oniguruma/m4/.whatever +0 -0
  263. /package/deps/jq/{modules → vendor}/oniguruma/make_win.bat +0 -0
  264. /package/deps/jq/{modules → vendor}/oniguruma/make_win32.bat +0 -0
  265. /package/deps/jq/{modules → vendor}/oniguruma/make_win64.bat +0 -0
  266. /package/deps/jq/{modules → vendor}/oniguruma/onig-config.cmake.in +0 -0
  267. /package/deps/jq/{modules → vendor}/oniguruma/onig-config.in +0 -0
  268. /package/deps/jq/{modules → vendor}/oniguruma/oniguruma.pc.cmake.in +0 -0
  269. /package/deps/jq/{modules → vendor}/oniguruma/oniguruma.pc.in +0 -0
  270. /package/deps/jq/{modules → vendor}/oniguruma/sample/CMakeLists.txt +0 -0
  271. /package/deps/jq/{modules → vendor}/oniguruma/sample/Makefile.am +0 -0
  272. /package/deps/jq/{modules → vendor}/oniguruma/sample/bug_fix.c +0 -0
  273. /package/deps/jq/{modules → vendor}/oniguruma/sample/callout.c +0 -0
  274. /package/deps/jq/{modules → vendor}/oniguruma/sample/count.c +0 -0
  275. /package/deps/jq/{modules → vendor}/oniguruma/sample/crnl.c +0 -0
  276. /package/deps/jq/{modules → vendor}/oniguruma/sample/echo.c +0 -0
  277. /package/deps/jq/{modules → vendor}/oniguruma/sample/encode.c +0 -0
  278. /package/deps/jq/{modules → vendor}/oniguruma/sample/listcap.c +0 -0
  279. /package/deps/jq/{modules → vendor}/oniguruma/sample/names.c +0 -0
  280. /package/deps/jq/{modules → vendor}/oniguruma/sample/posix.c +0 -0
  281. /package/deps/jq/{modules → vendor}/oniguruma/sample/regset.c +0 -0
  282. /package/deps/jq/{modules → vendor}/oniguruma/sample/scan.c +0 -0
  283. /package/deps/jq/{modules → vendor}/oniguruma/sample/simple.c +0 -0
  284. /package/deps/jq/{modules → vendor}/oniguruma/sample/sql.c +0 -0
  285. /package/deps/jq/{modules → vendor}/oniguruma/sample/syntax.c +0 -0
  286. /package/deps/jq/{modules → vendor}/oniguruma/sample/user_property.c +0 -0
  287. /package/deps/jq/{modules → vendor}/oniguruma/src/Makefile.am +0 -0
  288. /package/deps/jq/{modules → vendor}/oniguruma/src/Makefile.windows +0 -0
  289. /package/deps/jq/{modules → vendor}/oniguruma/src/big5.c +0 -0
  290. /package/deps/jq/{modules → vendor}/oniguruma/src/config.h.cmake.in +0 -0
  291. /package/deps/jq/{modules → vendor}/oniguruma/src/config.h.win32 +0 -0
  292. /package/deps/jq/{modules → vendor}/oniguruma/src/config.h.win64 +0 -0
  293. /package/deps/jq/{modules → vendor}/oniguruma/src/config.h.windows.in +0 -0
  294. /package/deps/jq/{modules → vendor}/oniguruma/src/cp1251.c +0 -0
  295. /package/deps/jq/{modules → vendor}/oniguruma/src/euc_jp.c +0 -0
  296. /package/deps/jq/{modules → vendor}/oniguruma/src/euc_jp_prop.c +0 -0
  297. /package/deps/jq/{modules → vendor}/oniguruma/src/euc_jp_prop.gperf +0 -0
  298. /package/deps/jq/{modules → vendor}/oniguruma/src/euc_kr.c +0 -0
  299. /package/deps/jq/{modules → vendor}/oniguruma/src/euc_tw.c +0 -0
  300. /package/deps/jq/{modules → vendor}/oniguruma/src/gb18030.c +0 -0
  301. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_1.c +0 -0
  302. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_10.c +0 -0
  303. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_11.c +0 -0
  304. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_13.c +0 -0
  305. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_14.c +0 -0
  306. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_15.c +0 -0
  307. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_16.c +0 -0
  308. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_2.c +0 -0
  309. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_3.c +0 -0
  310. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_4.c +0 -0
  311. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_5.c +0 -0
  312. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_6.c +0 -0
  313. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_7.c +0 -0
  314. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_8.c +0 -0
  315. /package/deps/jq/{modules → vendor}/oniguruma/src/iso8859_9.c +0 -0
  316. /package/deps/jq/{modules → vendor}/oniguruma/src/koi8.c +0 -0
  317. /package/deps/jq/{modules → vendor}/oniguruma/src/koi8_r.c +0 -0
  318. /package/deps/jq/{modules → vendor}/oniguruma/src/make_property.sh +0 -0
  319. /package/deps/jq/{modules → vendor}/oniguruma/src/mktable.c +0 -0
  320. /package/deps/jq/{modules → vendor}/oniguruma/src/onig_init.c +0 -0
  321. /package/deps/jq/{modules → vendor}/oniguruma/src/oniggnu.h +0 -0
  322. /package/deps/jq/{modules → vendor}/oniguruma/src/onigposix.h +0 -0
  323. /package/deps/jq/{modules → vendor}/oniguruma/src/regenc.c +0 -0
  324. /package/deps/jq/{modules → vendor}/oniguruma/src/regenc.h +0 -0
  325. /package/deps/jq/{modules → vendor}/oniguruma/src/regext.c +0 -0
  326. /package/deps/jq/{modules → vendor}/oniguruma/src/reggnu.c +0 -0
  327. /package/deps/jq/{modules → vendor}/oniguruma/src/regparse.h +0 -0
  328. /package/deps/jq/{modules → vendor}/oniguruma/src/regposerr.c +0 -0
  329. /package/deps/jq/{modules → vendor}/oniguruma/src/regposix.c +0 -0
  330. /package/deps/jq/{modules → vendor}/oniguruma/src/regtrav.c +0 -0
  331. /package/deps/jq/{modules → vendor}/oniguruma/src/regversion.c +0 -0
  332. /package/deps/jq/{modules → vendor}/oniguruma/src/sjis.c +0 -0
  333. /package/deps/jq/{modules → vendor}/oniguruma/src/sjis_prop.c +0 -0
  334. /package/deps/jq/{modules → vendor}/oniguruma/src/sjis_prop.gperf +0 -0
  335. /package/deps/jq/{modules → vendor}/oniguruma/src/unicode.c +0 -0
  336. /package/deps/jq/{modules → vendor}/oniguruma/src/utf32_be.c +0 -0
  337. /package/deps/jq/{modules → vendor}/oniguruma/src/utf32_le.c +0 -0
  338. /package/deps/jq/{modules → vendor}/oniguruma/src/utf8.c +0 -0
  339. /package/deps/jq/{modules → vendor}/oniguruma/test/CMakeLists.txt +0 -0
  340. /package/deps/jq/{modules → vendor}/oniguruma/test/Makefile.am +0 -0
  341. /package/deps/jq/{modules → vendor}/oniguruma/test/test.sh +0 -0
  342. /package/deps/jq/{modules → vendor}/oniguruma/test/test_regset.c +0 -0
  343. /package/deps/jq/{modules → vendor}/oniguruma/test/testu.c +0 -0
  344. /package/deps/jq/{modules → vendor}/oniguruma/tis-ci/stub.c +0 -0
  345. /package/deps/jq/{modules → vendor}/oniguruma/tis-ci/test_back.config +0 -0
  346. /package/deps/jq/{modules → vendor}/oniguruma/tis-ci/test_regset.config +0 -0
  347. /package/deps/jq/{modules → vendor}/oniguruma/tis-ci/test_syntax.config +0 -0
  348. /package/deps/jq/{modules → vendor}/oniguruma/tis-ci/test_utf8.config +0 -0
  349. /package/deps/jq/{modules → vendor}/oniguruma/tis-ci/testc.config +0 -0
  350. /package/deps/jq/{modules → vendor}/oniguruma/tis-ci/testu.config +0 -0
  351. /package/deps/jq/{modules → vendor}/oniguruma/tis.config +0 -0
  352. /package/deps/jq/{modules → vendor}/oniguruma/windows/testc.c +0 -0
@@ -0,0 +1,3858 @@
1
+ ---
2
+ headline: jq 1.8 Manual
3
+
4
+ body: |
5
+
6
+ A jq program is a "filter": it takes an input, and produces an
7
+ output. There are a lot of builtin filters for extracting a
8
+ particular field of an object, or converting a number to a string,
9
+ or various other standard tasks.
10
+
11
+ Filters can be combined in various ways - you can pipe the output of
12
+ one filter into another filter, or collect the output of a filter
13
+ into an array.
14
+
15
+ Some filters produce multiple results, for instance there's one that
16
+ produces all the elements of its input array. Piping that filter
17
+ into a second runs the second filter for each element of the
18
+ array. Generally, things that would be done with loops and iteration
19
+ in other languages are just done by gluing filters together in jq.
20
+
21
+ It's important to remember that every filter has an input and an
22
+ output. Even literals like "hello" or 42 are filters - they take an
23
+ input but always produce the same literal as output. Operations that
24
+ combine two filters, like addition, generally feed the same input to
25
+ both and combine the results. So, you can implement an averaging
26
+ filter as `add / length` - feeding the input array both to the `add`
27
+ filter and the `length` filter and then performing the division.
28
+
29
+ But that's getting ahead of ourselves. :) Let's start with something
30
+ simpler:
31
+
32
+ manpage_intro: |
33
+ jq(1) -- Command-line JSON processor
34
+ ====================================
35
+
36
+ ## SYNOPSIS
37
+
38
+ `jq` [<options>...] <filter> [<files>...]
39
+
40
+ `jq` can transform JSON in various ways, by selecting, iterating,
41
+ reducing and otherwise mangling JSON documents. For instance,
42
+ running the command `jq 'map(.price) | add'` will take an array of
43
+ JSON objects as input and return the sum of their "price" fields.
44
+
45
+ `jq` can accept text input as well, but by default, `jq` reads a
46
+ stream of JSON entities (including numbers and other literals) from
47
+ `stdin`. Whitespace is only needed to separate entities such as 1
48
+ and 2, and true and false. One or more <files> may be specified, in
49
+ which case `jq` will read input from those instead.
50
+
51
+ The <options> are described in the [INVOKING JQ] section; they
52
+ mostly concern input and output formatting. The <filter> is written
53
+ in the jq language and specifies how to transform the input
54
+ file or document.
55
+
56
+ ## FILTERS
57
+
58
+ manpage_epilogue: |
59
+ ## BUGS
60
+
61
+ Presumably. Report them or discuss them at:
62
+
63
+ https://github.com/jqlang/jq/issues
64
+
65
+ ## AUTHOR
66
+
67
+ Stephen Dolan `<mu@netsoc.tcd.ie>`
68
+
69
+ sections:
70
+ - title: Invoking jq
71
+ body: |
72
+
73
+ jq filters run on a stream of JSON data. The input to jq is
74
+ parsed as a sequence of whitespace-separated JSON values which
75
+ are passed through the provided filter one at a time. The
76
+ output(s) of the filter are written to standard output, as a
77
+ sequence of newline-separated JSON data.
78
+
79
+ The simplest and most common filter (or jq program) is `.`,
80
+ which is the identity operator, copying the inputs of the jq
81
+ processor to the output stream. Because the default behavior of
82
+ the jq processor is to read JSON texts from the input stream,
83
+ and to pretty-print outputs, the `.` program's main use is to
84
+ validate and pretty-print the inputs. The jq programming
85
+ language is quite rich and allows for much more than just
86
+ validation and pretty-printing.
87
+
88
+ Note: it is important to mind the shell's quoting rules. As a
89
+ general rule it's best to always quote (with single-quote
90
+ characters on Unix shells) the jq program, as too many characters with special
91
+ meaning to jq are also shell meta-characters. For example, `jq
92
+ "foo"` will fail on most Unix shells because that will be the same
93
+ as `jq foo`, which will generally fail because `foo is not
94
+ defined`. When using the Windows command shell (cmd.exe) it's
95
+ best to use double quotes around your jq program when given on the
96
+ command-line (instead of the `-f program-file` option), but then
97
+ double-quotes in the jq program need backslash escaping. When using
98
+ the Powershell (`powershell.exe`) or the Powershell Core
99
+ (`pwsh`/`pwsh.exe`), use single-quote characters around the jq
100
+ program and backslash-escaped double-quotes (`\"`) inside the jq
101
+ program.
102
+
103
+ * Unix shells: `jq '.["foo"]'`
104
+ * Powershell: `jq '.[\"foo\"]'`
105
+ * Windows command shell: `jq ".[\"foo\"]"`
106
+
107
+ Note: jq allows user-defined functions, but every jq program
108
+ must have a top-level expression.
109
+
110
+ You can affect how jq reads and writes its input and output
111
+ using some command-line options:
112
+
113
+ * `--null-input` / `-n`:
114
+
115
+ Don't read any input at all. Instead, the filter is run once
116
+ using `null` as the input. This is useful when using jq as a
117
+ simple calculator or to construct JSON data from scratch.
118
+
119
+ * `--raw-input` / `-R`:
120
+
121
+ Don't parse the input as JSON. Instead, each line of text is
122
+ passed to the filter as a string. If combined with `--slurp`,
123
+ then the entire input is passed to the filter as a single long
124
+ string.
125
+
126
+ * `--slurp` / `-s`:
127
+
128
+ Instead of running the filter for each JSON object in the
129
+ input, read the entire input stream into a large array and run
130
+ the filter just once.
131
+
132
+ * `--compact-output` / `-c`:
133
+
134
+ By default, jq pretty-prints JSON output. Using this option
135
+ will result in more compact output by instead putting each
136
+ JSON object on a single line.
137
+
138
+ * `--raw-output` / `-r`:
139
+
140
+ With this option, if the filter's result is a string then it
141
+ will be written directly to standard output rather than being
142
+ formatted as a JSON string with quotes. This can be useful for
143
+ making jq filters talk to non-JSON-based systems.
144
+
145
+ * `--raw-output0`:
146
+
147
+ Like `-r` but jq will print NUL instead of newline after each output.
148
+ This can be useful when the values being output can contain newlines.
149
+ When the output value contains NUL, jq exits with non-zero code.
150
+
151
+ * `--join-output` / `-j`:
152
+
153
+ Like `-r` but jq won't print a newline after each output.
154
+
155
+ * `--ascii-output` / `-a`:
156
+
157
+ jq usually outputs non-ASCII Unicode codepoints as UTF-8, even
158
+ if the input specified them as escape sequences (like
159
+ "\u03bc"). Using this option, you can force jq to produce pure
160
+ ASCII output with every non-ASCII character replaced with the
161
+ equivalent escape sequence.
162
+
163
+ * `--sort-keys` / `-S`:
164
+
165
+ Output the fields of each object with the keys in sorted order.
166
+
167
+ * `--color-output` / `-C` and `--monochrome-output` / `-M`:
168
+
169
+ By default, jq outputs colored JSON if writing to a
170
+ terminal. You can force it to produce color even if writing to
171
+ a pipe or a file using `-C`, and disable color with `-M`.
172
+ When the `NO_COLOR` environment variable is not empty, jq disables
173
+ colored output by default, but you can enable it by `-C`.
174
+
175
+ Colors can be configured with the `JQ_COLORS` environment
176
+ variable (see below).
177
+
178
+ * `--tab`:
179
+
180
+ Use a tab for each indentation level instead of two spaces.
181
+
182
+ * `--indent n`:
183
+
184
+ Use the given number of spaces (no more than 7) for indentation.
185
+
186
+ * `--unbuffered`:
187
+
188
+ Flush the output after each JSON object is printed (useful if
189
+ you're piping a slow data source into jq and piping jq's
190
+ output elsewhere).
191
+
192
+ * `--stream`:
193
+
194
+ Parse the input in streaming fashion, outputting arrays of path
195
+ and leaf values (scalars and empty arrays or empty objects).
196
+ For example, `"a"` becomes `[[],"a"]`, and `[[],"a",["b"]]`
197
+ becomes `[[0],[]]`, `[[1],"a"]`, and `[[2,0],"b"]`.
198
+
199
+ This is useful for processing very large inputs. Use this in
200
+ conjunction with filtering and the `reduce` and `foreach` syntax
201
+ to reduce large inputs incrementally.
202
+
203
+ * `--stream-errors`:
204
+
205
+ Like `--stream`, but invalid JSON inputs yield array values
206
+ where the first element is the error and the second is a path.
207
+ For example, `["a",n]` produces `["Invalid literal at line 1,
208
+ column 7",[1]]`.
209
+
210
+ Implies `--stream`. Invalid JSON inputs produce no error values
211
+ when `--stream` without `--stream-errors`.
212
+
213
+ * `--seq`:
214
+
215
+ Use the `application/json-seq` MIME type scheme for separating
216
+ JSON texts in jq's input and output. This means that an ASCII
217
+ RS (record separator) character is printed before each value on
218
+ output and an ASCII LF (line feed) is printed after every
219
+ output. Input JSON texts that fail to parse are ignored (but
220
+ warned about), discarding all subsequent input until the next
221
+ RS. This mode also parses the output of jq without the `--seq`
222
+ option.
223
+
224
+ * `-f` / `--from-file`:
225
+
226
+ Read the filter from a file rather than from a command line,
227
+ like awk's -f option. This changes the filter argument to be
228
+ interpreted as a filename, instead of the source of a program.
229
+
230
+ * `-L directory` / `--library-path directory`:
231
+
232
+ Prepend `directory` to the search list for modules. If this
233
+ option is used then no builtin search list is used. See the
234
+ section on modules below.
235
+
236
+ * `--arg name value`:
237
+
238
+ This option passes a value to the jq program as a predefined
239
+ variable. If you run jq with `--arg foo bar`, then `$foo` is
240
+ available in the program and has the value `"bar"`. Note that
241
+ `value` will be treated as a string, so `--arg foo 123` will
242
+ bind `$foo` to `"123"`.
243
+
244
+ Named arguments are also available to the jq program as
245
+ `$ARGS.named`. When the name is not a valid identifier, this is
246
+ the only way to access it.
247
+
248
+ * `--argjson name JSON-text`:
249
+
250
+ This option passes a JSON-encoded value to the jq program as a
251
+ predefined variable. If you run jq with `--argjson foo 123`, then
252
+ `$foo` is available in the program and has the value `123`.
253
+
254
+ * `--slurpfile variable-name filename`:
255
+
256
+ This option reads all the JSON texts in the named file and binds
257
+ an array of the parsed JSON values to the given global variable.
258
+ If you run jq with `--slurpfile foo bar`, then `$foo` is available
259
+ in the program and has an array whose elements correspond to the
260
+ texts in the file named `bar`.
261
+
262
+ * `--rawfile variable-name filename`:
263
+
264
+ This option reads in the named file and binds its contents to the given
265
+ global variable. If you run jq with `--rawfile foo bar`, then `$foo` is
266
+ available in the program and has a string whose contents are to the texts
267
+ in the file named `bar`.
268
+
269
+ * `--args`:
270
+
271
+ Remaining arguments are positional string arguments. These are
272
+ available to the jq program as `$ARGS.positional[]`.
273
+
274
+ * `--jsonargs`:
275
+
276
+ Remaining arguments are positional JSON text arguments. These
277
+ are available to the jq program as `$ARGS.positional[]`.
278
+
279
+ * `--exit-status` / `-e`:
280
+
281
+ Sets the exit status of jq to 0 if the last output value was
282
+ neither `false` nor `null`, 1 if the last output value was
283
+ either `false` or `null`, or 4 if no valid result was ever
284
+ produced. Normally jq exits with 2 if there was any usage
285
+ problem or system error, 3 if there was a jq program compile
286
+ error, or 0 if the jq program ran.
287
+
288
+ Another way to set the exit status is with the `halt_error`
289
+ builtin function.
290
+
291
+ * `--binary` / `-b`:
292
+
293
+ Windows users using WSL, MSYS2, or Cygwin, should use this option
294
+ when using a native jq.exe, otherwise jq will turn newlines (LFs)
295
+ into carriage-return-then-newline (CRLF).
296
+
297
+ * `--version` / `-V`:
298
+
299
+ Output the jq version and exit with zero.
300
+
301
+ * `--build-configuration`:
302
+
303
+ Output the build configuration of jq and exit with zero.
304
+ This output has no supported format or structure and may change
305
+ without notice in future releases.
306
+
307
+ * `--help` / `-h`:
308
+
309
+ Output the jq help and exit with zero.
310
+
311
+ * `--`:
312
+
313
+ Terminates argument processing. Remaining arguments are not
314
+ interpreted as options.
315
+
316
+ * `--run-tests [filename]`:
317
+
318
+ Runs the tests in the given file or standard input. This must
319
+ be the last option given and does not honor all preceding
320
+ options. The input consists of comment lines, empty lines, and
321
+ program lines followed by one input line, as many lines of
322
+ output as are expected (one per output), and a terminating empty
323
+ line. Compilation failure tests start with a line containing
324
+ only `%%FAIL`, then a line containing the program to compile,
325
+ then a line containing an error message to compare to the
326
+ actual.
327
+
328
+ Be warned that this option can change backwards-incompatibly.
329
+
330
+ - title: Basic filters
331
+ entries:
332
+ - title: "Identity: `.`"
333
+ body: |
334
+
335
+ The absolute simplest filter is `.` . This filter takes its
336
+ input and produces the same value as output. That is, this
337
+ is the identity operator.
338
+
339
+ Since jq by default pretty-prints all output, a trivial
340
+ program consisting of nothing but `.` can be used to format
341
+ JSON output from, say, `curl`.
342
+
343
+ Although the identity filter never modifies the value of its
344
+ input, jq processing can sometimes make it appear as though
345
+ it does. For example, using the current implementation of
346
+ jq, we would see that the expression:
347
+
348
+ 1E1234567890 | .
349
+
350
+ produces `1.7976931348623157e+308` on at least one platform.
351
+ This is because, in the process of parsing the number, this
352
+ particular version of jq has converted it to an IEEE754
353
+ double-precision representation, losing precision.
354
+
355
+ The way in which jq handles numbers has changed over time
356
+ and further changes are likely within the parameters set by
357
+ the relevant JSON standards. Moreover, build configuration
358
+ options can alter how jq processes numbers.
359
+
360
+ The following remarks are therefore offered with the
361
+ understanding that they are intended to be descriptive of the
362
+ current version of jq and should not be interpreted as being
363
+ prescriptive:
364
+
365
+ (1) Any arithmetic operation on a number that has not
366
+ already been converted to an IEEE754 double precision
367
+ representation will trigger a conversion to the IEEE754
368
+ representation.
369
+
370
+ (2) jq will attempt to maintain the original decimal
371
+ precision of number literals (if the `--disable-decnum`
372
+ build configuration option was not used), but in expressions
373
+ such `1E1234567890`, precision will be lost if the exponent
374
+ is too large.
375
+
376
+ (3) Comparisons are carried out using the untruncated
377
+ big decimal representation of numbers if available, as
378
+ illustrated in one of the following examples.
379
+
380
+ The examples below use the builtin function `have_decnum` in
381
+ order to demonstrate the expected effects of using / not
382
+ using the `--disable-decnum` build configuration option, and
383
+ also to allow automated tests derived from these examples to
384
+ pass regardless of whether that option is used.
385
+
386
+ examples:
387
+ - program: '.'
388
+ input: '"Hello, world!"'
389
+ output: ['"Hello, world!"']
390
+
391
+ - program: '.'
392
+ input: '0.12345678901234567890123456789'
393
+ output: ['0.12345678901234567890123456789']
394
+
395
+ - program: '[., tojson] == if have_decnum then [12345678909876543212345,"12345678909876543212345"] else [12345678909876543000000,"12345678909876543000000"] end'
396
+ input: '12345678909876543212345'
397
+ output: ['true']
398
+
399
+ - program: '[1234567890987654321,-1234567890987654321 | tojson] == if have_decnum then ["1234567890987654321","-1234567890987654321"] else ["1234567890987654400","-1234567890987654400"] end'
400
+ input: 'null'
401
+ output: ['true']
402
+
403
+ - program: '. < 0.12345678901234567890123456788'
404
+ input: '0.12345678901234567890123456789'
405
+ output: ['false']
406
+
407
+ - program: 'map([., . == 1]) | tojson == if have_decnum then "[[1,true],[1.000,true],[1.0,true],[1.00,true]]" else "[[1,true],[1,true],[1,true],[1,true]]" end'
408
+ input: '[1, 1.000, 1.0, 100e-2]'
409
+ output: ['true']
410
+
411
+ - program: '. as $big | [$big, $big + 1] | map(. > 10000000000000000000000000000000) | . == if have_decnum then [true, false] else [false, false] end'
412
+ input: '10000000000000000000000000000001'
413
+ output: ['true']
414
+
415
+ - title: "Object Identifier-Index: `.foo`, `.foo.bar`"
416
+ body: |
417
+
418
+ The simplest *useful* filter has the form `.foo`. When given a
419
+ JSON object (aka dictionary or hash) as input, `.foo` produces
420
+ the value at the key "foo" if the key is present, or null otherwise.
421
+
422
+ A filter of the form `.foo.bar` is equivalent to `.foo | .bar`.
423
+
424
+ The `.foo` syntax only works for simple, identifier-like keys, that
425
+ is, keys that are all made of alphanumeric characters and
426
+ underscore, and which do not start with a digit.
427
+
428
+ If the key contains special characters or starts with a digit,
429
+ you need to surround it with double quotes like this:
430
+ `."foo$"`, or else `.["foo$"]`.
431
+
432
+ For example `.["foo::bar"]` and `.["foo.bar"]` work while
433
+ `.foo::bar` does not.
434
+
435
+ examples:
436
+ - program: '.foo'
437
+ input: '{"foo": 42, "bar": "less interesting data"}'
438
+ output: ['42']
439
+
440
+ - program: '.foo'
441
+ input: '{"notfoo": true, "alsonotfoo": false}'
442
+ output: ['null']
443
+
444
+ - program: '.["foo"]'
445
+ input: '{"foo": 42}'
446
+ output: ['42']
447
+
448
+ - title: "Optional Object Identifier-Index: `.foo?`"
449
+ body: |
450
+
451
+ Just like `.foo`, but does not output an error when `.` is not an
452
+ object.
453
+
454
+ examples:
455
+ - program: '.foo?'
456
+ input: '{"foo": 42, "bar": "less interesting data"}'
457
+ output: ['42']
458
+ - program: '.foo?'
459
+ input: '{"notfoo": true, "alsonotfoo": false}'
460
+ output: ['null']
461
+ - program: '.["foo"]?'
462
+ input: '{"foo": 42}'
463
+ output: ['42']
464
+ - program: '[.foo?]'
465
+ input: '[1,2]'
466
+ output: ['[]']
467
+
468
+ - title: "Object Index: `.[<string>]`"
469
+ body: |
470
+
471
+ You can also look up fields of an object using syntax like
472
+ `.["foo"]` (`.foo` above is a shorthand version of this, but
473
+ only for identifier-like strings).
474
+
475
+ - title: "Array Index: `.[<number>]`"
476
+ body: |
477
+
478
+ When the index value is an integer, `.[<number>]` can index
479
+ arrays. Arrays are zero-based, so `.[2]` returns the third
480
+ element.
481
+
482
+ Negative indices are allowed, with -1 referring to the last
483
+ element, -2 referring to the next to last element, and so on.
484
+
485
+ examples:
486
+ - program: '.[0]'
487
+ input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
488
+ output: ['{"name":"JSON", "good":true}']
489
+
490
+ - program: '.[2]'
491
+ input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
492
+ output: ['null']
493
+
494
+ - program: '.[-2]'
495
+ input: '[1,2,3]'
496
+ output: ['2']
497
+
498
+ - title: "Array/String Slice: `.[<number>:<number>]`"
499
+ body: |
500
+
501
+ The `.[<number>:<number>]` syntax can be used to return a
502
+ subarray of an array or substring of a string. The array
503
+ returned by `.[10:15]` will be of length 5, containing the
504
+ elements from index 10 (inclusive) to index 15 (exclusive).
505
+ Either index may be negative (in which case it counts
506
+ backwards from the end of the array), or omitted (in which
507
+ case it refers to the start or end of the array).
508
+ Indices are zero-based.
509
+
510
+ examples:
511
+ - program: '.[2:4]'
512
+ input: '["a","b","c","d","e"]'
513
+ output: ['["c", "d"]']
514
+
515
+ - program: '.[2:4]'
516
+ input: '"abcdefghi"'
517
+ output: ['"cd"']
518
+
519
+ - program: '.[:3]'
520
+ input: '["a","b","c","d","e"]'
521
+ output: ['["a", "b", "c"]']
522
+
523
+ - program: '.[-2:]'
524
+ input: '["a","b","c","d","e"]'
525
+ output: ['["d", "e"]']
526
+
527
+ - title: "Array/Object Value Iterator: `.[]`"
528
+ body: |
529
+
530
+ If you use the `.[index]` syntax, but omit the index
531
+ entirely, it will return *all* of the elements of an
532
+ array. Running `.[]` with the input `[1,2,3]` will produce the
533
+ numbers as three separate results, rather than as a single
534
+ array. A filter of the form `.foo[]` is equivalent to
535
+ `.foo | .[]`.
536
+
537
+ You can also use this on an object, and it will return all
538
+ the values of the object.
539
+
540
+ Note that the iterator operator is a generator of values.
541
+
542
+ examples:
543
+ - program: '.[]'
544
+ input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
545
+ output:
546
+ - '{"name":"JSON", "good":true}'
547
+ - '{"name":"XML", "good":false}'
548
+
549
+ - program: '.[]'
550
+ input: '[]'
551
+ output: []
552
+
553
+ - program: '.foo[]'
554
+ input: '{"foo":[1,2,3]}'
555
+ output: ['1','2','3']
556
+
557
+ - program: '.[]'
558
+ input: '{"a": 1, "b": 1}'
559
+ output: ['1', '1']
560
+
561
+ - title: "`.[]?`"
562
+ body: |
563
+
564
+ Like `.[]`, but no errors will be output if . is not an array
565
+ or object. A filter of the form `.foo[]?` is equivalent to
566
+ `.foo | .[]?`.
567
+
568
+ - title: "Comma: `,`"
569
+ body: |
570
+
571
+ If two filters are separated by a comma, then the
572
+ same input will be fed into both and the two filters' output
573
+ value streams will be concatenated in order: first, all of the
574
+ outputs produced by the left expression, and then all of the
575
+ outputs produced by the right. For instance, filter `.foo,
576
+ .bar`, produces both the "foo" fields and "bar" fields as
577
+ separate outputs.
578
+
579
+ The `,` operator is one way to construct generators.
580
+
581
+ examples:
582
+ - program: '.foo, .bar'
583
+ input: '{"foo": 42, "bar": "something else", "baz": true}'
584
+ output: ['42', '"something else"']
585
+
586
+ - program: ".user, .projects[]"
587
+ input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'
588
+ output: ['"stedolan"', '"jq"', '"wikiflow"']
589
+
590
+ - program: '.[4,2]'
591
+ input: '["a","b","c","d","e"]'
592
+ output: ['"e"', '"c"']
593
+
594
+ - title: "Pipe: `|`"
595
+ body: |
596
+
597
+ The | operator combines two filters by feeding the output(s) of
598
+ the one on the left into the input of the one on the right. It's
599
+ similar to the Unix shell's pipe, if you're used to that.
600
+
601
+ If the one on the left produces multiple results, the one on
602
+ the right will be run for each of those results. So, the
603
+ expression `.[] | .foo` retrieves the "foo" field of each
604
+ element of the input array. This is a cartesian product,
605
+ which can be surprising.
606
+
607
+ Note that `.a.b.c` is the same as `.a | .b | .c`.
608
+
609
+ Note too that `.` is the input value at the particular stage
610
+ in a "pipeline", specifically: where the `.` expression appears.
611
+ Thus `.a | . | .b` is the same as `.a.b`, as the `.` in the
612
+ middle refers to whatever value `.a` produced.
613
+
614
+ examples:
615
+ - program: '.[] | .name'
616
+ input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
617
+ output: ['"JSON"', '"XML"']
618
+
619
+ - title: "Parenthesis"
620
+ body: |
621
+
622
+ Parenthesis work as a grouping operator just as in any typical
623
+ programming language.
624
+
625
+ examples:
626
+ - program: '(. + 2) * 5'
627
+ input: '1'
628
+ output: ['15']
629
+
630
+ - title: Types and Values
631
+ body: |
632
+
633
+ jq supports the same set of datatypes as JSON - numbers,
634
+ strings, booleans, arrays, objects (which in JSON-speak are
635
+ hashes with only string keys), and "null".
636
+
637
+ Booleans, null, strings and numbers are written the same way as
638
+ in JSON. Just like everything else in jq, these simple
639
+ values take an input and produce an output - `42` is a valid jq
640
+ expression that takes an input, ignores it, and returns 42
641
+ instead.
642
+
643
+ Numbers in jq are internally represented by their IEEE754 double
644
+ precision approximation. Any arithmetic operation with numbers,
645
+ whether they are literals or results of previous filters, will
646
+ produce a double precision floating point result.
647
+
648
+ However, when parsing a literal jq will store the original literal
649
+ string. If no mutation is applied to this value then it will make
650
+ to the output in its original form, even if conversion to double
651
+ would result in a loss.
652
+
653
+ entries:
654
+ - title: "Array construction: `[]`"
655
+ body: |
656
+
657
+ As in JSON, `[]` is used to construct arrays, as in
658
+ `[1,2,3]`. The elements of the arrays can be any jq
659
+ expression, including a pipeline. All of the results produced
660
+ by all of the expressions are collected into one big array.
661
+ You can use it to construct an array out of a known quantity
662
+ of values (as in `[.foo, .bar, .baz]`) or to "collect" all the
663
+ results of a filter into an array (as in `[.items[].name]`)
664
+
665
+ Once you understand the "," operator, you can look at jq's array
666
+ syntax in a different light: the expression `[1,2,3]` is not using a
667
+ built-in syntax for comma-separated arrays, but is instead applying
668
+ the `[]` operator (collect results) to the expression 1,2,3 (which
669
+ produces three different results).
670
+
671
+ If you have a filter `X` that produces four results,
672
+ then the expression `[X]` will produce a single result, an
673
+ array of four elements.
674
+
675
+ examples:
676
+ - program: "[.user, .projects[]]"
677
+ input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'
678
+ output: ['["stedolan", "jq", "wikiflow"]']
679
+ - program: "[ .[] | . * 2]"
680
+ input: '[1, 2, 3]'
681
+ output: ['[2, 4, 6]']
682
+
683
+ - title: "Object Construction: `{}`"
684
+ body: |
685
+
686
+ Like JSON, `{}` is for constructing objects (aka
687
+ dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
688
+
689
+ If the keys are "identifier-like", then the quotes can be left
690
+ off, as in `{a:42, b:17}`. Variable references as key
691
+ expressions use the value of the variable as the key. Key
692
+ expressions other than constant literals, identifiers, or
693
+ variable references, need to be parenthesized, e.g.,
694
+ `{("a"+"b"):59}`.
695
+
696
+ The value can be any expression (although you may need to wrap
697
+ it in parentheses if, for example, it contains colons), which
698
+ gets applied to the {} expression's input (remember, all
699
+ filters have an input and an output).
700
+
701
+ {foo: .bar}
702
+
703
+ will produce the JSON object `{"foo": 42}` if given the JSON
704
+ object `{"bar":42, "baz":43}` as its input. You can use this
705
+ to select particular fields of an object: if the input is an
706
+ object with "user", "title", "id", and "content" fields and
707
+ you just want "user" and "title", you can write
708
+
709
+ {user: .user, title: .title}
710
+
711
+ Because that is so common, there's a shortcut syntax for it:
712
+ `{user, title}`.
713
+
714
+ If one of the expressions produces multiple results,
715
+ multiple dictionaries will be produced. If the input's
716
+
717
+ {"user":"stedolan","titles":["JQ Primer", "More JQ"]}
718
+
719
+ then the expression
720
+
721
+ {user, title: .titles[]}
722
+
723
+ will produce two outputs:
724
+
725
+ {"user":"stedolan", "title": "JQ Primer"}
726
+ {"user":"stedolan", "title": "More JQ"}
727
+
728
+ Putting parentheses around the key means it will be evaluated as an
729
+ expression. With the same input as above,
730
+
731
+ {(.user): .titles}
732
+
733
+ produces
734
+
735
+ {"stedolan": ["JQ Primer", "More JQ"]}
736
+
737
+ Variable references as keys use the value of the variable as
738
+ the key. Without a value then the variable's name becomes the
739
+ key and its value becomes the value,
740
+
741
+ "f o o" as $foo | "b a r" as $bar | {$foo, $bar:$foo}
742
+
743
+ produces
744
+
745
+ {"foo":"f o o","b a r":"f o o"}
746
+
747
+ examples:
748
+ - program: '{user, title: .titles[]}'
749
+ input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
750
+ output:
751
+ - '{"user":"stedolan", "title": "JQ Primer"}'
752
+ - '{"user":"stedolan", "title": "More JQ"}'
753
+ - program: '{(.user): .titles}'
754
+ input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
755
+ output: ['{"stedolan": ["JQ Primer", "More JQ"]}']
756
+
757
+ - title: "Recursive Descent: `..`"
758
+ body: |
759
+
760
+ Recursively descends `.`, producing every value. This is the
761
+ same as the zero-argument `recurse` builtin (see below). This
762
+ is intended to resemble the XPath `//` operator. Note that
763
+ `..a` does not work; use `.. | .a` instead. In the example
764
+ below we use `.. | .a?` to find all the values of object keys
765
+ "a" in any object found "below" `.`.
766
+
767
+ This is particularly useful in conjunction with `path(EXP)`
768
+ (also see below) and the `?` operator.
769
+
770
+ examples:
771
+ - program: '.. | .a?'
772
+ input: '[[{"a":1}]]'
773
+ output: ['1']
774
+
775
+ - title: Builtin operators and functions
776
+ body: |
777
+
778
+ Some jq operators (for instance, `+`) do different things
779
+ depending on the type of their arguments (arrays, numbers,
780
+ etc.). However, jq never does implicit type conversions. If you
781
+ try to add a string to an object you'll get an error message and
782
+ no result.
783
+
784
+ Please note that all numbers are converted to IEEE754 double precision
785
+ floating point representation. Arithmetic and logical operators are working
786
+ with these converted doubles. Results of all such operations are also limited
787
+ to the double precision.
788
+
789
+ The only exception to this behaviour of number is a snapshot of original number
790
+ literal. When a number which originally was provided as a literal is never
791
+ mutated until the end of the program then it is printed to the output in its
792
+ original literal form. This also includes cases when the original literal
793
+ would be truncated when converted to the IEEE754 double precision floating point
794
+ number.
795
+
796
+ entries:
797
+ - title: "Addition: `+`"
798
+ body: |
799
+
800
+ The operator `+` takes two filters, applies them both
801
+ to the same input, and adds the results together. What
802
+ "adding" means depends on the types involved:
803
+
804
+ - **Numbers** are added by normal arithmetic.
805
+
806
+ - **Arrays** are added by being concatenated into a larger array.
807
+
808
+ - **Strings** are added by being joined into a larger string.
809
+
810
+ - **Objects** are added by merging, that is, inserting all
811
+ the key-value pairs from both objects into a single
812
+ combined object. If both objects contain a value for the
813
+ same key, the object on the right of the `+` wins. (For
814
+ recursive merge use the `*` operator.)
815
+
816
+ `null` can be added to any value, and returns the other
817
+ value unchanged.
818
+
819
+ examples:
820
+ - program: '.a + 1'
821
+ input: '{"a": 7}'
822
+ output: ['8']
823
+ - program: '.a + .b'
824
+ input: '{"a": [1,2], "b": [3,4]}'
825
+ output: ['[1,2,3,4]']
826
+ - program: '.a + null'
827
+ input: '{"a": 1}'
828
+ output: ['1']
829
+ - program: '.a + 1'
830
+ input: '{}'
831
+ output: ['1']
832
+ - program: '{a: 1} + {b: 2} + {c: 3} + {a: 42}'
833
+ input: 'null'
834
+ output: ['{"a": 42, "b": 2, "c": 3}']
835
+
836
+ - title: "Subtraction: `-`"
837
+ body: |
838
+
839
+ As well as normal arithmetic subtraction on numbers, the `-`
840
+ operator can be used on arrays to remove all occurrences of
841
+ the second array's elements from the first array.
842
+
843
+ examples:
844
+ - program: '4 - .a'
845
+ input: '{"a":3}'
846
+ output: ['1']
847
+ - program: . - ["xml", "yaml"]
848
+ input: '["xml", "yaml", "json"]'
849
+ output: ['["json"]']
850
+
851
+ - title: "Multiplication, division, modulo: `*`, `/`, `%`"
852
+ body: |
853
+
854
+ These infix operators behave as expected when given two numbers.
855
+ Division by zero raises an error. `x % y` computes x modulo y.
856
+
857
+ Multiplying a string by a number produces the concatenation of
858
+ that string that many times. `"x" * 0` produces `""`.
859
+
860
+ Dividing a string by another splits the first using the second
861
+ as separators.
862
+
863
+ Multiplying two objects will merge them recursively: this works
864
+ like addition but if both objects contain a value for the
865
+ same key, and the values are objects, the two are merged with
866
+ the same strategy.
867
+
868
+ examples:
869
+ - program: '10 / . * 3'
870
+ input: '5'
871
+ output: ['6']
872
+ - program: '. / ", "'
873
+ input: '"a, b,c,d, e"'
874
+ output: ['["a","b,c,d","e"]']
875
+ - program: '{"k": {"a": 1, "b": 2}} * {"k": {"a": 0,"c": 3}}'
876
+ input: 'null'
877
+ output: ['{"k": {"a": 0, "b": 2, "c": 3}}']
878
+ - program: '.[] | (1 / .)?'
879
+ input: '[1,0,-1]'
880
+ output: ['1', '-1']
881
+
882
+ - title: "`abs`"
883
+ body: |
884
+
885
+ The builtin function `abs` is defined naively as: `if . < 0 then - . else . end`.
886
+
887
+ For numeric input, this is the absolute value. See the
888
+ section on the identity filter for the implications of this
889
+ definition for numeric input.
890
+
891
+ To compute the absolute value of a number as a floating point number, you may wish use `fabs`.
892
+
893
+ examples:
894
+ - program: 'map(abs)'
895
+ input: '[-10, -1.1, -1e-1]'
896
+ output: ['[10,1.1,1e-1]']
897
+
898
+ - title: "`length`"
899
+ body: |
900
+
901
+ The builtin function `length` gets the length of various
902
+ different types of value:
903
+
904
+ - The length of a **string** is the number of Unicode
905
+ codepoints it contains (which will be the same as its
906
+ JSON-encoded length in bytes if it's pure ASCII).
907
+
908
+ - The length of a **number** is its absolute value.
909
+
910
+ - The length of an **array** is the number of elements.
911
+
912
+ - The length of an **object** is the number of key-value pairs.
913
+
914
+ - The length of **null** is zero.
915
+
916
+ - It is an error to use `length` on a **boolean**.
917
+
918
+ examples:
919
+ - program: '.[] | length'
920
+ input: '[[1,2], "string", {"a":2}, null, -5]'
921
+ output: ['2', '6', '1', '0', '5']
922
+
923
+
924
+ - title: "`utf8bytelength`"
925
+ body: |
926
+
927
+ The builtin function `utf8bytelength` outputs the number of
928
+ bytes used to encode a string in UTF-8.
929
+
930
+ examples:
931
+ - program: 'utf8bytelength'
932
+ input: '"\u03bc"'
933
+ output: ['2']
934
+
935
+ - title: "`keys`, `keys_unsorted`"
936
+ body: |
937
+
938
+ The builtin function `keys`, when given an object, returns
939
+ its keys in an array.
940
+
941
+ The keys are sorted "alphabetically", by unicode codepoint
942
+ order. This is not an order that makes particular sense in
943
+ any particular language, but you can count on it being the
944
+ same for any two objects with the same set of keys,
945
+ regardless of locale settings.
946
+
947
+ When `keys` is given an array, it returns the valid indices
948
+ for that array: the integers from 0 to length-1.
949
+
950
+ The `keys_unsorted` function is just like `keys`, but if
951
+ the input is an object then the keys will not be sorted,
952
+ instead the keys will roughly be in insertion order.
953
+
954
+ examples:
955
+ - program: 'keys'
956
+ input: '{"abc": 1, "abcd": 2, "Foo": 3}'
957
+ output: ['["Foo", "abc", "abcd"]']
958
+ - program: 'keys'
959
+ input: '[42,3,35]'
960
+ output: ['[0,1,2]']
961
+
962
+ - title: "`has(key)`"
963
+ body: |
964
+
965
+ The builtin function `has` returns whether the input object
966
+ has the given key, or the input array has an element at the
967
+ given index.
968
+
969
+ `has($key)` has the same effect as checking whether `$key`
970
+ is a member of the array returned by `keys`, although `has`
971
+ will be faster.
972
+
973
+ examples:
974
+ - program: 'map(has("foo"))'
975
+ input: '[{"foo": 42}, {}]'
976
+ output: ['[true, false]']
977
+ - program: 'map(has(2))'
978
+ input: '[[0,1], ["a","b","c"]]'
979
+ output: ['[false, true]']
980
+
981
+ - title: "`in`"
982
+ body: |
983
+
984
+ The builtin function `in` returns whether or not the input key is in the
985
+ given object, or the input index corresponds to an element
986
+ in the given array. It is, essentially, an inversed version
987
+ of `has`.
988
+
989
+ examples:
990
+ - program: '.[] | in({"foo": 42})'
991
+ input: '["foo", "bar"]'
992
+ output: ['true', 'false']
993
+ - program: 'map(in([0,1]))'
994
+ input: '[2, 0]'
995
+ output: ['[false, true]']
996
+
997
+ - title: "`map(f)`, `map_values(f)`"
998
+ body: |
999
+
1000
+ For any filter `f`, `map(f)` and `map_values(f)` apply `f`
1001
+ to each of the values in the input array or object, that is,
1002
+ to the values of `.[]`.
1003
+
1004
+ In the absence of errors, `map(f)` always outputs an array
1005
+ whereas `map_values(f)` outputs an array if given an array,
1006
+ or an object if given an object.
1007
+
1008
+ When the input to `map_values(f)` is an object, the output
1009
+ object has the same keys as the input object except for
1010
+ those keys whose values when piped to `f` produce no values
1011
+ at all.
1012
+
1013
+ The key difference between `map(f)` and `map_values(f)` is
1014
+ that the former simply forms an array from all the values of
1015
+ `($x|f)` for each value, `$x`, in the input array or object,
1016
+ but `map_values(f)` only uses `first($x|f)`.
1017
+
1018
+ Specifically, for object inputs, `map_values(f)` constructs
1019
+ the output object by examining in turn the value of
1020
+ `first(.[$k]|f)` for each key, `$k`, of the input. If this
1021
+ expression produces no values, then the corresponding key
1022
+ will be dropped; otherwise, the output object will have that
1023
+ value at the key, `$k`.
1024
+
1025
+ Here are some examples to clarify the behavior of `map` and
1026
+ `map_values` when applied to arrays. These examples assume the
1027
+ input is `[1]` in all cases:
1028
+
1029
+ map(.+1) #=> [2]
1030
+ map(., .) #=> [1,1]
1031
+ map(empty) #=> []
1032
+
1033
+ map_values(.+1) #=> [2]
1034
+ map_values(., .) #=> [1]
1035
+ map_values(empty) #=> []
1036
+
1037
+ `map(f)` is equivalent to `[.[] | f]` and
1038
+ `map_values(f)` is equivalent to `.[] |= f`.
1039
+
1040
+ In fact, these are their implementations.
1041
+
1042
+
1043
+ examples:
1044
+ - program: 'map(.+1)'
1045
+ input: '[1,2,3]'
1046
+ output: ['[2,3,4]']
1047
+
1048
+ - program: 'map_values(.+1)'
1049
+ input: '{"a": 1, "b": 2, "c": 3}'
1050
+ output: ['{"a": 2, "b": 3, "c": 4}']
1051
+
1052
+ - program: 'map(., .)'
1053
+ input: '[1,2]'
1054
+ output: ['[1,1,2,2]']
1055
+
1056
+ - program: 'map_values(. // empty)'
1057
+ input: '{"a": null, "b": true, "c": false}'
1058
+ output: ['{"b":true}']
1059
+
1060
+
1061
+ - title: "`pick(pathexps)`"
1062
+ body: |
1063
+
1064
+ Emit the projection of the input object or array defined by the
1065
+ specified sequence of path expressions, such that if `p` is any
1066
+ one of these specifications, then `(. | p)` will evaluate to the
1067
+ same value as `(. | pick(pathexps) | p)`. For arrays, negative
1068
+ indices and `.[m:n]` specifications should not be used.
1069
+
1070
+ examples:
1071
+ - program: 'pick(.a, .b.c, .x)'
1072
+ input: '{"a": 1, "b": {"c": 2, "d": 3}, "e": 4}'
1073
+ output: ['{"a":1,"b":{"c":2},"x":null}']
1074
+
1075
+ - program: 'pick(.[2], .[0], .[0])'
1076
+ input: '[1,2,3,4]'
1077
+ output: ['[1,null,3]']
1078
+
1079
+
1080
+ - title: "`path(path_expression)`"
1081
+ body: |
1082
+
1083
+ Outputs array representations of the given path expression
1084
+ in `.`. The outputs are arrays of strings (object keys)
1085
+ and/or numbers (array indices).
1086
+
1087
+ Path expressions are jq expressions like `.a`, but also `.[]`.
1088
+ There are two types of path expressions: ones that can match
1089
+ exactly, and ones that cannot. For example, `.a.b.c` is an
1090
+ exact match path expression, while `.a[].b` is not.
1091
+
1092
+ `path(exact_path_expression)` will produce the array
1093
+ representation of the path expression even if it does not
1094
+ exist in `.`, if `.` is `null` or an array or an object.
1095
+
1096
+ `path(pattern)` will produce array representations of the
1097
+ paths matching `pattern` if the paths exist in `.`.
1098
+
1099
+ Note that the path expressions are not different from normal
1100
+ expressions. The expression
1101
+ `path(..|select(type=="boolean"))` outputs all the paths to
1102
+ boolean values in `.`, and only those paths.
1103
+
1104
+ examples:
1105
+ - program: 'path(.a[0].b)'
1106
+ input: 'null'
1107
+ output: ['["a",0,"b"]']
1108
+ - program: '[path(..)]'
1109
+ input: '{"a":[{"b":1}]}'
1110
+ output: ['[[],["a"],["a",0],["a",0,"b"]]']
1111
+
1112
+ - title: "`del(path_expression)`"
1113
+ body: |
1114
+
1115
+ The builtin function `del` removes a key and its corresponding
1116
+ value from an object.
1117
+
1118
+ examples:
1119
+ - program: 'del(.foo)'
1120
+ input: '{"foo": 42, "bar": 9001, "baz": 42}'
1121
+ output: ['{"bar": 9001, "baz": 42}']
1122
+ - program: 'del(.[1, 2])'
1123
+ input: '["foo", "bar", "baz"]'
1124
+ output: ['["foo"]']
1125
+
1126
+ - title: "`getpath(PATHS)`"
1127
+ body: |
1128
+
1129
+ The builtin function `getpath` outputs the values in `.` found
1130
+ at each path in `PATHS`.
1131
+
1132
+ examples:
1133
+ - program: 'getpath(["a","b"])'
1134
+ input: 'null'
1135
+ output: ['null']
1136
+ - program: '[getpath(["a","b"], ["a","c"])]'
1137
+ input: '{"a":{"b":0, "c":1}}'
1138
+ output: ['[0, 1]']
1139
+
1140
+ - title: "`setpath(PATHS; VALUE)`"
1141
+ body: |
1142
+
1143
+ The builtin function `setpath` sets the `PATHS` in `.` to `VALUE`.
1144
+
1145
+ examples:
1146
+ - program: 'setpath(["a","b"]; 1)'
1147
+ input: 'null'
1148
+ output: ['{"a": {"b": 1}}']
1149
+ - program: 'setpath(["a","b"]; 1)'
1150
+ input: '{"a":{"b":0}}'
1151
+ output: ['{"a": {"b": 1}}']
1152
+ - program: 'setpath([0,"a"]; 1)'
1153
+ input: 'null'
1154
+ output: ['[{"a":1}]']
1155
+
1156
+ - title: "`delpaths(PATHS)`"
1157
+ body: |
1158
+
1159
+ The builtin function `delpaths` deletes the `PATHS` in `.`.
1160
+ `PATHS` must be an array of paths, where each path is an array
1161
+ of strings and numbers.
1162
+
1163
+ examples:
1164
+ - program: 'delpaths([["a","b"]])'
1165
+ input: '{"a":{"b":1},"x":{"y":2}}'
1166
+ output: ['{"a":{},"x":{"y":2}}']
1167
+
1168
+ - title: "`to_entries`, `from_entries`, `with_entries(f)`"
1169
+ body: |
1170
+
1171
+ These functions convert between an object and an array of
1172
+ key-value pairs. If `to_entries` is passed an object, then
1173
+ for each `k: v` entry in the input, the output array
1174
+ includes `{"key": k, "value": v}`.
1175
+
1176
+ `from_entries` does the opposite conversion, and `with_entries(f)`
1177
+ is a shorthand for `to_entries | map(f) | from_entries`, useful for
1178
+ doing some operation to all keys and values of an object.
1179
+ `from_entries` accepts `"key"`, `"Key"`, `"name"`, `"Name"`,
1180
+ `"value"`, and `"Value"` as keys.
1181
+
1182
+ examples:
1183
+ - program: 'to_entries'
1184
+ input: '{"a": 1, "b": 2}'
1185
+ output: ['[{"key":"a", "value":1}, {"key":"b", "value":2}]']
1186
+ - program: 'from_entries'
1187
+ input: '[{"key":"a", "value":1}, {"key":"b", "value":2}]'
1188
+ output: ['{"a": 1, "b": 2}']
1189
+ - program: 'with_entries(.key |= "KEY_" + .)'
1190
+ input: '{"a": 1, "b": 2}'
1191
+ output: ['{"KEY_a": 1, "KEY_b": 2}']
1192
+
1193
+
1194
+ - title: "`select(boolean_expression)`"
1195
+ body: |
1196
+
1197
+ The function `select(f)` produces its input unchanged if
1198
+ `f` returns true for that input, and produces no output
1199
+ otherwise.
1200
+
1201
+ It's useful for filtering lists: `[1,2,3] | map(select(. >= 2))`
1202
+ will give you `[2,3]`.
1203
+
1204
+ examples:
1205
+ - program: 'map(select(. >= 2))'
1206
+ input: '[1,5,3,0,7]'
1207
+ output: ['[5,3,7]']
1208
+ - program: '.[] | select(.id == "second")'
1209
+ input: '[{"id": "first", "val": 1}, {"id": "second", "val": 2}]'
1210
+ output: ['{"id": "second", "val": 2}']
1211
+
1212
+
1213
+ - title: "`arrays`, `objects`, `iterables`, `booleans`, `numbers`, `normals`, `finites`, `strings`, `nulls`, `values`, `scalars`"
1214
+ body: |
1215
+
1216
+ These built-ins select only inputs that are arrays, objects,
1217
+ iterables (arrays or objects), booleans, numbers, normal
1218
+ numbers, finite numbers, strings, null, non-null values, and
1219
+ non-iterables, respectively.
1220
+
1221
+ examples:
1222
+ - program: '.[]|numbers'
1223
+ input: '[[],{},1,"foo",null,true,false]'
1224
+ output: ['1']
1225
+
1226
+ - title: "`empty`"
1227
+ body: |
1228
+
1229
+ `empty` returns no results. None at all. Not even `null`.
1230
+
1231
+ It's useful on occasion. You'll know if you need it :)
1232
+
1233
+ examples:
1234
+ - program: '1, empty, 2'
1235
+ input: 'null'
1236
+ output: ['1', '2']
1237
+ - program: '[1,2,empty,3]'
1238
+ input: 'null'
1239
+ output: ['[1,2,3]']
1240
+
1241
+ - title: "`error`, `error(message)`"
1242
+ body: |
1243
+
1244
+ Produces an error with the input value, or with the message
1245
+ given as the argument. Errors can be caught with try/catch;
1246
+ see below.
1247
+
1248
+ examples:
1249
+ - program: 'try error catch .'
1250
+ input: '"error message"'
1251
+ output: ['"error message"']
1252
+
1253
+ - program: 'try error("invalid value: \(.)") catch .'
1254
+ input: '42'
1255
+ output: ['"invalid value: 42"']
1256
+
1257
+ - title: "`halt`"
1258
+ body: |
1259
+
1260
+ Stops the jq program with no further outputs. jq will exit
1261
+ with exit status `0`.
1262
+
1263
+ - title: "`halt_error`, `halt_error(exit_code)`"
1264
+ body: |
1265
+
1266
+ Stops the jq program with no further outputs. The input will
1267
+ be printed on `stderr` as raw output (i.e., strings will not
1268
+ have double quotes) with no decoration, not even a newline.
1269
+
1270
+ The given `exit_code` (defaulting to `5`) will be jq's exit
1271
+ status.
1272
+
1273
+ For example, `"Error: something went wrong\n"|halt_error(1)`.
1274
+
1275
+ - title: "`$__loc__`"
1276
+ body: |
1277
+
1278
+ Produces an object with a "file" key and a "line" key, with
1279
+ the filename and line number where `$__loc__` occurs, as
1280
+ values.
1281
+
1282
+ examples:
1283
+ - program: 'try error("\($__loc__)") catch .'
1284
+ input: 'null'
1285
+ output: ['"{\"file\":\"<top-level>\",\"line\":1}"']
1286
+
1287
+ - title: "`paths`, `paths(node_filter)`"
1288
+ body: |
1289
+
1290
+ `paths` outputs the paths to all the elements in its input
1291
+ (except it does not output the empty list, representing .
1292
+ itself).
1293
+
1294
+ `paths(f)` outputs the paths to any values for which `f` is `true`.
1295
+ That is, `paths(type == "number")` outputs the paths to all numeric
1296
+ values.
1297
+
1298
+ examples:
1299
+ - program: '[paths]'
1300
+ input: '[1,[[],{"a":2}]]'
1301
+ output: ['[[0],[1],[1,0],[1,1],[1,1,"a"]]']
1302
+ - program: '[paths(type == "number")]'
1303
+ input: '[1,[[],{"a":2}]]'
1304
+ output: ['[[0],[1,1,"a"]]']
1305
+
1306
+ - title: "`add`, `add(generator)`"
1307
+ body: |
1308
+
1309
+ The filter `add` takes as input an array, and produces as
1310
+ output the elements of the array added together. This might
1311
+ mean summed, concatenated or merged depending on the types
1312
+ of the elements of the input array - the rules are the same
1313
+ as those for the `+` operator (described above).
1314
+
1315
+ If the input is an empty array, `add` returns `null`.
1316
+
1317
+ `add(generator)` operates on the given generator rather than
1318
+ the input.
1319
+
1320
+ examples:
1321
+ - program: add
1322
+ input: '["a","b","c"]'
1323
+ output: ['"abc"']
1324
+ - program: add
1325
+ input: '[1, 2, 3]'
1326
+ output: ['6']
1327
+ - program: add
1328
+ input: '[]'
1329
+ output: ["null"]
1330
+ - program: add(.[].a)
1331
+ input: '[{"a":3}, {"a":5}, {"b":6}]'
1332
+ output: ['8']
1333
+
1334
+ - title: "`any`, `any(condition)`, `any(generator; condition)`"
1335
+ body: |
1336
+
1337
+ The filter `any` takes as input an array of boolean values,
1338
+ and produces `true` as output if any of the elements of
1339
+ the array are `true`.
1340
+
1341
+ If the input is an empty array, `any` returns `false`.
1342
+
1343
+ The `any(condition)` form applies the given condition to the
1344
+ elements of the input array.
1345
+
1346
+ The `any(generator; condition)` form applies the given
1347
+ condition to all the outputs of the given generator.
1348
+
1349
+ examples:
1350
+ - program: any
1351
+ input: '[true, false]'
1352
+ output: ["true"]
1353
+ - program: any
1354
+ input: '[false, false]'
1355
+ output: ["false"]
1356
+ - program: any
1357
+ input: '[]'
1358
+ output: ["false"]
1359
+
1360
+ - title: "`all`, `all(condition)`, `all(generator; condition)`"
1361
+ body: |
1362
+
1363
+ The filter `all` takes as input an array of boolean values,
1364
+ and produces `true` as output if all of the elements of
1365
+ the array are `true`.
1366
+
1367
+ The `all(condition)` form applies the given condition to the
1368
+ elements of the input array.
1369
+
1370
+ The `all(generator; condition)` form applies the given
1371
+ condition to all the outputs of the given generator.
1372
+
1373
+ If the input is an empty array, `all` returns `true`.
1374
+
1375
+ examples:
1376
+ - program: all
1377
+ input: '[true, false]'
1378
+ output: ["false"]
1379
+ - program: all
1380
+ input: '[true, true]'
1381
+ output: ["true"]
1382
+ - program: all
1383
+ input: '[]'
1384
+ output: ["true"]
1385
+
1386
+ - title: "`flatten`, `flatten(depth)`"
1387
+ body: |
1388
+
1389
+ The filter `flatten` takes as input an array of nested arrays,
1390
+ and produces a flat array in which all arrays inside the original
1391
+ array have been recursively replaced by their values. You can pass
1392
+ an argument to it to specify how many levels of nesting to flatten.
1393
+
1394
+ `flatten(2)` is like `flatten`, but going only up to two
1395
+ levels deep.
1396
+
1397
+ examples:
1398
+ - program: flatten
1399
+ input: '[1, [2], [[3]]]'
1400
+ output: ["[1, 2, 3]"]
1401
+ - program: flatten(1)
1402
+ input: '[1, [2], [[3]]]'
1403
+ output: ["[1, 2, [3]]"]
1404
+ - program: flatten
1405
+ input: '[[]]'
1406
+ output: ["[]"]
1407
+ - program: flatten
1408
+ input: '[{"foo": "bar"}, [{"foo": "baz"}]]'
1409
+ output: ['[{"foo": "bar"}, {"foo": "baz"}]']
1410
+
1411
+ - title: "`range(upto)`, `range(from; upto)`, `range(from; upto; by)`"
1412
+ body: |
1413
+
1414
+ The `range` function produces a range of numbers. `range(4; 10)`
1415
+ produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers
1416
+ are produced as separate outputs. Use `[range(4; 10)]` to get a range as
1417
+ an array.
1418
+
1419
+ The one argument form generates numbers from 0 to the given
1420
+ number, with an increment of 1.
1421
+
1422
+ The two argument form generates numbers from `from` to `upto`
1423
+ with an increment of 1.
1424
+
1425
+ The three argument form generates numbers `from` to `upto`
1426
+ with an increment of `by`.
1427
+
1428
+ examples:
1429
+ - program: 'range(2; 4)'
1430
+ input: 'null'
1431
+ output: ['2', '3']
1432
+ - program: '[range(2; 4)]'
1433
+ input: 'null'
1434
+ output: ['[2,3]']
1435
+ - program: '[range(4)]'
1436
+ input: 'null'
1437
+ output: ['[0,1,2,3]']
1438
+ - program: '[range(0; 10; 3)]'
1439
+ input: 'null'
1440
+ output: ['[0,3,6,9]']
1441
+ - program: '[range(0; 10; -1)]'
1442
+ input: 'null'
1443
+ output: ['[]']
1444
+ - program: '[range(0; -5; -1)]'
1445
+ input: 'null'
1446
+ output: ['[0,-1,-2,-3,-4]']
1447
+
1448
+ - title: "`floor`"
1449
+ body: |
1450
+
1451
+ The `floor` function returns the floor of its numeric input.
1452
+
1453
+ examples:
1454
+ - program: 'floor'
1455
+ input: '3.14159'
1456
+ output: ['3']
1457
+
1458
+ - title: "`sqrt`"
1459
+ body: |
1460
+
1461
+ The `sqrt` function returns the square root of its numeric input.
1462
+
1463
+ examples:
1464
+ - program: 'sqrt'
1465
+ input: '9'
1466
+ output: ['3']
1467
+
1468
+ - title: "`tonumber`"
1469
+ body: |
1470
+
1471
+ The `tonumber` function parses its input as a number. It
1472
+ will convert correctly-formatted strings to their numeric
1473
+ equivalent, leave numbers alone, and give an error on all other input.
1474
+
1475
+ examples:
1476
+ - program: '.[] | tonumber'
1477
+ input: '[1, "1"]'
1478
+ output: ['1', '1']
1479
+
1480
+ - title: "`toboolean`"
1481
+ body: |
1482
+
1483
+ The `toboolean` function parses its input as a boolean. It
1484
+ will convert correctly-formatted strings to their boolean
1485
+ equivalent, leave booleans alone, and give an error on all other input.
1486
+
1487
+ examples:
1488
+ - program: '.[] | toboolean'
1489
+ input: '["true", "false", true, false]'
1490
+ output: ['true', 'false', 'true', 'false']
1491
+
1492
+ - title: "`tostring`"
1493
+ body: |
1494
+
1495
+ The `tostring` function prints its input as a
1496
+ string. Strings are left unchanged, and all other values are
1497
+ JSON-encoded.
1498
+
1499
+ examples:
1500
+ - program: '.[] | tostring'
1501
+ input: '[1, "1", [1]]'
1502
+ output: ['"1"', '"1"', '"[1]"']
1503
+
1504
+ - title: "`type`"
1505
+ body: |
1506
+
1507
+ The `type` function returns the type of its argument as a
1508
+ string, which is one of null, boolean, number, string, array
1509
+ or object.
1510
+
1511
+ examples:
1512
+ - program: 'map(type)'
1513
+ input: '[0, false, [], {}, null, "hello"]'
1514
+ output: ['["number", "boolean", "array", "object", "null", "string"]']
1515
+
1516
+ - title: "`infinite`, `nan`, `isinfinite`, `isnan`, `isfinite`, `isnormal`"
1517
+ body: |
1518
+
1519
+ Some arithmetic operations can yield infinities and "not a
1520
+ number" (NaN) values. The `isinfinite` builtin returns `true`
1521
+ if its input is infinite. The `isnan` builtin returns `true`
1522
+ if its input is a NaN. The `infinite` builtin returns a
1523
+ positive infinite value. The `nan` builtin returns a NaN.
1524
+ The `isnormal` builtin returns true if its input is a normal
1525
+ number.
1526
+
1527
+ Note that division by zero raises an error.
1528
+
1529
+ Currently most arithmetic operations operating on infinities,
1530
+ NaNs, and sub-normals do not raise errors.
1531
+
1532
+ examples:
1533
+ - program: '.[] | (infinite * .) < 0'
1534
+ input: '[-1, 1]'
1535
+ output: ['true', 'false']
1536
+ - program: 'infinite, nan | type'
1537
+ input: 'null'
1538
+ output: ['"number"', '"number"']
1539
+
1540
+ - title: "`sort`, `sort_by(path_expression)`"
1541
+ body: |
1542
+
1543
+ The `sort` functions sorts its input, which must be an
1544
+ array. Values are sorted in the following order:
1545
+
1546
+ * `null`
1547
+ * `false`
1548
+ * `true`
1549
+ * numbers
1550
+ * strings, in alphabetical order (by unicode codepoint value)
1551
+ * arrays, in lexical order
1552
+ * objects
1553
+
1554
+ The ordering for objects is a little complex: first they're
1555
+ compared by comparing their sets of keys (as arrays in
1556
+ sorted order), and if their keys are equal then the values
1557
+ are compared key by key.
1558
+
1559
+ `sort_by` may be used to sort by a particular field of an
1560
+ object, or by applying any jq filter. `sort_by(f)` compares
1561
+ two elements by comparing the result of `f` on each element.
1562
+ When `f` produces multiple values, it firstly compares the
1563
+ first values, and the second values if the first values are
1564
+ equal, and so on.
1565
+
1566
+ examples:
1567
+ - program: 'sort'
1568
+ input: '[8,3,null,6]'
1569
+ output: ['[null,3,6,8]']
1570
+
1571
+ - program: 'sort_by(.foo)'
1572
+ input: '[{"foo":4, "bar":10}, {"foo":3, "bar":10}, {"foo":2, "bar":1}]'
1573
+ output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":4, "bar":10}]']
1574
+
1575
+ - program: 'sort_by(.foo, .bar)'
1576
+ input: '[{"foo":4, "bar":10}, {"foo":3, "bar":20}, {"foo":2, "bar":1}, {"foo":3, "bar":10}]'
1577
+ output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":3, "bar":20}, {"foo":4, "bar":10}]']
1578
+
1579
+ - title: "`group_by(path_expression)`"
1580
+ body: |
1581
+
1582
+ `group_by(.foo)` takes as input an array, groups the
1583
+ elements having the same `.foo` field into separate arrays,
1584
+ and produces all of these arrays as elements of a larger
1585
+ array, sorted by the value of the `.foo` field.
1586
+
1587
+ Any jq expression, not just a field access, may be used in
1588
+ place of `.foo`. The sorting order is the same as described
1589
+ in the `sort` function above.
1590
+
1591
+ examples:
1592
+ - program: 'group_by(.foo)'
1593
+ input: '[{"foo":1, "bar":10}, {"foo":3, "bar":100}, {"foo":1, "bar":1}]'
1594
+ output: ['[[{"foo":1, "bar":10}, {"foo":1, "bar":1}], [{"foo":3, "bar":100}]]']
1595
+
1596
+ - title: "`min`, `max`, `min_by(path_exp)`, `max_by(path_exp)`"
1597
+ body: |
1598
+
1599
+ Find the minimum or maximum element of the input array.
1600
+
1601
+ The `min_by(path_exp)` and `max_by(path_exp)` functions allow
1602
+ you to specify a particular field or property to examine, e.g.
1603
+ `min_by(.foo)` finds the object with the smallest `foo` field.
1604
+
1605
+ examples:
1606
+ - program: 'min'
1607
+ input: '[5,4,2,7]'
1608
+ output: ['2']
1609
+ - program: 'max_by(.foo)'
1610
+ input: '[{"foo":1, "bar":14}, {"foo":2, "bar":3}]'
1611
+ output: ['{"foo":2, "bar":3}']
1612
+
1613
+ - title: "`unique`, `unique_by(path_exp)`"
1614
+ body: |
1615
+
1616
+ The `unique` function takes as input an array and produces
1617
+ an array of the same elements, in sorted order, with
1618
+ duplicates removed.
1619
+
1620
+ The `unique_by(path_exp)` function will keep only one element
1621
+ for each value obtained by applying the argument. Think of it
1622
+ as making an array by taking one element out of every group
1623
+ produced by `group`.
1624
+
1625
+ examples:
1626
+ - program: 'unique'
1627
+ input: '[1,2,5,3,5,3,1,3]'
1628
+ output: ['[1,2,3,5]']
1629
+ - program: 'unique_by(.foo)'
1630
+ input: '[{"foo": 1, "bar": 2}, {"foo": 1, "bar": 3}, {"foo": 4, "bar": 5}]'
1631
+ output: ['[{"foo": 1, "bar": 2}, {"foo": 4, "bar": 5}]']
1632
+ - program: 'unique_by(length)'
1633
+ input: '["chunky", "bacon", "kitten", "cicada", "asparagus"]'
1634
+ output: ['["bacon", "chunky", "asparagus"]']
1635
+
1636
+ - title: "`reverse`"
1637
+ body: |
1638
+
1639
+ This function reverses an array.
1640
+
1641
+ examples:
1642
+ - program: 'reverse'
1643
+ input: '[1,2,3,4]'
1644
+ output: ['[4,3,2,1]']
1645
+
1646
+ - title: "`contains(element)`"
1647
+ body: |
1648
+
1649
+ The filter `contains(b)` will produce true if b is
1650
+ completely contained within the input. A string B is
1651
+ contained in a string A if B is a substring of A. An array B
1652
+ is contained in an array A if all elements in B are
1653
+ contained in any element in A. An object B is contained in
1654
+ object A if all of the values in B are contained in the
1655
+ value in A with the same key. All other types are assumed to
1656
+ be contained in each other if they are equal.
1657
+
1658
+ examples:
1659
+ - program: 'contains("bar")'
1660
+ input: '"foobar"'
1661
+ output: ['true']
1662
+ - program: 'contains(["baz", "bar"])'
1663
+ input: '["foobar", "foobaz", "blarp"]'
1664
+ output: ['true']
1665
+ - program: 'contains(["bazzzzz", "bar"])'
1666
+ input: '["foobar", "foobaz", "blarp"]'
1667
+ output: ['false']
1668
+ - program: 'contains({foo: 12, bar: [{barp: 12}]})'
1669
+ input: '{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}'
1670
+ output: ['true']
1671
+ - program: 'contains({foo: 12, bar: [{barp: 15}]})'
1672
+ input: '{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}'
1673
+ output: ['false']
1674
+
1675
+ - title: "`indices(s)`"
1676
+ body: |
1677
+
1678
+ Outputs an array containing the indices in `.` where `s`
1679
+ occurs. The input may be an array, in which case if `s` is an
1680
+ array then the indices output will be those where all elements
1681
+ in `.` match those of `s`.
1682
+
1683
+ examples:
1684
+ - program: 'indices(", ")'
1685
+ input: '"a,b, cd, efg, hijk"'
1686
+ output: ['[3,7,12]']
1687
+ - program: 'indices(1)'
1688
+ input: '[0,1,2,1,3,1,4]'
1689
+ output: ['[1,3,5]']
1690
+ - program: 'indices([1,2])'
1691
+ input: '[0,1,2,3,1,4,2,5,1,2,6,7]'
1692
+ output: ['[1,8]']
1693
+
1694
+ - title: "`index(s)`, `rindex(s)`"
1695
+ body: |
1696
+
1697
+ Outputs the index of the first (`index`) or last (`rindex`)
1698
+ occurrence of `s` in the input.
1699
+
1700
+ examples:
1701
+ - program: 'index(", ")'
1702
+ input: '"a,b, cd, efg, hijk"'
1703
+ output: ['3']
1704
+ - program: 'index(1)'
1705
+ input: '[0,1,2,1,3,1,4]'
1706
+ output: ['1']
1707
+ - program: 'index([1,2])'
1708
+ input: '[0,1,2,3,1,4,2,5,1,2,6,7]'
1709
+ output: ['1']
1710
+ - program: 'rindex(", ")'
1711
+ input: '"a,b, cd, efg, hijk"'
1712
+ output: ['12']
1713
+ - program: 'rindex(1)'
1714
+ input: '[0,1,2,1,3,1,4]'
1715
+ output: ['5']
1716
+ - program: 'rindex([1,2])'
1717
+ input: '[0,1,2,3,1,4,2,5,1,2,6,7]'
1718
+ output: ['8']
1719
+
1720
+ - title: "`inside`"
1721
+ body: |
1722
+
1723
+ The filter `inside(b)` will produce true if the input is
1724
+ completely contained within b. It is, essentially, an
1725
+ inversed version of `contains`.
1726
+
1727
+ examples:
1728
+ - program: 'inside("foobar")'
1729
+ input: '"bar"'
1730
+ output: ['true']
1731
+ - program: 'inside(["foobar", "foobaz", "blarp"])'
1732
+ input: '["baz", "bar"]'
1733
+ output: ['true']
1734
+ - program: 'inside(["foobar", "foobaz", "blarp"])'
1735
+ input: '["bazzzzz", "bar"]'
1736
+ output: ['false']
1737
+ - program: 'inside({"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]})'
1738
+ input: '{"foo": 12, "bar": [{"barp": 12}]}'
1739
+ output: ['true']
1740
+ - program: 'inside({"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]})'
1741
+ input: '{"foo": 12, "bar": [{"barp": 15}]}'
1742
+ output: ['false']
1743
+
1744
+ - title: "`startswith(str)`"
1745
+ body: |
1746
+
1747
+ Outputs `true` if . starts with the given string argument.
1748
+
1749
+ examples:
1750
+ - program: '[.[]|startswith("foo")]'
1751
+ input: '["fo", "foo", "barfoo", "foobar", "barfoob"]'
1752
+ output: ['[false, true, false, true, false]']
1753
+
1754
+ - title: "`endswith(str)`"
1755
+ body: |
1756
+
1757
+ Outputs `true` if . ends with the given string argument.
1758
+
1759
+ examples:
1760
+ - program: '[.[]|endswith("foo")]'
1761
+ input: '["foobar", "barfoo"]'
1762
+ output: ['[false, true]']
1763
+
1764
+ - title: "`combinations`, `combinations(n)`"
1765
+ body: |
1766
+
1767
+ Outputs all combinations of the elements of the arrays in the
1768
+ input array. If given an argument `n`, it outputs all combinations
1769
+ of `n` repetitions of the input array.
1770
+
1771
+ examples:
1772
+ - program: 'combinations'
1773
+ input: '[[1,2], [3, 4]]'
1774
+ output: ['[1, 3]', '[1, 4]', '[2, 3]', '[2, 4]']
1775
+ - program: 'combinations(2)'
1776
+ input: '[0, 1]'
1777
+ output: ['[0, 0]', '[0, 1]', '[1, 0]', '[1, 1]']
1778
+
1779
+ - title: "`ltrimstr(str)`"
1780
+ body: |
1781
+
1782
+ Outputs its input with the given prefix string removed, if it
1783
+ starts with it.
1784
+
1785
+ examples:
1786
+ - program: '[.[]|ltrimstr("foo")]'
1787
+ input: '["fo", "foo", "barfoo", "foobar", "afoo"]'
1788
+ output: ['["fo","","barfoo","bar","afoo"]']
1789
+
1790
+ - title: "`rtrimstr(str)`"
1791
+ body: |
1792
+
1793
+ Outputs its input with the given suffix string removed, if it
1794
+ ends with it.
1795
+
1796
+ examples:
1797
+ - program: '[.[]|rtrimstr("foo")]'
1798
+ input: '["fo", "foo", "barfoo", "foobar", "foob"]'
1799
+ output: ['["fo","","bar","foobar","foob"]']
1800
+
1801
+ - title: "`trimstr(str)`"
1802
+ body: |
1803
+
1804
+ Outputs its input with the given string removed at both ends, if it
1805
+ starts or ends with it.
1806
+
1807
+ examples:
1808
+ - program: '[.[]|trimstr("foo")]'
1809
+ input: '["fo", "foo", "barfoo", "foobarfoo", "foob"]'
1810
+ output: ['["fo","","bar","bar","b"]']
1811
+
1812
+ - title: "`trim`, `ltrim`, `rtrim`"
1813
+ body: |
1814
+
1815
+ `trim` trims both leading and trailing whitespace.
1816
+
1817
+ `ltrim` trims only leading (left side) whitespace.
1818
+
1819
+ `rtrim` trims only trailing (right side) whitespace.
1820
+
1821
+ Whitespace characters are the usual `" "`, `"\n"` `"\t"`, `"\r"`
1822
+ and also all characters in the Unicode character database with the
1823
+ whitespace property. Note that what considers whitespace might
1824
+ change in the future.
1825
+
1826
+ examples:
1827
+ - program: 'trim, ltrim, rtrim'
1828
+ input: '" abc "'
1829
+ output: ['"abc"', '"abc "', '" abc"']
1830
+
1831
+ - title: "`explode`"
1832
+ body: |
1833
+
1834
+ Converts an input string into an array of the string's
1835
+ codepoint numbers.
1836
+
1837
+ examples:
1838
+ - program: 'explode'
1839
+ input: '"foobar"'
1840
+ output: ['[102,111,111,98,97,114]']
1841
+
1842
+ - title: "`implode`"
1843
+ body: |
1844
+
1845
+ The inverse of explode.
1846
+
1847
+ examples:
1848
+ - program: 'implode'
1849
+ input: '[65, 66, 67]'
1850
+ output: ['"ABC"']
1851
+
1852
+ - title: "`split(str)`"
1853
+ body: |
1854
+
1855
+ Splits an input string on the separator argument.
1856
+
1857
+ `split` can also split on regex matches when called with
1858
+ two arguments (see the regular expressions section below).
1859
+
1860
+ examples:
1861
+ - program: 'split(", ")'
1862
+ input: '"a, b,c,d, e, "'
1863
+ output: ['["a","b,c,d","e",""]']
1864
+
1865
+ - title: "`join(str)`"
1866
+ body: |
1867
+
1868
+ Joins the array of elements given as input, using the
1869
+ argument as separator. It is the inverse of `split`: that is,
1870
+ running `split("foo") | join("foo")` over any input string
1871
+ returns said input string.
1872
+
1873
+ Numbers and booleans in the input are converted to strings.
1874
+ Null values are treated as empty strings. Arrays and objects
1875
+ in the input are not supported.
1876
+
1877
+ examples:
1878
+ - program: 'join(", ")'
1879
+ input: '["a","b,c,d","e"]'
1880
+ output: ['"a, b,c,d, e"']
1881
+ - program: 'join(" ")'
1882
+ input: '["a",1,2.3,true,null,false]'
1883
+ output: ['"a 1 2.3 true false"']
1884
+
1885
+ - title: "`ascii_downcase`, `ascii_upcase`"
1886
+ body: |
1887
+
1888
+ Emit a copy of the input string with its alphabetic characters (a-z and A-Z)
1889
+ converted to the specified case.
1890
+
1891
+ examples:
1892
+ - program: 'ascii_upcase'
1893
+ input: '"useful but not for é"'
1894
+ output: ['"USEFUL BUT NOT FOR é"']
1895
+
1896
+ - title: "`while(cond; update)`"
1897
+ body: |
1898
+
1899
+ The `while(cond; update)` function allows you to repeatedly
1900
+ apply an update to `.` until `cond` is false.
1901
+
1902
+ Note that `while(cond; update)` is internally defined as a
1903
+ recursive jq function. Recursive calls within `while` will
1904
+ not consume additional memory if `update` produces at most one
1905
+ output for each input. See advanced topics below.
1906
+
1907
+ examples:
1908
+ - program: '[while(.<100; .*2)]'
1909
+ input: '1'
1910
+ output: ['[1,2,4,8,16,32,64]']
1911
+
1912
+ - title: "`repeat(exp)`"
1913
+ body: |
1914
+
1915
+ The `repeat(exp)` function allows you to repeatedly
1916
+ apply expression `exp` to `.` until an error is raised.
1917
+
1918
+ Note that `repeat(exp)` is internally defined as a
1919
+ recursive jq function. Recursive calls within `repeat` will
1920
+ not consume additional memory if `exp` produces at most one
1921
+ output for each input. See advanced topics below.
1922
+
1923
+ examples:
1924
+ - program: '[repeat(.*2, error)?]'
1925
+ input: '1'
1926
+ output: ['[2]']
1927
+
1928
+ - title: "`until(cond; next)`"
1929
+ body: |
1930
+
1931
+ The `until(cond; next)` function allows you to repeatedly
1932
+ apply the expression `next`, initially to `.` then to its own
1933
+ output, until `cond` is true. For example, this can be used
1934
+ to implement a factorial function (see below).
1935
+
1936
+ Note that `until(cond; next)` is internally defined as a
1937
+ recursive jq function. Recursive calls within `until()` will
1938
+ not consume additional memory if `next` produces at most one
1939
+ output for each input. See advanced topics below.
1940
+
1941
+ examples:
1942
+ - program: '[.,1]|until(.[0] < 1; [.[0] - 1, .[1] * .[0]])|.[1]'
1943
+ input: '4'
1944
+ output: ['24']
1945
+
1946
+
1947
+ - title: "`recurse(f)`, `recurse`, `recurse(f; condition)`"
1948
+ body: |
1949
+
1950
+ The `recurse(f)` function allows you to search through a
1951
+ recursive structure, and extract interesting data from all
1952
+ levels. Suppose your input represents a filesystem:
1953
+
1954
+ {"name": "/", "children": [
1955
+ {"name": "/bin", "children": [
1956
+ {"name": "/bin/ls", "children": []},
1957
+ {"name": "/bin/sh", "children": []}]},
1958
+ {"name": "/home", "children": [
1959
+ {"name": "/home/stephen", "children": [
1960
+ {"name": "/home/stephen/jq", "children": []}]}]}]}
1961
+
1962
+ Now suppose you want to extract all of the filenames
1963
+ present. You need to retrieve `.name`, `.children[].name`,
1964
+ `.children[].children[].name`, and so on. You can do this
1965
+ with:
1966
+
1967
+ recurse(.children[]) | .name
1968
+
1969
+ When called without an argument, `recurse` is equivalent to
1970
+ `recurse(.[]?)`.
1971
+
1972
+ `recurse(f)` is identical to `recurse(f; true)` and can be
1973
+ used without concerns about recursion depth.
1974
+
1975
+ `recurse(f; condition)` is a generator which begins by
1976
+ emitting . and then emits in turn .|f, .|f|f, .|f|f|f, ... so long
1977
+ as the computed value satisfies the condition. For example,
1978
+ to generate all the integers, at least in principle, one
1979
+ could write `recurse(.+1; true)`.
1980
+
1981
+ The recursive calls in `recurse` will not consume additional
1982
+ memory whenever `f` produces at most a single output for each
1983
+ input.
1984
+
1985
+ examples:
1986
+ - program: 'recurse(.foo[])'
1987
+ input: '{"foo":[{"foo": []}, {"foo":[{"foo":[]}]}]}'
1988
+ output:
1989
+ - '{"foo":[{"foo":[]},{"foo":[{"foo":[]}]}]}'
1990
+ - '{"foo":[]}'
1991
+ - '{"foo":[{"foo":[]}]}'
1992
+ - '{"foo":[]}'
1993
+
1994
+ - program: 'recurse'
1995
+ input: '{"a":0,"b":[1]}'
1996
+ output:
1997
+ - '{"a":0,"b":[1]}'
1998
+ - '0'
1999
+ - '[1]'
2000
+ - '1'
2001
+
2002
+ - program: 'recurse(. * .; . < 20)'
2003
+ input: '2'
2004
+ output: ['2', '4', '16']
2005
+
2006
+ - title: "`walk(f)`"
2007
+ body: |
2008
+
2009
+ The `walk(f)` function applies f recursively to every
2010
+ component of the input entity. When an array is
2011
+ encountered, f is first applied to its elements and then to
2012
+ the array itself; when an object is encountered, f is first
2013
+ applied to all the values and then to the object. In
2014
+ practice, f will usually test the type of its input, as
2015
+ illustrated in the following examples. The first example
2016
+ highlights the usefulness of processing the elements of an
2017
+ array of arrays before processing the array itself. The second
2018
+ example shows how all the keys of all the objects within the
2019
+ input can be considered for alteration.
2020
+
2021
+ examples:
2022
+ - program: 'walk(if type == "array" then sort else . end)'
2023
+ input: '[[4, 1, 7], [8, 5, 2], [3, 6, 9]]'
2024
+ output:
2025
+ - '[[1,4,7],[2,5,8],[3,6,9]]'
2026
+
2027
+ - program: 'walk( if type == "object" then with_entries( .key |= sub( "^_+"; "") ) else . end )'
2028
+ input: '[ { "_a": { "__b": 2 } } ]'
2029
+ output:
2030
+ - '[{"a":{"b":2}}]'
2031
+
2032
+ - title: "`have_literal_numbers`"
2033
+ body: |
2034
+
2035
+ This builtin returns true if jq's build configuration
2036
+ includes support for preservation of input number literals.
2037
+
2038
+ - title: "`have_decnum`"
2039
+ body: |
2040
+
2041
+ This builtin returns true if jq was built with "decnum",
2042
+ which is the current literal number preserving numeric
2043
+ backend implementation for jq.
2044
+
2045
+ - title: "`$JQ_BUILD_CONFIGURATION`"
2046
+ body: |
2047
+
2048
+ This builtin binding shows the jq executable's build
2049
+ configuration. Its value has no particular format, but
2050
+ it can be expected to be at least the `./configure`
2051
+ command-line arguments, and may be enriched in the
2052
+ future to include the version strings for the build
2053
+ tooling used.
2054
+
2055
+ Note that this can be overridden in the command-line
2056
+ with `--arg` and related options.
2057
+
2058
+ - title: "`$ENV`, `env`"
2059
+ body: |
2060
+
2061
+ `$ENV` is an object representing the environment variables as
2062
+ set when the jq program started.
2063
+
2064
+ `env` outputs an object representing jq's current environment.
2065
+
2066
+ At the moment there is no builtin for setting environment
2067
+ variables.
2068
+
2069
+ examples:
2070
+ - program: '$ENV.PAGER'
2071
+ input: 'null'
2072
+ output: ['"less"']
2073
+
2074
+ - program: 'env.PAGER'
2075
+ input: 'null'
2076
+ output: ['"less"']
2077
+
2078
+ - title: "`transpose`"
2079
+ body: |
2080
+
2081
+ Transpose a possibly jagged matrix (an array of arrays).
2082
+ Rows are padded with nulls so the result is always rectangular.
2083
+
2084
+ examples:
2085
+ - program: 'transpose'
2086
+ input: '[[1], [2,3]]'
2087
+ output: ['[[1,2],[null,3]]']
2088
+
2089
+ - title: "`bsearch(x)`"
2090
+ body: |
2091
+
2092
+ `bsearch(x)` conducts a binary search for x in the input
2093
+ array. If the input is sorted and contains x, then
2094
+ `bsearch(x)` will return its index in the array; otherwise, if
2095
+ the array is sorted, it will return (-1 - ix) where ix is an
2096
+ insertion point such that the array would still be sorted
2097
+ after the insertion of x at ix. If the array is not sorted,
2098
+ `bsearch(x)` will return an integer that is probably of no
2099
+ interest.
2100
+
2101
+ examples:
2102
+ - program: 'bsearch(0)'
2103
+ input: '[0,1]'
2104
+ output: ['0']
2105
+ - program: 'bsearch(0)'
2106
+ input: '[1,2,3]'
2107
+ output: ['-1']
2108
+ - program: 'bsearch(4) as $ix | if $ix < 0 then .[-(1+$ix)] = 4 else . end'
2109
+ input: '[1,2,3]'
2110
+ output: ['[1,2,3,4]']
2111
+
2112
+ - title: "String interpolation: `\\(exp)`"
2113
+ body: |
2114
+
2115
+ Inside a string, you can put an expression inside parens
2116
+ after a backslash. Whatever the expression returns will be
2117
+ interpolated into the string.
2118
+
2119
+ examples:
2120
+ - program: '"The input was \(.), which is one less than \(.+1)"'
2121
+ input: '42'
2122
+ output: ['"The input was 42, which is one less than 43"']
2123
+
2124
+ - title: "Convert to/from JSON"
2125
+ body: |
2126
+
2127
+ The `tojson` and `fromjson` builtins dump values as JSON texts
2128
+ or parse JSON texts into values, respectively. The `tojson`
2129
+ builtin differs from `tostring` in that `tostring` returns strings
2130
+ unmodified, while `tojson` encodes strings as JSON strings.
2131
+
2132
+ examples:
2133
+ - program: '[.[]|tostring]'
2134
+ input: '[1, "foo", ["foo"]]'
2135
+ output: ['["1","foo","[\"foo\"]"]']
2136
+ - program: '[.[]|tojson]'
2137
+ input: '[1, "foo", ["foo"]]'
2138
+ output: ['["1","\"foo\"","[\"foo\"]"]']
2139
+ - program: '[.[]|tojson|fromjson]'
2140
+ input: '[1, "foo", ["foo"]]'
2141
+ output: ['[1,"foo",["foo"]]']
2142
+
2143
+ - title: "Format strings and escaping"
2144
+ body: |
2145
+
2146
+ The `@foo` syntax is used to format and escape strings,
2147
+ which is useful for building URLs, documents in a language
2148
+ like HTML or XML, and so forth. `@foo` can be used as a
2149
+ filter on its own, the possible escapings are:
2150
+
2151
+ * `@text`:
2152
+
2153
+ Calls `tostring`, see that function for details.
2154
+
2155
+ * `@json`:
2156
+
2157
+ Serializes the input as JSON.
2158
+
2159
+ * `@html`:
2160
+
2161
+ Applies HTML/XML escaping, by mapping the characters
2162
+ `<>&'"` to their entity equivalents `&lt;`, `&gt;`,
2163
+ `&amp;`, `&apos;`, `&quot;`.
2164
+
2165
+ * `@uri`:
2166
+
2167
+ Applies percent-encoding, by mapping all reserved URI
2168
+ characters to a `%XX` sequence.
2169
+
2170
+ * `@urid`:
2171
+
2172
+ The inverse of `@uri`, applies percent-decoding, by mapping
2173
+ all `%XX` sequences to their corresponding URI characters.
2174
+
2175
+ * `@csv`:
2176
+
2177
+ The input must be an array, and it is rendered as CSV
2178
+ with double quotes for strings, and quotes escaped by
2179
+ repetition.
2180
+
2181
+ * `@tsv`:
2182
+
2183
+ The input must be an array, and it is rendered as TSV
2184
+ (tab-separated values). Each input array will be printed as
2185
+ a single line. Fields are separated by a single
2186
+ tab (ascii `0x09`). Input characters line-feed (ascii `0x0a`),
2187
+ carriage-return (ascii `0x0d`), tab (ascii `0x09`) and
2188
+ backslash (ascii `0x5c`) will be output as escape sequences
2189
+ `\n`, `\r`, `\t`, `\\` respectively.
2190
+
2191
+ * `@sh`:
2192
+
2193
+ The input is escaped suitable for use in a command-line
2194
+ for a POSIX shell. If the input is an array, the output
2195
+ will be a series of space-separated strings.
2196
+
2197
+ * `@base64`:
2198
+
2199
+ The input is converted to base64 as specified by RFC 4648.
2200
+
2201
+ * `@base64d`:
2202
+
2203
+ The inverse of `@base64`, input is decoded as specified by RFC 4648.
2204
+ Note\: If the decoded string is not UTF-8, the results are undefined.
2205
+
2206
+ This syntax can be combined with string interpolation in a
2207
+ useful way. You can follow a `@foo` token with a string
2208
+ literal. The contents of the string literal will *not* be
2209
+ escaped. However, all interpolations made inside that string
2210
+ literal will be escaped. For instance,
2211
+
2212
+ @uri "https://www.google.com/search?q=\(.search)"
2213
+
2214
+ will produce the following output for the input
2215
+ `{"search":"what is jq?"}`:
2216
+
2217
+ "https://www.google.com/search?q=what%20is%20jq%3F"
2218
+
2219
+ Note that the slashes, question mark, etc. in the URL are
2220
+ not escaped, as they were part of the string literal.
2221
+
2222
+ examples:
2223
+ - program: '@html'
2224
+ input: '"This works if x < y"'
2225
+ output: ['"This works if x &lt; y"']
2226
+
2227
+ - program: '@sh "echo \(.)"'
2228
+ input: "\"O'Hara's Ale\""
2229
+ output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]
2230
+
2231
+ - program: '@base64'
2232
+ input: '"This is a message"'
2233
+ output: ['"VGhpcyBpcyBhIG1lc3NhZ2U="']
2234
+
2235
+ - program: '@base64d'
2236
+ input: '"VGhpcyBpcyBhIG1lc3NhZ2U="'
2237
+ output: ['"This is a message"']
2238
+
2239
+ - title: "Dates"
2240
+ body: |
2241
+
2242
+ jq provides some basic date handling functionality, with some
2243
+ high-level and low-level builtins. In all cases these
2244
+ builtins deal exclusively with time in UTC.
2245
+
2246
+ The `fromdateiso8601` builtin parses datetimes in the ISO 8601
2247
+ format to a number of seconds since the Unix epoch
2248
+ (1970-01-01T00:00:00Z). The `todateiso8601` builtin does the
2249
+ inverse.
2250
+
2251
+ The `fromdate` builtin parses datetime strings. Currently
2252
+ `fromdate` only supports ISO 8601 datetime strings, but in the
2253
+ future it will attempt to parse datetime strings in more
2254
+ formats.
2255
+
2256
+ The `todate` builtin is an alias for `todateiso8601`.
2257
+
2258
+ The `now` builtin outputs the current time, in seconds since
2259
+ the Unix epoch.
2260
+
2261
+ Low-level jq interfaces to the C-library time functions are
2262
+ also provided: `strptime`, `strftime`, `strflocaltime`,
2263
+ `mktime`, `gmtime`, and `localtime`. Refer to your host
2264
+ operating system's documentation for the format strings used
2265
+ by `strptime` and `strftime`. Note: these are not necessarily
2266
+ stable interfaces in jq, particularly as to their localization
2267
+ functionality.
2268
+
2269
+ The `gmtime` builtin consumes a number of seconds since the
2270
+ Unix epoch and outputs a "broken down time" representation of
2271
+ Greenwich Mean Time as an array of numbers representing
2272
+ (in this order): the year, the month (zero-based), the day of
2273
+ the month (one-based), the hour of the day, the minute of the
2274
+ hour, the second of the minute, the day of the week, and the
2275
+ day of the year -- all one-based unless otherwise stated. The
2276
+ day of the week number may be wrong on some systems for dates
2277
+ before March 1st 1900, or after December 31 2099.
2278
+
2279
+ The `localtime` builtin works like the `gmtime` builtin, but
2280
+ using the local timezone setting.
2281
+
2282
+ The `mktime` builtin consumes "broken down time"
2283
+ representations of time output by `gmtime` and `strptime`.
2284
+
2285
+ The `strptime(fmt)` builtin parses input strings matching the
2286
+ `fmt` argument. The output is in the "broken down time"
2287
+ representation consumed by `mktime` and output by `gmtime`.
2288
+
2289
+ The `strftime(fmt)` builtin formats a time (GMT) with the
2290
+ given format. The `strflocaltime` does the same, but using
2291
+ the local timezone setting.
2292
+
2293
+ The format strings for `strptime` and `strftime` are described
2294
+ in typical C library documentation. The format string for ISO
2295
+ 8601 datetime is `"%Y-%m-%dT%H:%M:%SZ"`.
2296
+
2297
+ jq may not support some or all of this date functionality on
2298
+ some systems. In particular, the `%u` and `%j` specifiers for
2299
+ `strptime(fmt)` are not supported on macOS.
2300
+
2301
+ examples:
2302
+ - program: 'fromdate'
2303
+ input: '"2015-03-05T23:51:47Z"'
2304
+ output: ['1425599507']
2305
+
2306
+ - program: 'strptime("%Y-%m-%dT%H:%M:%SZ")'
2307
+ input: '"2015-03-05T23:51:47Z"'
2308
+ output: ['[2015,2,5,23,51,47,4,63]']
2309
+
2310
+ - program: 'strptime("%Y-%m-%dT%H:%M:%SZ")|mktime'
2311
+ input: '"2015-03-05T23:51:47Z"'
2312
+ output: ['1425599507']
2313
+
2314
+ - title: "SQL-Style Operators"
2315
+ body: |
2316
+
2317
+ jq provides a few SQL-style operators.
2318
+
2319
+ * `INDEX(stream; index_expression)`:
2320
+
2321
+ This builtin produces an object whose keys are computed by
2322
+ the given index expression applied to each value from the
2323
+ given stream.
2324
+
2325
+ * `JOIN($idx; stream; idx_expr; join_expr)`:
2326
+
2327
+ This builtin joins the values from the given stream to the
2328
+ given index. The index's keys are computed by applying the
2329
+ given index expression to each value from the given stream.
2330
+ An array of the value in the stream and the corresponding
2331
+ value from the index is fed to the given join expression to
2332
+ produce each result.
2333
+
2334
+ * `JOIN($idx; stream; idx_expr)`:
2335
+
2336
+ Same as `JOIN($idx; stream; idx_expr; .)`.
2337
+
2338
+ * `JOIN($idx; idx_expr)`:
2339
+
2340
+ This builtin joins the input `.` to the given index, applying
2341
+ the given index expression to `.` to compute the index key.
2342
+ The join operation is as described above.
2343
+
2344
+ * `IN(s)`:
2345
+
2346
+ This builtin outputs `true` if `.` appears in the given
2347
+ stream, otherwise it outputs `false`.
2348
+
2349
+ * `IN(source; s)`:
2350
+
2351
+ This builtin outputs `true` if any value in the source stream
2352
+ appears in the second stream, otherwise it outputs `false`.
2353
+
2354
+ - title: "`builtins`"
2355
+ body: |
2356
+
2357
+ Returns a list of all builtin functions in the format `name/arity`.
2358
+ Since functions with the same name but different arities are considered
2359
+ separate functions, `all/0`, `all/1`, and `all/2` would all be present
2360
+ in the list.
2361
+
2362
+ - title: Conditionals and Comparisons
2363
+ entries:
2364
+ - title: "`==`, `!=`"
2365
+ body: |
2366
+
2367
+ The expression 'a == b' will produce 'true' if the results of evaluating
2368
+ a and b are equal (that is, if they represent equivalent JSON values) and
2369
+ 'false' otherwise. In particular, strings are never considered equal
2370
+ to numbers. In checking for the equality of JSON objects, the ordering of keys
2371
+ is irrelevant. If you're coming from JavaScript, please note that jq's `==` is like
2372
+ JavaScript's `===`, the "strict equality" operator.
2373
+
2374
+ != is "not equal", and 'a != b' returns the opposite value of 'a == b'
2375
+
2376
+ examples:
2377
+ - program: '. == false'
2378
+ input: 'null'
2379
+ output: ['false']
2380
+
2381
+ - program: '. == {"b": {"d": (4 + 1e-20), "c": 3}, "a":1}'
2382
+ input: '{"a":1, "b": {"c": 3, "d": 4}}'
2383
+ output: ['true']
2384
+
2385
+ - program: '.[] == 1'
2386
+ input: '[1, 1.0, "1", "banana"]'
2387
+ output: ['true', 'true', 'false', 'false']
2388
+
2389
+ - title: if-then-else-end
2390
+ body: |
2391
+
2392
+ `if A then B else C end` will act the same as `B` if `A`
2393
+ produces a value other than false or null, but act the same
2394
+ as `C` otherwise.
2395
+
2396
+ `if A then B end` is the same as `if A then B else . end`.
2397
+ That is, the `else` branch is optional, and if absent is the
2398
+ same as `.`. This also applies to `elif` with absent ending `else` branch.
2399
+
2400
+ Checking for false or null is a simpler notion of
2401
+ "truthiness" than is found in JavaScript or Python, but it
2402
+ means that you'll sometimes have to be more explicit about
2403
+ the condition you want. You can't test whether, e.g. a
2404
+ string is empty using `if .name then A else B end`; you'll
2405
+ need something like `if .name == "" then A else B end` instead.
2406
+
2407
+ If the condition `A` produces multiple results, then `B` is evaluated
2408
+ once for each result that is not false or null, and `C` is evaluated
2409
+ once for each false or null.
2410
+
2411
+ More cases can be added to an if using `elif A then B` syntax.
2412
+
2413
+ examples:
2414
+ - program: |-
2415
+ if . == 0 then
2416
+ "zero"
2417
+ elif . == 1 then
2418
+ "one"
2419
+ else
2420
+ "many"
2421
+ end
2422
+ input: '2'
2423
+ output: ['"many"']
2424
+
2425
+ - title: "`>`, `>=`, `<=`, `<`"
2426
+ body: |
2427
+
2428
+ The comparison operators `>`, `>=`, `<=`, `<` return whether
2429
+ their left argument is greater than, greater than or equal
2430
+ to, less than or equal to or less than their right argument
2431
+ (respectively).
2432
+
2433
+ The ordering is the same as that described for `sort`, above.
2434
+
2435
+ examples:
2436
+ - program: '. < 5'
2437
+ input: '2'
2438
+ output: ['true']
2439
+
2440
+ - title: "`and`, `or`, `not`"
2441
+ body: |
2442
+
2443
+ jq supports the normal Boolean operators `and`, `or`, `not`.
2444
+ They have the same standard of truth as if expressions -
2445
+ `false` and `null` are considered "false values", and
2446
+ anything else is a "true value".
2447
+
2448
+ If an operand of one of these operators produces multiple
2449
+ results, the operator itself will produce a result for each input.
2450
+
2451
+ `not` is in fact a builtin function rather than an operator,
2452
+ so it is called as a filter to which things can be piped
2453
+ rather than with special syntax, as in `.foo and .bar |
2454
+ not`.
2455
+
2456
+ These three only produce the values `true` and `false`, and
2457
+ so are only useful for genuine Boolean operations, rather
2458
+ than the common Perl/Python/Ruby idiom of
2459
+ "value_that_may_be_null or default". If you want to use this
2460
+ form of "or", picking between two values rather than
2461
+ evaluating a condition, see the `//` operator below.
2462
+
2463
+ examples:
2464
+ - program: '42 and "a string"'
2465
+ input: 'null'
2466
+ output: ['true']
2467
+ - program: '(true, false) or false'
2468
+ input: 'null'
2469
+ output: ['true', 'false']
2470
+ - program: '(true, true) and (true, false)'
2471
+ input: 'null'
2472
+ output: ['true', 'false', 'true', 'false']
2473
+ - program: '[true, false | not]'
2474
+ input: 'null'
2475
+ output: ['[false, true]']
2476
+
2477
+ - title: "Alternative operator: `//`"
2478
+ body: |
2479
+
2480
+ The `//` operator produces all the values of its left-hand
2481
+ side that are neither `false` nor `null`. If the
2482
+ left-hand side produces no values other than `false` or
2483
+ `null`, then `//` produces all the values of its right-hand
2484
+ side.
2485
+
2486
+ A filter of the form `a // b` produces all the results of
2487
+ `a` that are not `false` or `null`. If `a` produces no
2488
+ results, or no results other than `false` or `null`, then `a
2489
+ // b` produces the results of `b`.
2490
+
2491
+ This is useful for providing defaults: `.foo // 1` will
2492
+ evaluate to `1` if there's no `.foo` element in the
2493
+ input. It's similar to how `or` is sometimes used in Python
2494
+ (jq's `or` operator is reserved for strictly Boolean
2495
+ operations).
2496
+
2497
+ Note: `some_generator // defaults_here` is not the same
2498
+ as `some_generator | . // defaults_here`. The latter will
2499
+ produce default values for all non-`false`, non-`null`
2500
+ values of the left-hand side, while the former will not.
2501
+ Precedence rules can make this confusing. For example, in
2502
+ `false, 1 // 2` the left-hand side of `//` is `1`, not
2503
+ `false, 1` -- `false, 1 // 2` parses the same way as `false,
2504
+ (1 // 2)`. In `(false, null, 1) | . // 42` the left-hand
2505
+ side of `//` is `.`, which always produces just one value,
2506
+ while in `(false, null, 1) // 42` the left-hand side is a
2507
+ generator of three values, and since it produces a
2508
+ value other `false` and `null`, the default `42` is not
2509
+ produced.
2510
+
2511
+ examples:
2512
+ - program: 'empty // 42'
2513
+ input: 'null'
2514
+ output: ['42']
2515
+ - program: '.foo // 42'
2516
+ input: '{"foo": 19}'
2517
+ output: ['19']
2518
+ - program: '.foo // 42'
2519
+ input: '{}'
2520
+ output: ['42']
2521
+ - program: '(false, null, 1) // 42'
2522
+ input: 'null'
2523
+ output: ['1']
2524
+ - program: '(false, null, 1) | . // 42'
2525
+ input: 'null'
2526
+ output: ['42', '42', '1']
2527
+
2528
+ - title: try-catch
2529
+ body: |
2530
+
2531
+ Errors can be caught by using `try EXP catch EXP`. The first
2532
+ expression is executed, and if it fails then the second is
2533
+ executed with the error message. The output of the handler,
2534
+ if any, is output as if it had been the output of the
2535
+ expression to try.
2536
+
2537
+ The `try EXP` form uses `empty` as the exception handler.
2538
+
2539
+ examples:
2540
+ - program: 'try .a catch ". is not an object"'
2541
+ input: 'true'
2542
+ output: ['". is not an object"']
2543
+ - program: '[.[]|try .a]'
2544
+ input: '[{}, true, {"a":1}]'
2545
+ output: ['[null, 1]']
2546
+ - program: 'try error("some exception") catch .'
2547
+ input: 'true'
2548
+ output: ['"some exception"']
2549
+
2550
+ - title: Breaking out of control structures
2551
+ body: |
2552
+
2553
+ A convenient use of try/catch is to break out of control
2554
+ structures like `reduce`, `foreach`, `while`, and so on.
2555
+
2556
+ For example:
2557
+
2558
+ # Repeat an expression until it raises "break" as an
2559
+ # error, then stop repeating without re-raising the error.
2560
+ # But if the error caught is not "break" then re-raise it.
2561
+ try repeat(exp) catch if .=="break" then empty else error
2562
+
2563
+ jq has a syntax for named lexical labels to "break" or "go (back) to":
2564
+
2565
+ label $out | ... break $out ...
2566
+
2567
+ The `break $label_name` expression will cause the program to
2568
+ act as though the nearest (to the left) `label $label_name`
2569
+ produced `empty`.
2570
+
2571
+ The relationship between the `break` and corresponding `label`
2572
+ is lexical: the label has to be "visible" from the break.
2573
+
2574
+ To break out of a `reduce`, for example:
2575
+
2576
+ label $out | reduce .[] as $item (null; if .==false then break $out else ... end)
2577
+
2578
+ The following jq program produces a syntax error:
2579
+
2580
+ break $out
2581
+
2582
+ because no label `$out` is visible.
2583
+
2584
+ - title: "Error Suppression / Optional Operator: `?`"
2585
+ body: |
2586
+
2587
+ The `?` operator, used as `EXP?`, is shorthand for `try EXP`.
2588
+
2589
+ examples:
2590
+ - program: '[.[] | .a?]'
2591
+ input: '[{}, true, {"a":1}]'
2592
+ output: ['[null, 1]']
2593
+ - program: '[.[] | tonumber?]'
2594
+ input: '["1", "invalid", "3", 4]'
2595
+ output: ['[1, 3, 4]']
2596
+
2597
+ - title: Regular expressions
2598
+ body: |
2599
+
2600
+ jq uses the
2601
+ [Oniguruma regular expression library](https://github.com/kkos/oniguruma/blob/master/doc/RE),
2602
+ as do PHP, TextMate, Sublime Text, etc, so the
2603
+ description here will focus on jq specifics.
2604
+
2605
+ Oniguruma supports several flavors of regular expression, so it is important to know
2606
+ that jq uses the ["Perl NG" (Perl with named groups)](https://github.com/kkos/oniguruma/blob/master/doc/SYNTAX.md) flavor.
2607
+
2608
+ The jq regex filters are defined so that they can be used using
2609
+ one of these patterns:
2610
+
2611
+ STRING | FILTER(REGEX)
2612
+ STRING | FILTER(REGEX; FLAGS)
2613
+ STRING | FILTER([REGEX])
2614
+ STRING | FILTER([REGEX, FLAGS])
2615
+
2616
+ where:
2617
+
2618
+ * STRING, REGEX, and FLAGS are jq strings and subject to jq string interpolation;
2619
+ * REGEX, after string interpolation, should be a valid regular expression;
2620
+ * FILTER is one of `test`, `match`, or `capture`, as described below.
2621
+
2622
+ Since REGEX must evaluate to a JSON string, some characters that are needed
2623
+ to form a regular expression must be escaped. For example, the regular expression
2624
+ `\s` signifying a whitespace character would be written as `"\\s"`.
2625
+
2626
+ FLAGS is a string consisting of one of more of the supported flags:
2627
+
2628
+ * `g` - Global search (find all matches, not just the first)
2629
+ * `i` - Case insensitive search
2630
+ * `m` - Multi line mode (`.` will match newlines)
2631
+ * `n` - Ignore empty matches
2632
+ * `p` - Both s and m modes are enabled
2633
+ * `s` - Single line mode (`^` -> `\A`, `$` -> `\Z`)
2634
+ * `l` - Find longest possible matches
2635
+ * `x` - Extended regex format (ignore whitespace and comments)
2636
+
2637
+ To match a whitespace with the `x` flag, use `\s`, e.g.
2638
+
2639
+ jq -n '"a b" | test("a\\sb"; "x")'
2640
+
2641
+ Note that certain flags may also be specified within REGEX, e.g.
2642
+
2643
+ jq -n '("test", "TEst", "teST", "TEST") | test("(?i)te(?-i)st")'
2644
+
2645
+ evaluates to: `true`, `true`, `false`, `false`.
2646
+
2647
+ entries:
2648
+ - title: "`test(val)`, `test(regex; flags)`"
2649
+ body: |
2650
+
2651
+ Like `match`, but does not return match objects, only `true` or `false`
2652
+ for whether or not the regex matches the input.
2653
+
2654
+ examples:
2655
+ - program: 'test("foo")'
2656
+ input: '"foo"'
2657
+ output: ['true']
2658
+ - program: '.[] | test("a b c # spaces are ignored"; "ix")'
2659
+ input: '["xabcd", "ABC"]'
2660
+ output: ['true', 'true']
2661
+
2662
+ - title: "`match(val)`, `match(regex; flags)`"
2663
+ body: |
2664
+
2665
+ **match** outputs an object for each match it finds. Matches have
2666
+ the following fields:
2667
+
2668
+ * `offset` - offset in UTF-8 codepoints from the beginning of the input
2669
+ * `length` - length in UTF-8 codepoints of the match
2670
+ * `string` - the string that it matched
2671
+ * `captures` - an array of objects representing capturing groups.
2672
+
2673
+ Capturing group objects have the following fields:
2674
+
2675
+ * `offset` - offset in UTF-8 codepoints from the beginning of the input
2676
+ * `length` - length in UTF-8 codepoints of this capturing group
2677
+ * `string` - the string that was captured
2678
+ * `name` - the name of the capturing group (or `null` if it was unnamed)
2679
+
2680
+ Capturing groups that did not match anything return an offset of -1
2681
+
2682
+ examples:
2683
+ - program: 'match("(abc)+"; "g")'
2684
+ input: '"abc abc"'
2685
+ output:
2686
+ - '{"offset": 0, "length": 3, "string": "abc", "captures": [{"offset": 0, "length": 3, "string": "abc", "name": null}]}'
2687
+ - '{"offset": 4, "length": 3, "string": "abc", "captures": [{"offset": 4, "length": 3, "string": "abc", "name": null}]}'
2688
+ - program: 'match("foo")'
2689
+ input: '"foo bar foo"'
2690
+ output: ['{"offset": 0, "length": 3, "string": "foo", "captures": []}']
2691
+ - program: 'match(["foo", "ig"])'
2692
+ input: '"foo bar FOO"'
2693
+ output:
2694
+ - '{"offset": 0, "length": 3, "string": "foo", "captures": []}'
2695
+ - '{"offset": 8, "length": 3, "string": "FOO", "captures": []}'
2696
+ - program: 'match("foo (?<bar123>bar)? foo"; "ig")'
2697
+ input: '"foo bar foo foo foo"'
2698
+ output:
2699
+ - '{"offset": 0, "length": 11, "string": "foo bar foo", "captures": [{"offset": 4, "length": 3, "string": "bar", "name": "bar123"}]}'
2700
+ - '{"offset": 12, "length": 8, "string": "foo foo", "captures": [{"offset": -1, "length": 0, "string": null, "name": "bar123"}]}'
2701
+
2702
+ - program: '[ match("."; "g")] | length'
2703
+ input: '"abc"'
2704
+ output: ['3']
2705
+
2706
+
2707
+ - title: "`capture(val)`, `capture(regex; flags)`"
2708
+ body: |
2709
+
2710
+ Collects the named captures in a JSON object, with the name
2711
+ of each capture as the key, and the matched string as the
2712
+ corresponding value.
2713
+
2714
+ examples:
2715
+ - program: 'capture("(?<a>[a-z]+)-(?<n>[0-9]+)")'
2716
+ input: '"xyzzy-14"'
2717
+ output: ['{ "a": "xyzzy", "n": "14" }']
2718
+
2719
+ - title: "`scan(regex)`, `scan(regex; flags)`"
2720
+ body: |
2721
+
2722
+ Emit a stream of the non-overlapping substrings of the input
2723
+ that match the regex in accordance with the flags, if any
2724
+ have been specified. If there is no match, the stream is empty.
2725
+ To capture all the matches for each input string, use the idiom
2726
+ `[ expr ]`, e.g. `[ scan(regex) ]`. If the regex contains capturing
2727
+ groups, the filter emits a stream of arrays, each of which contains
2728
+ the captured strings.
2729
+
2730
+ examples:
2731
+ - program: 'scan("c")'
2732
+ input: '"abcdefabc"'
2733
+ output: ['"c"', '"c"']
2734
+ - program: 'scan("(a+)(b+)")'
2735
+ input: '"abaabbaaabbb"'
2736
+ output: ['["a","b"]', '["aa","bb"]', '["aaa","bbb"]']
2737
+
2738
+ - title: "`split(regex; flags)`"
2739
+ body: |
2740
+
2741
+ Splits an input string on each regex match.
2742
+
2743
+ For backwards compatibility, when called with a single argument,
2744
+ `split` splits on a string, not a regex.
2745
+
2746
+ examples:
2747
+ - program: 'split(", *"; null)'
2748
+ input: '"ab,cd, ef"'
2749
+ output: ['["ab","cd","ef"]']
2750
+
2751
+
2752
+ - title: "`splits(regex)`, `splits(regex; flags)`"
2753
+ body: |
2754
+
2755
+ These provide the same results as their `split` counterparts,
2756
+ but as a stream instead of an array.
2757
+
2758
+ examples:
2759
+ - program: 'splits(", *")'
2760
+ input: '"ab,cd, ef, gh"'
2761
+ output: ['"ab"','"cd"','"ef"','"gh"']
2762
+ - program: 'splits(",? *"; "n")'
2763
+ input: '"ab,cd ef, gh"'
2764
+ output: ['"ab"','"cd"','"ef"','"gh"']
2765
+
2766
+ - title: "`sub(regex; tostring)`, `sub(regex; tostring; flags)`"
2767
+ body: |
2768
+
2769
+ Emit the string obtained by replacing the first match of
2770
+ regex in the input string with `tostring`, after
2771
+ interpolation. `tostring` should be a jq string or a stream
2772
+ of such strings, each of which may contain references to
2773
+ named captures. The named captures are, in effect, presented
2774
+ as a JSON object (as constructed by `capture`) to
2775
+ `tostring`, so a reference to a captured variable named "x"
2776
+ would take the form: `"\(.x)"`.
2777
+
2778
+ examples:
2779
+ - program: 'sub("[^a-z]*(?<x>[a-z]+)"; "Z\(.x)"; "g")'
2780
+ input: '"123abc456def"'
2781
+ output: ['"ZabcZdef"']
2782
+
2783
+ - program: '[sub("(?<a>.)"; "\(.a|ascii_upcase)", "\(.a|ascii_downcase)")]'
2784
+ input: '"aB"'
2785
+ output: ['["AB","aB"]']
2786
+
2787
+ - title: "`gsub(regex; tostring)`, `gsub(regex; tostring; flags)`"
2788
+ body: |
2789
+
2790
+ `gsub` is like `sub` but all the non-overlapping occurrences of the regex are
2791
+ replaced by `tostring`, after interpolation. If the second argument is a stream
2792
+ of jq strings, then `gsub` will produce a corresponding stream of JSON strings.
2793
+
2794
+ examples:
2795
+ - program: 'gsub("(?<x>.)[^a]*"; "+\(.x)-")'
2796
+ input: '"Abcabc"'
2797
+ output: ['"+A-+a-"']
2798
+
2799
+ - program: '[gsub("p"; "a", "b")]'
2800
+ input: '"p"'
2801
+ output: ['["a","b"]']
2802
+
2803
+
2804
+ - title: Advanced features
2805
+ body: |
2806
+ Variables are an absolute necessity in most programming languages, but
2807
+ they're relegated to an "advanced feature" in jq.
2808
+
2809
+ In most languages, variables are the only means of passing around
2810
+ data. If you calculate a value, and you want to use it more than once,
2811
+ you'll need to store it in a variable. To pass a value to another part
2812
+ of the program, you'll need that part of the program to define a
2813
+ variable (as a function parameter, object member, or whatever) in
2814
+ which to place the data.
2815
+
2816
+ It is also possible to define functions in jq, although this is
2817
+ is a feature whose biggest use is defining jq's standard library
2818
+ (many jq functions such as `map` and `select` are in fact written
2819
+ in jq).
2820
+
2821
+ jq has reduction operators, which are very powerful but a bit
2822
+ tricky. Again, these are mostly used internally, to define some
2823
+ useful bits of jq's standard library.
2824
+
2825
+ It may not be obvious at first, but jq is all about generators
2826
+ (yes, as often found in other languages). Some utilities are
2827
+ provided to help deal with generators.
2828
+
2829
+ Some minimal I/O support (besides reading JSON from standard
2830
+ input, and writing JSON to standard output) is available.
2831
+
2832
+ Finally, there is a module/library system.
2833
+
2834
+ entries:
2835
+ - title: "Variable / Symbolic Binding Operator: `... as $identifier | ...`"
2836
+ body: |
2837
+
2838
+ In jq, all filters have an input and an output, so manual
2839
+ plumbing is not necessary to pass a value from one part of a program
2840
+ to the next. Many expressions, for instance `a + b`, pass their input
2841
+ to two distinct subexpressions (here `a` and `b` are both passed the
2842
+ same input), so variables aren't usually necessary in order to use a
2843
+ value twice.
2844
+
2845
+ For instance, calculating the average value of an array of numbers
2846
+ requires a few variables in most languages - at least one to hold the
2847
+ array, perhaps one for each element or for a loop counter. In jq, it's
2848
+ simply `add / length` - the `add` expression is given the array and
2849
+ produces its sum, and the `length` expression is given the array and
2850
+ produces its length.
2851
+
2852
+ So, there's generally a cleaner way to solve most problems in jq than
2853
+ defining variables. Still, sometimes they do make things easier, so jq
2854
+ lets you define variables using `expression as $variable`. All
2855
+ variable names start with `$`. Here's a slightly uglier version of the
2856
+ array-averaging example:
2857
+
2858
+ length as $array_length | add / $array_length
2859
+
2860
+ We'll need a more complicated problem to find a situation where using
2861
+ variables actually makes our lives easier.
2862
+
2863
+
2864
+ Suppose we have an array of blog posts, with "author" and "title"
2865
+ fields, and another object which is used to map author usernames to
2866
+ real names. Our input looks like:
2867
+
2868
+ {"posts": [{"title": "First post", "author": "anon"},
2869
+ {"title": "A well-written article", "author": "person1"}],
2870
+ "realnames": {"anon": "Anonymous Coward",
2871
+ "person1": "Person McPherson"}}
2872
+
2873
+ We want to produce the posts with the author field containing a real
2874
+ name, as in:
2875
+
2876
+ {"title": "First post", "author": "Anonymous Coward"}
2877
+ {"title": "A well-written article", "author": "Person McPherson"}
2878
+
2879
+ We use a variable, `$names`, to store the realnames object, so that we
2880
+ can refer to it later when looking up author usernames:
2881
+
2882
+ .realnames as $names | .posts[] | {title, author: $names[.author]}
2883
+
2884
+ The expression `exp as $x | ...` means: for each value of expression
2885
+ `exp`, run the rest of the pipeline with the entire original input, and
2886
+ with `$x` set to that value. Thus `as` functions as something of a
2887
+ foreach loop.
2888
+
2889
+ Just as `{foo}` is a handy way of writing `{foo: .foo}`, so
2890
+ `{$foo}` is a handy way of writing `{foo: $foo}`.
2891
+
2892
+ Multiple variables may be declared using a single `as` expression by
2893
+ providing a pattern that matches the structure of the input
2894
+ (this is known as "destructuring"):
2895
+
2896
+ . as {realnames: $names, posts: [$first, $second]} | ...
2897
+
2898
+ The variable declarations in array patterns (e.g., `. as
2899
+ [$first, $second]`) bind to the elements of the array in from
2900
+ the element at index zero on up, in order. When there is no
2901
+ value at the index for an array pattern element, `null` is
2902
+ bound to that variable.
2903
+
2904
+ Variables are scoped over the rest of the expression that defines
2905
+ them, so
2906
+
2907
+ .realnames as $names | (.posts[] | {title, author: $names[.author]})
2908
+
2909
+ will work, but
2910
+
2911
+ (.realnames as $names | .posts[]) | {title, author: $names[.author]}
2912
+
2913
+ won't.
2914
+
2915
+ For programming language theorists, it's more accurate to
2916
+ say that jq variables are lexically-scoped bindings. In
2917
+ particular there's no way to change the value of a binding;
2918
+ one can only setup a new binding with the same name, but which
2919
+ will not be visible where the old one was.
2920
+
2921
+ examples:
2922
+ - program: '.bar as $x | .foo | . + $x'
2923
+ input: '{"foo":10, "bar":200}'
2924
+ output: ['210']
2925
+ - program: '. as $i|[(.*2|. as $i| $i), $i]'
2926
+ input: '5'
2927
+ output: ['[10,5]']
2928
+ - program: '. as [$a, $b, {c: $c}] | $a + $b + $c'
2929
+ input: '[2, 3, {"c": 4, "d": 5}]'
2930
+ output: ['9']
2931
+ - program: '.[] as [$a, $b] | {a: $a, b: $b}'
2932
+ input: '[[0], [0, 1], [2, 1, 0]]'
2933
+ output: ['{"a":0,"b":null}', '{"a":0,"b":1}', '{"a":2,"b":1}']
2934
+
2935
+ - title: 'Destructuring Alternative Operator: `?//`'
2936
+ body: |
2937
+
2938
+ The destructuring alternative operator provides a concise mechanism
2939
+ for destructuring an input that can take one of several forms.
2940
+
2941
+ Suppose we have an API that returns a list of resources and events
2942
+ associated with them, and we want to get the user_id and timestamp of
2943
+ the first event for each resource. The API (having been clumsily
2944
+ converted from XML) will only wrap the events in an array if the resource
2945
+ has multiple events:
2946
+
2947
+ {"resources": [{"id": 1, "kind": "widget", "events": {"action": "create", "user_id": 1, "ts": 13}},
2948
+ {"id": 2, "kind": "widget", "events": [{"action": "create", "user_id": 1, "ts": 14}, {"action": "destroy", "user_id": 1, "ts": 15}]}]}
2949
+
2950
+ We can use the destructuring alternative operator to handle this structural change simply:
2951
+
2952
+ .resources[] as {$id, $kind, events: {$user_id, $ts}} ?// {$id, $kind, events: [{$user_id, $ts}]} | {$user_id, $kind, $id, $ts}
2953
+
2954
+ Or, if we aren't sure if the input is an array of values or an object:
2955
+
2956
+ .[] as [$id, $kind, $user_id, $ts] ?// {$id, $kind, $user_id, $ts} | ...
2957
+
2958
+ Each alternative need not define all of the same variables, but all named
2959
+ variables will be available to the subsequent expression. Variables not
2960
+ matched in the alternative that succeeded will be `null`:
2961
+
2962
+ .resources[] as {$id, $kind, events: {$user_id, $ts}} ?// {$id, $kind, events: [{$first_user_id, $first_ts}]} | {$user_id, $first_user_id, $kind, $id, $ts, $first_ts}
2963
+
2964
+ Additionally, if the subsequent expression returns an error, the
2965
+ alternative operator will attempt to try the next binding. Errors
2966
+ that occur during the final alternative are passed through.
2967
+
2968
+ [[3]] | .[] as [$a] ?// [$b] | if $a != null then error("err: \($a)") else {$a,$b} end
2969
+
2970
+ examples:
2971
+ - program: '.[] as {$a, $b, c: {$d, $e}} ?// {$a, $b, c: [{$d, $e}]} | {$a, $b, $d, $e}'
2972
+ input: '[{"a": 1, "b": 2, "c": {"d": 3, "e": 4}}, {"a": 1, "b": 2, "c": [{"d": 3, "e": 4}]}]'
2973
+ output: ['{"a":1,"b":2,"d":3,"e":4}', '{"a":1,"b":2,"d":3,"e":4}']
2974
+ - program: '.[] as {$a, $b, c: {$d}} ?// {$a, $b, c: [{$e}]} | {$a, $b, $d, $e}'
2975
+ input: '[{"a": 1, "b": 2, "c": {"d": 3, "e": 4}}, {"a": 1, "b": 2, "c": [{"d": 3, "e": 4}]}]'
2976
+ output: ['{"a":1,"b":2,"d":3,"e":null}', '{"a":1,"b":2,"d":null,"e":4}']
2977
+ - program: '.[] as [$a] ?// [$b] | if $a != null then error("err: \($a)") else {$a,$b} end'
2978
+ input: '[[3]]'
2979
+ output: ['{"a":null,"b":3}']
2980
+
2981
+ - title: 'Defining Functions'
2982
+ body: |
2983
+
2984
+ You can give a filter a name using "def" syntax:
2985
+
2986
+ def increment: . + 1;
2987
+
2988
+ From then on, `increment` is usable as a filter just like a
2989
+ builtin function (in fact, this is how many of the builtins
2990
+ are defined). A function may take arguments:
2991
+
2992
+ def map(f): [.[] | f];
2993
+
2994
+ Arguments are passed as _filters_ (functions with no
2995
+ arguments), _not_ as values. The same argument may be
2996
+ referenced multiple times with different inputs (here `f` is
2997
+ run for each element of the input array). Arguments to a
2998
+ function work more like callbacks than like value arguments.
2999
+ This is important to understand. Consider:
3000
+
3001
+ def foo(f): f|f;
3002
+ 5|foo(.*2)
3003
+
3004
+ The result will be 20 because `f` is `.*2`, and during the
3005
+ first invocation of `f` `.` will be 5, and the second time it
3006
+ will be 10 (5 * 2), so the result will be 20. Function
3007
+ arguments are filters, and filters expect an input when
3008
+ invoked.
3009
+
3010
+ If you want the value-argument behaviour for defining simple
3011
+ functions, you can just use a variable:
3012
+
3013
+ def addvalue(f): f as $f | map(. + $f);
3014
+
3015
+ Or use the short-hand:
3016
+
3017
+ def addvalue($f): ...;
3018
+
3019
+ With either definition, `addvalue(.foo)` will add the current
3020
+ input's `.foo` field to each element of the array. Do note
3021
+ that calling `addvalue(.[])` will cause the `map(. + $f)` part
3022
+ to be evaluated once per value in the value of `.` at the call
3023
+ site.
3024
+
3025
+ Multiple definitions using the same function name are allowed.
3026
+ Each re-definition replaces the previous one for the same
3027
+ number of function arguments, but only for references from
3028
+ functions (or main program) subsequent to the re-definition.
3029
+ See also the section below on scoping.
3030
+
3031
+ examples:
3032
+ - program: 'def addvalue(f): . + [f]; map(addvalue(.[0]))'
3033
+ input: '[[1,2],[10,20]]'
3034
+ output: ['[[1,2,1], [10,20,10]]']
3035
+ - program: 'def addvalue(f): f as $x | map(. + $x); addvalue(.[0])'
3036
+ input: '[[1,2],[10,20]]'
3037
+ output: ['[[1,2,1,2], [10,20,1,2]]']
3038
+
3039
+ - title: 'Scoping'
3040
+ body: |
3041
+
3042
+ There are two types of symbols in jq: value bindings (a.k.a.,
3043
+ "variables"), and functions. Both are scoped lexically,
3044
+ with expressions being able to refer only to symbols that
3045
+ have been defined "to the left" of them. The only exception
3046
+ to this rule is that functions can refer to themselves so as
3047
+ to be able to create recursive functions.
3048
+
3049
+ For example, in the following expression there is a binding
3050
+ which is visible "to the right" of it, `... | .*3 as
3051
+ $times_three | [. + $times_three] | ...`, but not "to the
3052
+ left". Consider this expression now, `... | (.*3 as
3053
+ $times_three | [. + $times_three]) | ...`: here the binding
3054
+ `$times_three` is _not_ visible past the closing parenthesis.
3055
+
3056
+ - title: "`isempty(exp)`"
3057
+ body: |
3058
+
3059
+ Returns true if `exp` produces no outputs, false otherwise.
3060
+
3061
+ examples:
3062
+ - program: 'isempty(empty)'
3063
+ input: 'null'
3064
+ output: ['true']
3065
+
3066
+ - program: 'isempty(.[])'
3067
+ input: '[]'
3068
+ output: ['true']
3069
+
3070
+ - program: 'isempty(.[])'
3071
+ input: '[1,2,3]'
3072
+ output: ['false']
3073
+
3074
+ - title: "`limit(n; expr)`"
3075
+ body: |
3076
+
3077
+ The `limit` function extracts up to `n` outputs from `expr`.
3078
+
3079
+ examples:
3080
+ - program: '[limit(3; .[])]'
3081
+ input: '[0,1,2,3,4,5,6,7,8,9]'
3082
+ output: ['[0,1,2]']
3083
+
3084
+ - title: "`skip(n; expr)`"
3085
+ body: |
3086
+
3087
+ The `skip` function skips the first `n` outputs from `expr`.
3088
+
3089
+ examples:
3090
+ - program: '[skip(3; .[])]'
3091
+ input: '[0,1,2,3,4,5,6,7,8,9]'
3092
+ output: ['[3,4,5,6,7,8,9]']
3093
+
3094
+ - title: "`first(expr)`, `last(expr)`, `nth(n; expr)`"
3095
+ body: |
3096
+
3097
+ The `first(expr)` and `last(expr)` functions extract the first
3098
+ and last values from `expr`, respectively.
3099
+
3100
+ The `nth(n; expr)` function extracts the nth value output by `expr`.
3101
+ Note that `nth(n; expr)` doesn't support negative values of `n`.
3102
+
3103
+ examples:
3104
+ - program: '[first(range(.)), last(range(.)), nth(5; range(.))]'
3105
+ input: '10'
3106
+ output: ['[0,9,5]']
3107
+ - program: '[first(empty), last(empty), nth(5; empty)]'
3108
+ input: 'null'
3109
+ output: ['[]']
3110
+
3111
+ - title: "`first`, `last`, `nth(n)`"
3112
+ body: |
3113
+
3114
+ The `first` and `last` functions extract the first
3115
+ and last values from any array at `.`.
3116
+
3117
+ The `nth(n)` function extracts the nth value of any array at `.`.
3118
+
3119
+ examples:
3120
+ - program: '[range(.)]|[first, last, nth(5)]'
3121
+ input: '10'
3122
+ output: ['[0,9,5]']
3123
+
3124
+ - title: "`reduce`"
3125
+ body: |
3126
+
3127
+ The `reduce` syntax allows you to combine all of the results of
3128
+ an expression by accumulating them into a single answer.
3129
+ The form is `reduce EXP as $var (INIT; UPDATE)`.
3130
+ As an example, we'll pass `[1,2,3]` to this expression:
3131
+
3132
+ reduce .[] as $item (0; . + $item)
3133
+
3134
+ For each result that `.[]` produces, `. + $item` is run to
3135
+ accumulate a running total, starting from 0 as the input value.
3136
+ In this example, `.[]` produces the results `1`, `2`, and `3`,
3137
+ so the effect is similar to running something like this:
3138
+
3139
+ 0 | 1 as $item | . + $item |
3140
+ 2 as $item | . + $item |
3141
+ 3 as $item | . + $item
3142
+
3143
+ examples:
3144
+ - program: 'reduce .[] as $item (0; . + $item)'
3145
+ input: '[1,2,3,4,5]'
3146
+ output: ['15']
3147
+
3148
+ - program: 'reduce .[] as [$i,$j] (0; . + $i * $j)'
3149
+ input: '[[1,2],[3,4],[5,6]]'
3150
+ output: ['44']
3151
+
3152
+ - program: 'reduce .[] as {$x,$y} (null; .x += $x | .y += [$y])'
3153
+ input: '[{"x":"a","y":1},{"x":"b","y":2},{"x":"c","y":3}]'
3154
+ output: ['{"x":"abc","y":[1,2,3]}']
3155
+
3156
+ - title: "`foreach`"
3157
+ body: |
3158
+
3159
+ The `foreach` syntax is similar to `reduce`, but intended to
3160
+ allow the construction of `limit` and reducers that produce
3161
+ intermediate results.
3162
+
3163
+ The form is `foreach EXP as $var (INIT; UPDATE; EXTRACT)`.
3164
+ As an example, we'll pass `[1,2,3]` to this expression:
3165
+
3166
+ foreach .[] as $item (0; . + $item; [$item, . * 2])
3167
+
3168
+ Like the `reduce` syntax, `. + $item` is run for each result
3169
+ that `.[]` produces, but `[$item, . * 2]` is run for each
3170
+ intermediate values. In this example, since the intermediate
3171
+ values are `1`, `3`, and `6`, the `foreach` expression produces
3172
+ `[1,2]`, `[2,6]`, and `[3,12]`. So the effect is similar
3173
+ to running something like this:
3174
+
3175
+ 0 | 1 as $item | . + $item | [$item, . * 2],
3176
+ 2 as $item | . + $item | [$item, . * 2],
3177
+ 3 as $item | . + $item | [$item, . * 2]
3178
+
3179
+ When `EXTRACT` is omitted, the identity filter is used.
3180
+ That is, it outputs the intermediate values as they are.
3181
+
3182
+ examples:
3183
+ - program: 'foreach .[] as $item (0; . + $item)'
3184
+ input: '[1,2,3,4,5]'
3185
+ output: ['1','3','6','10','15']
3186
+
3187
+ - program: 'foreach .[] as $item (0; . + $item; [$item, . * 2])'
3188
+ input: '[1,2,3,4,5]'
3189
+ output: ['[1,2]','[2,6]','[3,12]','[4,20]','[5,30]']
3190
+
3191
+ - program: 'foreach .[] as $item (0; . + 1; {index: ., $item})'
3192
+ input: '["foo", "bar", "baz"]'
3193
+ output:
3194
+ - '{"index":1,"item":"foo"}'
3195
+ - '{"index":2,"item":"bar"}'
3196
+ - '{"index":3,"item":"baz"}'
3197
+
3198
+ - title: Recursion
3199
+ body: |
3200
+
3201
+ As described above, `recurse` uses recursion, and any jq
3202
+ function can be recursive. The `while` builtin is also
3203
+ implemented in terms of recursion.
3204
+
3205
+ Tail calls are optimized whenever the expression to the left of
3206
+ the recursive call outputs its last value. In practice this
3207
+ means that the expression to the left of the recursive call
3208
+ should not produce more than one output for each input.
3209
+
3210
+ For example:
3211
+
3212
+ def recurse(f): def r: ., (f | select(. != null) | r); r;
3213
+
3214
+ def while(cond; update):
3215
+ def _while:
3216
+ if cond then ., (update | _while) else empty end;
3217
+ _while;
3218
+
3219
+ def repeat(exp):
3220
+ def _repeat:
3221
+ exp, _repeat;
3222
+ _repeat;
3223
+
3224
+ - title: Generators and iterators
3225
+ body: |
3226
+
3227
+ Some jq operators and functions are actually generators in
3228
+ that they can produce zero, one, or more values for each
3229
+ input, just as one might expect in other programming
3230
+ languages that have generators. For example, `.[]`
3231
+ generates all the values in its input (which must be an
3232
+ array or an object), `range(0; 10)` generates the integers
3233
+ between 0 and 10, and so on.
3234
+
3235
+ Even the comma operator is a generator, generating first
3236
+ the values generated by the expression to the left of the
3237
+ comma, then the values generated by the expression on the
3238
+ right of the comma.
3239
+
3240
+ The `empty` builtin is the generator that produces zero
3241
+ outputs. The `empty` builtin backtracks to the preceding
3242
+ generator expression.
3243
+
3244
+ All jq functions can be generators just by using builtin
3245
+ generators. It is also possible to construct new generators
3246
+ using only recursion and the comma operator. If
3247
+ recursive calls are "in tail position" then the
3248
+ generator will be efficient. In the example below the
3249
+ recursive call by `_range` to itself is in tail position.
3250
+ The example shows off three advanced topics: tail recursion,
3251
+ generator construction, and sub-functions.
3252
+
3253
+ examples:
3254
+ - program: 'def range(init; upto; by):
3255
+ def _range:
3256
+ if (by > 0 and . < upto) or (by < 0 and . > upto)
3257
+ then ., ((.+by)|_range)
3258
+ else empty end;
3259
+ if init == upto then empty elif by == 0 then init else init|_range end;
3260
+ range(0; 10; 3)'
3261
+ input: 'null'
3262
+ output: ['0', '3', '6', '9']
3263
+ - program: 'def while(cond; update):
3264
+ def _while:
3265
+ if cond then ., (update | _while) else empty end;
3266
+ _while;
3267
+ [while(.<100; .*2)]'
3268
+ input: '1'
3269
+ output: ['[1,2,4,8,16,32,64]']
3270
+
3271
+ - title: 'Math'
3272
+ body: |
3273
+
3274
+ jq currently only has IEEE754 double-precision (64-bit) floating
3275
+ point number support.
3276
+
3277
+ Besides simple arithmetic operators such as `+`, jq also has most
3278
+ standard math functions from the C math library. C math functions
3279
+ that take a single input argument (e.g., `sin()`) are available as
3280
+ zero-argument jq functions. C math functions that take two input
3281
+ arguments (e.g., `pow()`) are available as two-argument jq
3282
+ functions that ignore `.`. C math functions that take three input
3283
+ arguments are available as three-argument jq functions that ignore
3284
+ `.`.
3285
+
3286
+ Availability of standard math functions depends on the
3287
+ availability of the corresponding math functions in your operating
3288
+ system and C math library. Unavailable math functions will be
3289
+ defined but will raise an error.
3290
+
3291
+ One-input C math functions: `acos` `acosh` `asin` `asinh` `atan`
3292
+ `atanh` `cbrt` `ceil` `cos` `cosh` `erf` `erfc` `exp` `exp10`
3293
+ `exp2` `expm1` `fabs` `floor` `gamma` `j0` `j1` `lgamma` `log`
3294
+ `log10` `log1p` `log2` `logb` `nearbyint` `rint` `round`
3295
+ `significand` `sin` `sinh` `sqrt` `tan` `tanh` `tgamma` `trunc`
3296
+ `y0` `y1`.
3297
+
3298
+ Two-input C math functions: `atan2` `copysign` `drem` `fdim`
3299
+ `fmax` `fmin` `fmod` `frexp` `hypot` `jn` `ldexp` `modf`
3300
+ `nextafter` `nexttoward` `pow` `remainder` `scalb` `scalbln` `yn`.
3301
+
3302
+ Three-input C math functions: `fma`.
3303
+
3304
+ See your system's manual for more information on each of these.
3305
+
3306
+ - title: 'I/O'
3307
+ body: |
3308
+
3309
+ At this time jq has minimal support for I/O, mostly in the
3310
+ form of control over when inputs are read. Two builtins functions
3311
+ are provided for this, `input` and `inputs`, that read from the
3312
+ same sources (e.g., `stdin`, files named on the command-line) as
3313
+ jq itself. These two builtins, and jq's own reading actions, can
3314
+ be interleaved with each other. They are commonly used in combination
3315
+ with the null input option `-n` to prevent one input from being read
3316
+ implicitly.
3317
+
3318
+ Two builtins provide minimal output capabilities, `debug`, and
3319
+ `stderr`. (Recall that a jq program's output values are always
3320
+ output as JSON texts on `stdout`.) The `debug` builtin can have
3321
+ application-specific behavior, such as for executables that use
3322
+ the libjq C API but aren't the jq executable itself. The `stderr`
3323
+ builtin outputs its input in raw mode to stder with no additional
3324
+ decoration, not even a newline.
3325
+
3326
+ Most jq builtins are referentially transparent, and yield constant
3327
+ and repeatable value streams when applied to constant inputs.
3328
+ This is not true of I/O builtins.
3329
+
3330
+ entries:
3331
+ - title: "`input`"
3332
+ body: |
3333
+
3334
+ Outputs one new input.
3335
+
3336
+ Note that when using `input` it is generally necessary to
3337
+ invoke jq with the `-n` command-line option, otherwise
3338
+ the first entity will be lost.
3339
+
3340
+ echo 1 2 3 4 | jq '[., input]' # [1,2] [3,4]
3341
+
3342
+ - title: "`inputs`"
3343
+ body: |
3344
+
3345
+ Outputs all remaining inputs, one by one.
3346
+
3347
+ This is primarily useful for reductions over a program's
3348
+ inputs. Note that when using `inputs` it is generally necessary
3349
+ to invoke jq with the `-n` command-line option, otherwise
3350
+ the first entity will be lost.
3351
+
3352
+ echo 1 2 3 | jq -n 'reduce inputs as $i (0; . + $i)' # 6
3353
+
3354
+ - title: "`debug`, `debug(msgs)`"
3355
+ body: |
3356
+
3357
+ These two filters are like `.` but have as a side-effect the
3358
+ production of one or more messages on stderr.
3359
+
3360
+ The message produced by the `debug` filter has the form
3361
+
3362
+ ["DEBUG:",<input-value>]
3363
+
3364
+ where `<input-value>` is a compact rendition of the input
3365
+ value. This format may change in the future.
3366
+
3367
+ The `debug(msgs)` filter is defined as `(msgs | debug | empty), .`
3368
+ thus allowing great flexibility in the content of the message,
3369
+ while also allowing multi-line debugging statements to be created.
3370
+
3371
+ For example, the expression:
3372
+
3373
+ 1 as $x | 2 | debug("Entering function foo with $x == \($x)", .) | (.+1)
3374
+
3375
+ would produce the value 3 but with the following two lines
3376
+ being written to stderr:
3377
+
3378
+ ["DEBUG:","Entering function foo with $x == 1"]
3379
+ ["DEBUG:",2]
3380
+
3381
+ - title: "`stderr`"
3382
+ body: |
3383
+
3384
+ Prints its input in raw and compact mode to stderr with no
3385
+ additional decoration, not even a newline.
3386
+
3387
+ - title: "`input_filename`"
3388
+ body: |
3389
+
3390
+ Returns the name of the file whose input is currently being
3391
+ filtered. Note that this will not work well unless jq is
3392
+ running in a UTF-8 locale.
3393
+
3394
+ - title: "`input_line_number`"
3395
+ body: |
3396
+
3397
+ Returns the line number of the input currently being filtered.
3398
+
3399
+ - title: 'Streaming'
3400
+ body: |
3401
+
3402
+ With the `--stream` option jq can parse input texts in a streaming
3403
+ fashion, allowing jq programs to start processing large JSON texts
3404
+ immediately rather than after the parse completes. If you have a
3405
+ single JSON text that is 1GB in size, streaming it will allow you
3406
+ to process it much more quickly.
3407
+
3408
+ However, streaming isn't easy to deal with as the jq program will
3409
+ have `[<path>, <leaf-value>]` (and a few other forms) as inputs.
3410
+
3411
+ Several builtins are provided to make handling streams easier.
3412
+
3413
+ The examples below use the streamed form of `["a",["b"]]`, which is
3414
+ `[[0],"a"],[[1,0],"b"],[[1,0]],[[1]]`.
3415
+
3416
+ Streaming forms include `[<path>, <leaf-value>]` (to indicate any
3417
+ scalar value, empty array, or empty object), and `[<path>]` (to
3418
+ indicate the end of an array or object). Future versions of jq
3419
+ run with `--stream` and `--seq` may output additional forms such
3420
+ as `["error message"]` when an input text fails to parse.
3421
+
3422
+ entries:
3423
+ - title: "`truncate_stream(stream_expression)`"
3424
+ body: |
3425
+
3426
+ Consumes a number as input and truncates the corresponding
3427
+ number of path elements from the left of the outputs of the
3428
+ given streaming expression.
3429
+
3430
+ examples:
3431
+ - program: 'truncate_stream([[0],"a"],[[1,0],"b"],[[1,0]],[[1]])'
3432
+ input: '1'
3433
+ output: ['[[0],"b"]', '[[0]]']
3434
+
3435
+ - title: "`fromstream(stream_expression)`"
3436
+ body: |
3437
+
3438
+ Outputs values corresponding to the stream expression's
3439
+ outputs.
3440
+
3441
+ examples:
3442
+ - program: 'fromstream(1|truncate_stream([[0],"a"],[[1,0],"b"],[[1,0]],[[1]]))'
3443
+ input: 'null'
3444
+ output: ['["b"]']
3445
+
3446
+ - title: "`tostream`"
3447
+ body: |
3448
+
3449
+ The `tostream` builtin outputs the streamed form of its input.
3450
+
3451
+ examples:
3452
+ - program: '. as $dot|fromstream($dot|tostream)|.==$dot'
3453
+ input: '[0,[1,{"a":1},{"b":2}]]'
3454
+ output: ['true']
3455
+
3456
+ - title: Assignment
3457
+ body: |
3458
+ Assignment works a little differently in jq than in most
3459
+ programming languages. jq doesn't distinguish between references
3460
+ to and copies of something - two objects or arrays are either
3461
+ equal or not equal, without any further notion of being "the
3462
+ same object" or "not the same object".
3463
+
3464
+ If an object has two fields which are arrays, `.foo` and `.bar`,
3465
+ and you append something to `.foo`, then `.bar` will not get
3466
+ bigger, even if you've previously set `.bar = .foo`. If you're
3467
+ used to programming in languages like Python, Java, Ruby,
3468
+ JavaScript, etc. then you can think of it as though jq does a full
3469
+ deep copy of every object before it does the assignment (for
3470
+ performance it doesn't actually do that, but that's the general
3471
+ idea).
3472
+
3473
+ This means that it's impossible to build circular values in jq
3474
+ (such as an array whose first element is itself). This is quite
3475
+ intentional, and ensures that anything a jq program can produce
3476
+ can be represented in JSON.
3477
+
3478
+ All the assignment operators in jq have path expressions on the
3479
+ left-hand side (LHS). The right-hand side (RHS) provides values
3480
+ to set to the paths named by the LHS path expressions.
3481
+
3482
+ Values in jq are always immutable. Internally, assignment works
3483
+ by using a reduction to compute new, replacement values for `.` that
3484
+ have had all the desired assignments applied to `.`, then
3485
+ outputting the modified value. This might be made clear by this
3486
+ example: `{a:{b:{c:1}}} | (.a.b|=3), .`. This will output
3487
+ `{"a":{"b":3}}` and `{"a":{"b":{"c":1}}}` because the last
3488
+ sub-expression, `.`, sees the original value, not the modified
3489
+ value.
3490
+
3491
+ Most users will want to use modification assignment operators,
3492
+ such as `|=` or `+=`, rather than `=`.
3493
+
3494
+ Note that the LHS of assignment operators refers to a value in
3495
+ `.`. Thus `$var.foo = 1` won't work as expected (`$var.foo` is
3496
+ not a valid or useful path expression in `.`); use `$var | .foo =
3497
+ 1` instead.
3498
+
3499
+ Note too that `.a,.b=0` does not set `.a` and `.b`, but
3500
+ `(.a,.b)=0` sets both.
3501
+
3502
+ entries:
3503
+ - title: "Update-assignment: `|=`"
3504
+ body: |
3505
+ This is the "update" operator `|=`. It takes a filter on the
3506
+ right-hand side and works out the new value for the property
3507
+ of `.` being assigned to by running the old value through this
3508
+ expression. For instance, `(.foo, .bar) |= .+1` will build an
3509
+ object with the `foo` field set to the input's `foo` plus 1,
3510
+ and the `bar` field set to the input's `bar` plus 1.
3511
+
3512
+ The left-hand side can be any general path expression; see `path()`.
3513
+
3514
+ Note that the left-hand side of `|=` refers to a value in `.`.
3515
+ Thus `$var.foo |= . + 1` won't work as expected (`$var.foo` is
3516
+ not a valid or useful path expression in `.`); use `$var |
3517
+ .foo |= . + 1` instead.
3518
+
3519
+ If the right-hand side outputs no values (i.e., `empty`), then
3520
+ the left-hand side path will be deleted, as with `del(path)`.
3521
+
3522
+ If the right-hand side outputs multiple values, only the first
3523
+ one will be used (COMPATIBILITY NOTE: in jq 1.5 and earlier
3524
+ releases, it used to be that only the last one was used).
3525
+
3526
+ examples:
3527
+ - program: '(..|select(type=="boolean")) |= if . then 1 else 0 end'
3528
+ input: '[true,false,[5,true,[true,[false]],false]]'
3529
+ output: ['[1,0,[5,1,[1,[0]],0]]']
3530
+
3531
+ - title: "Arithmetic update-assignment: `+=`, `-=`, `*=`, `/=`, `%=`, `//=`"
3532
+ body: |
3533
+
3534
+ jq has a few operators of the form `a op= b`, which are all
3535
+ equivalent to `a |= . op b`. So, `+= 1` can be used to
3536
+ increment values, being the same as `|= . + 1`.
3537
+
3538
+ examples:
3539
+ - program: .foo += 1
3540
+ input: '{"foo": 42}'
3541
+ output: ['{"foo": 43}']
3542
+
3543
+ - title: "Plain assignment: `=`"
3544
+ body: |
3545
+
3546
+ This is the plain assignment operator. Unlike the others, the
3547
+ input to the right-hand side (RHS) is the same as the input to
3548
+ the left-hand side (LHS) rather than the value at the LHS
3549
+ path, and all values output by the RHS will be used (as shown
3550
+ below).
3551
+
3552
+ If the RHS of `=` produces multiple values, then for each such
3553
+ value jq will set the paths on the left-hand side to the value
3554
+ and then it will output the modified `.`. For example,
3555
+ `(.a,.b) = range(2)` outputs `{"a":0,"b":0}`, then
3556
+ `{"a":1,"b":1}`. The "update" assignment forms (see above) do
3557
+ not do this.
3558
+
3559
+ This example should show the difference between `=` and `|=`:
3560
+
3561
+ Provide input `{"a": {"b": 10}, "b": 20}` to the programs
3562
+
3563
+ .a = .b
3564
+
3565
+ and
3566
+
3567
+ .a |= .b
3568
+
3569
+ The former will set the `a` field of the input to the `b`
3570
+ field of the input, and produce the output `{"a": 20, "b": 20}`.
3571
+ The latter will set the `a` field of the input to the `a`
3572
+ field's `b` field, producing `{"a": 10, "b": 20}`.
3573
+
3574
+ examples:
3575
+ - program: .a = .b
3576
+ input: '{"a": {"b": 10}, "b": 20}'
3577
+ output: ['{"a":20,"b":20}']
3578
+
3579
+ - program: .a |= .b
3580
+ input: '{"a": {"b": 10}, "b": 20}'
3581
+ output: ['{"a":10,"b":20}']
3582
+
3583
+ - program: (.a, .b) = range(3)
3584
+ input: 'null'
3585
+ output:
3586
+ - '{"a":0,"b":0}'
3587
+ - '{"a":1,"b":1}'
3588
+ - '{"a":2,"b":2}'
3589
+
3590
+ - program: (.a, .b) |= range(3)
3591
+ input: 'null'
3592
+ output: ['{"a":0,"b":0}']
3593
+
3594
+ - title: Complex assignments
3595
+ body: |
3596
+ Lots more things are allowed on the left-hand side of a jq assignment
3597
+ than in most languages. We've already seen simple field accesses on
3598
+ the left hand side, and it's no surprise that array accesses work just
3599
+ as well:
3600
+
3601
+ .posts[0].title = "JQ Manual"
3602
+
3603
+ What may come as a surprise is that the expression on the left may
3604
+ produce multiple results, referring to different points in the input
3605
+ document:
3606
+
3607
+ .posts[].comments |= . + ["this is great"]
3608
+
3609
+ That example appends the string "this is great" to the "comments"
3610
+ array of each post in the input (where the input is an object with a
3611
+ field "posts" which is an array of posts).
3612
+
3613
+ When jq encounters an assignment like 'a = b', it records the "path"
3614
+ taken to select a part of the input document while executing a. This
3615
+ path is then used to find which part of the input to change while
3616
+ executing the assignment. Any filter may be used on the
3617
+ left-hand side of an equals - whichever paths it selects from the
3618
+ input will be where the assignment is performed.
3619
+
3620
+ This is a very powerful operation. Suppose we wanted to add a comment
3621
+ to blog posts, using the same "blog" input above. This time, we only
3622
+ want to comment on the posts written by "stedolan". We can find those
3623
+ posts using the "select" function described earlier:
3624
+
3625
+ .posts[] | select(.author == "stedolan")
3626
+
3627
+ The paths provided by this operation point to each of the posts that
3628
+ "stedolan" wrote, and we can comment on each of them in the same way
3629
+ that we did before:
3630
+
3631
+ (.posts[] | select(.author == "stedolan") | .comments) |=
3632
+ . + ["terrible."]
3633
+
3634
+ - title: Comments
3635
+
3636
+ body: |
3637
+
3638
+ You can write comments in your jq filters using `#`.
3639
+
3640
+ A `#` character (not part of a string) starts a comment.
3641
+ All characters from `#` to the end of the line are ignored.
3642
+
3643
+ If the end of the line is preceded by an odd number of backslash
3644
+ characters, the following line is also considered part of the
3645
+ comment and is ignored.
3646
+
3647
+ For example, the following code outputs `[1,3,4,7]`
3648
+
3649
+ [
3650
+ 1,
3651
+ # foo \
3652
+ 2,
3653
+ # bar \\
3654
+ 3,
3655
+ 4, # baz \\\
3656
+ 5, \
3657
+ 6,
3658
+ 7
3659
+ # comment \
3660
+ comment \
3661
+ comment
3662
+ ]
3663
+
3664
+ Backslash continuing the comment on the next line can be useful
3665
+ when writing the "shebang" for a jq script:
3666
+
3667
+ #!/bin/sh --
3668
+ # total - Output the sum of the given arguments (or stdin)
3669
+ # usage: total [numbers...]
3670
+ # \
3671
+ exec jq --args -MRnf -- "$0" "$@"
3672
+
3673
+ $ARGS.positional |
3674
+ reduce (
3675
+ if . == []
3676
+ then inputs
3677
+ else .[]
3678
+ end |
3679
+ . as $dot |
3680
+ try tonumber catch false |
3681
+ if not or isnan then
3682
+ @json "total: Invalid number \($dot).\n" | halt_error(1)
3683
+ end
3684
+ ) as $n (0; . + $n)
3685
+
3686
+ The `exec` line is considered a comment by jq, so it is ignored.
3687
+ But it is not ignored by `sh`, since in `sh` a backslash at the
3688
+ end of the line does not continue the comment.
3689
+ With this trick, when the script is invoked as `total 1 2`,
3690
+ `/bin/sh -- /path/to/total 1 2` will be run, and `sh` will then
3691
+ run `exec jq --args -MRnf -- /path/to/total 1 2` replacing itself
3692
+ with a `jq` interpreter invoked with the specified options (`-M`,
3693
+ `-R`, `-n`, `--args`), that evaluates the current file (`$0`),
3694
+ with the arguments (`$@`) that were passed to `sh`.
3695
+
3696
+ - title: Modules
3697
+ body: |
3698
+
3699
+ jq has a library/module system. Modules are files whose names end
3700
+ in `.jq`.
3701
+
3702
+ Modules imported by a program are searched for in a default search
3703
+ path (see below). The `import` and `include` directives allow the
3704
+ importer to alter this path.
3705
+
3706
+ Paths in the search path are subject to various substitutions.
3707
+
3708
+ For paths starting with `~/`, the user's home directory is
3709
+ substituted for `~`.
3710
+
3711
+ For paths starting with `$ORIGIN/`, the directory where the jq
3712
+ executable is located is substituted for `$ORIGIN`.
3713
+
3714
+ For paths starting with `./` or paths that are `.`, the path of
3715
+ the including file is substituted for `.`. For top-level programs
3716
+ given on the command-line, the current directory is used.
3717
+
3718
+ Import directives can optionally specify a search path to which
3719
+ the default is appended.
3720
+
3721
+ The default search path is the search path given to the `-L`
3722
+ command-line option, else `["~/.jq", "$ORIGIN/../lib/jq",
3723
+ "$ORIGIN/../lib"]`.
3724
+
3725
+ Null and empty string path elements terminate search path
3726
+ processing.
3727
+
3728
+ A dependency with relative path `foo/bar` would be searched for in
3729
+ `foo/bar.jq` and `foo/bar/bar.jq` in the given search path. This
3730
+ is intended to allow modules to be placed in a directory along
3731
+ with, for example, version control files, README files, and so on,
3732
+ but also to allow for single-file modules.
3733
+
3734
+ Consecutive components with the same name are not allowed to avoid
3735
+ ambiguities (e.g., `foo/foo`).
3736
+
3737
+ For example, with `-L$HOME/.jq` a module `foo` can be found in
3738
+ `$HOME/.jq/foo.jq` and `$HOME/.jq/foo/foo.jq`.
3739
+
3740
+ If `.jq` exists in the user's home directory, and is a file (not a
3741
+ directory), it is automatically sourced into the main program.
3742
+
3743
+ entries:
3744
+ - title: "`import RelativePathString as NAME [<metadata>];`"
3745
+ body: |
3746
+
3747
+ Imports a module found at the given path relative to a
3748
+ directory in a search path. A `.jq` suffix will be added to
3749
+ the relative path string. The module's symbols are prefixed
3750
+ with `NAME::`.
3751
+
3752
+ The optional metadata must be a constant jq expression. It
3753
+ should be an object with keys like `homepage` and so on. At
3754
+ this time jq only uses the `search` key/value of the metadata.
3755
+ The metadata is also made available to users via the
3756
+ `modulemeta` builtin.
3757
+
3758
+ The `search` key in the metadata, if present, should have a
3759
+ string or array value (array of strings); this is the search
3760
+ path to be prefixed to the top-level search path.
3761
+
3762
+ - title: "`include RelativePathString [<metadata>];`"
3763
+ body: |
3764
+
3765
+ Imports a module found at the given path relative to a
3766
+ directory in a search path as if it were included in place. A
3767
+ `.jq` suffix will be added to the relative path string. The
3768
+ module's symbols are imported into the caller's namespace as
3769
+ if the module's content had been included directly.
3770
+
3771
+ The optional metadata must be a constant jq expression. It
3772
+ should be an object with keys like `homepage` and so on. At
3773
+ this time jq only uses the `search` key/value of the metadata.
3774
+ The metadata is also made available to users via the
3775
+ `modulemeta` builtin.
3776
+
3777
+ - title: "`import RelativePathString as $NAME [<metadata>];`"
3778
+ body: |
3779
+
3780
+ Imports a JSON file found at the given path relative to a
3781
+ directory in a search path. A `.json` suffix will be added to
3782
+ the relative path string. The file's data will be available
3783
+ as `$NAME::NAME`.
3784
+
3785
+ The optional metadata must be a constant jq expression. It
3786
+ should be an object with keys like `homepage` and so on. At
3787
+ this time jq only uses the `search` key/value of the metadata.
3788
+ The metadata is also made available to users via the
3789
+ `modulemeta` builtin.
3790
+
3791
+ The `search` key in the metadata, if present, should have a
3792
+ string or array value (array of strings); this is the search
3793
+ path to be prefixed to the top-level search path.
3794
+
3795
+ - title: "`module <metadata>;`"
3796
+ body: |
3797
+
3798
+ This directive is entirely optional. It's not required for
3799
+ proper operation. It serves only the purpose of providing
3800
+ metadata that can be read with the `modulemeta` builtin.
3801
+
3802
+ The metadata must be a constant jq expression. It should be
3803
+ an object with keys like `homepage`. At this time jq doesn't
3804
+ use this metadata, but it is made available to users via the
3805
+ `modulemeta` builtin.
3806
+
3807
+ - title: "`modulemeta`"
3808
+ body: |
3809
+
3810
+ Takes a module name as input and outputs the module's metadata
3811
+ as an object, with the module's imports (including metadata)
3812
+ as an array value for the `deps` key and the module's defined
3813
+ functions as an array value for the `defs` key.
3814
+
3815
+ Programs can use this to query a module's metadata, which they
3816
+ could then use to, for example, search for, download, and
3817
+ install missing dependencies.
3818
+
3819
+ - title: Colors
3820
+ body: |
3821
+
3822
+ To configure alternative colors just set the `JQ_COLORS`
3823
+ environment variable to colon-delimited list of partial terminal
3824
+ escape sequences like `"1;31"`, in this order:
3825
+
3826
+ - color for `null`
3827
+ - color for `false`
3828
+ - color for `true`
3829
+ - color for numbers
3830
+ - color for strings
3831
+ - color for arrays
3832
+ - color for objects
3833
+ - color for object keys
3834
+
3835
+ The default color scheme is the same as setting
3836
+ `JQ_COLORS="0;90:0;39:0;39:0;39:0;32:1;39:1;39:1;34"`.
3837
+
3838
+ This is not a manual for VT100/ANSI escapes. However, each of
3839
+ these color specifications should consist of two numbers separated
3840
+ by a semi-colon, where the first number is one of these:
3841
+
3842
+ - 1 (bright)
3843
+ - 2 (dim)
3844
+ - 4 (underscore)
3845
+ - 5 (blink)
3846
+ - 7 (reverse)
3847
+ - 8 (hidden)
3848
+
3849
+ and the second is one of these:
3850
+
3851
+ - 30 (black)
3852
+ - 31 (red)
3853
+ - 32 (green)
3854
+ - 33 (yellow)
3855
+ - 34 (blue)
3856
+ - 35 (magenta)
3857
+ - 36 (cyan)
3858
+ - 37 (white)