jsqry is better than jq

In my last article on Habrรฉ, I wrote about the Jsqry library , which provides a simple and convenient query language (DSL) for JSON objects. Much time has passed since then and the library has also developed. A separate reason for pride - the library has 98% code coverage by tests. However, this article is not entirely about her.



I think many of you are familiar with the tool jq, which is practically the de facto standard for working with JSON in the command line and scripts. I was also an active user of it. But I was constantly worried about the unjustified complexity and unintuitive syntax of queries of this utility. And not me alone, here are just a few quotes from hacker news :



I have been using jq for years and still can't get it to work quite how I would expect it to.



I have the same issue with jq. I need to use my google fu to figure out how to do anything more than a simple select.



I don't know what the term would be, mental model, but I just can't get jq to click. Mostly because i only need it every once in a while. It's frustrating for me because it seems quite powerful.



I know I might be a dissenting opinion here, but I can never wrap my head around jq. I can manage jq ., jq .fooand jq -r, but beyond that, the DSL is just opaque to me.



Let's just say it: jq is an amazing tool, but the DSL is just bad.



Yeah, I find jq similar to writing regexes: I always have to look up the syntax, only get it working after some confusion why my patterns aren't matching, then forget it all in a few days so have to relearn it again later.

In short, you've probably already guessed it. I got an idea, why not turn my JS library into a command line executable.There is one nuance here. The library is written in JS and its DSL also relies on JS. This means that we need to find a way to package the program and some JS-runtime into a self-contained executable file.



jsqry - GraalVM edition



For those who are not yet in the subject (are there really still such? OO), let me remind you that GraalVM is such a pumped-up JVM from Oracle with additional features, the most noticeable of which are:



  1. JVM โ€” Java, Javascript, Python, Ruby, R, ..
  2. AOT- โ€” Java
  3. JIT- Java.


Graal , , -.



, 1. 2. โ€” JS .



https://github.com/jsqry/jsqry-cli. , โ€” deprecated. , . , 99 . - . , jq 3.7 Linux 64.



.



, Java + JS GraalVM.





App.java. , java- Apache Commons CLI.



java- javascript , src/main/resources.



. -



scripts.add(new String(Files.readAllBytes(Paths.get(jsFileResource.toURI()))));


( , native-image)



java.nio.file.FileSystemNotFoundException: Provider "resource" not installed


"" InputStream



scripts.add(new Scanner(jsFileResource.openStream()).useDelimiter("\\A").next());


, 100% Java .



java.awt.Graphics. GraalVM AWS Lambda .



jsqry โ€” QuickJS edition



- JS QuickJS . qjsc . ES2020. !



, CLI- jsqry: https://github.com/jsqry/jsqry-cli2.

.



, jsqry?



jsqry ( jq) JSON "" DSL.



โ€” JS jsqry .







$ echo '[{"name":"John","age":30},
         {"name":"Alice","age":25},
         {"name":"Bob","age":50}]' | jsqry 'name'
[
  "John",
  "Alice",
  "Bob"
]




$ echo '[{"name":"John","age":30},
         {"name":"Alice","age":25},
         {"name":"Bob","age":50}]' | jsqry -1 'name'
"John"




$ echo '[{"name":"John","age":30},{"name":"Alice","age":25},{"name":"Bob","age":50}]' \
    | jsqry '[ _.age>=? && _.name.toLowerCase().startsWith(?) ]' --arg 30 --arg-str joh 
[
  {
    "name": "John",
    "age": 30
  }
]


JSON pretty-printer



$ echo '[{"name":"John","age":30},{"name":"Alice","age":25},{"name":"Bob","age":50}]' \
 | jsqry
[
  {
    "name": "John",
    "age": 30
  },
  {
    "name": "Alice",
    "age": 25
  },
  {
    "name": "Bob",
    "age": 50
  }
]


JSON . !



-



2, 100, 2 . . DSL.



$ echo '[1,2,3,4,5]' | jsqry '[_>2] {_+100} s(-_) [-2:]'
[
  104,
  103
]


JS



jsqry JS- 1 , JS !



$ echo '["HTTP://EXAMPLE.COM/123", 
         "https://www.Google.com/search?q=test", 
         "https://www.YouTube.com/watch?v=_OBlgSz8sSM"]' \
 | jsqry '{ _.match(/:\/\/([^\/]+)\//)[1].toLowerCase() }'
[
  "example.com",
  "www.google.com",
  "www.youtube.com"
]


help-



$ jsqry
jsqry ver. 0.1.2
Usage: echo $JSON | jsqry 'query'
 -1,--first     return first result element
 -h,--help      print help and exit
 -v,--version   print version and exit
 -c,--compact   compact output (no pretty-print)
 -u,--unquote   unquote output string(s)
 -as ARG,
 --arg-str ARG  supply string query argument
 -a ARG,
 --arg ARG      supply query argument of any other type


jq



jq jsqry .





( ): 0.1.2.



, Linux x64 . , . .



, :



$ sudo bash -e -c "
wget https://github.com/jsqry/jsqry-cli2/releases/download/v0.1.2/jsqry-linux-amd64 -O/usr/local/bin/jsqry
chmod +x /usr/local/bin/jsqry
echo \"jsqry \$(jsqry -v) installed successfully\" 
"


CLI-



GitHub - . - , . , CLI- , . , , , โ€” .



"bash unit testing" BATS, ShellSpec, Bach , , ( 14 ), tush, .



tush



$ command --that --should --execute correctly
| expected stdout output

$ command --that --will --cause error
@ expected stderr output
? expected-exit-code


tush $, |, @ ? โ€” , . , $ , diff. , diff . :



$ /bin/bash /home/xonix/proj/jsqry-cli2/tests.sh
--- tests.tush expected
+++ tests.tush actual
@@ -1,5 +1,5 @@
 $ jsqry -v
-| 0.1.2
+| 0.1.1

 $ jsqry -h
 | jsqry ver. 0.1.1
!!! TESTS FAILED !!!




tests.tush.



, , .



GitHub Action, , :



Build and test





JSON



JSON . zvakanaka/color-json , StackOverflow . jq. , null .



screenshot



, .



npm- QuickJS



npm- jsqry . package.json . npm i. prepare-for-qjs.py, nodejs ES, QuickJS. jsqry-cli.js.



UTF-8 QuickJS



QuickJS stdin. , , QuickJS, . , UTF-8 JS-. , , QuickJS: twardoch/svgop.





build.sh.



"" , .



โ€” tests.sh. , , .



โ€” build.sh QuickJS, tests.sh tush. โ€” .



. ls -lh jsqry . , , CLI- . , โ€” , QuickJS.



652 KB. , , JS.





jq. .




All Articles