Giter Club home page Giter Club logo

jsqry-cli2's Introduction

jsqry-cli2

Build and test

jsqry is a small command line tool (similar to jq) to query JSON using sane DSL.

The purpose of this app is to expose the functionality of jsqry JS library in form of CLI.

Unlike jsqry-cli this one is based on QuickJS by Fabrice Bellard.

Examples

query

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

first element

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

use query parametrization

$ 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
  }
]

use as simple 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
  }
]

The output is pretty-printed by default. And colored!

something trickier

Filter greater than 2, map adding 100, sort descending, take last 2 elements. By combining these features you can build arbitrary complex queries. Find more on supported DSL.

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

full JS power

Since jsqry bundles the full-fledged JS engine in under 1 MB executable, the full power of JS is in your hands!

$ 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 message

$ 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

Compare to jq

https://gist.github.com/xonixx/d6066e83ec0773df248141440b18e8e4

Install

Current version: 0.1.2.

Sorry, but only Linux x64 is supported at the moment. Hopefully this will improve.

To install or update the tool simply run the command below.

$ 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\" 
"

jsqry-cli2's People

Contributors

xonixx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

zored

jsqry-cli2's Issues

Segfault

$ echo '[{"a":1},{"a":"hello"}]' | ./build/jsqry '[_.a>1]' ; echo $?
Segmentation fault
139

Add support for arguments

$ echo '[{"name": "Bob", "age": 30}, {"name": "Alice", "age": 25}]' | jsqry '[_.name==?].age' Alice
[
  25
]  

Let the args always parsed as strings (not numbers) for now

Add support to unquoted output

$ echo '[{"name":"Bob"}, {"name": "Alice"}]' | jsqry 'name' -u
Bob
Alice
$ echo '[{"name":"Bob"}, {"name": "Alice"}]' | jsqry 'name' -1 -u
Bob

What to do with non-unquote-able objects? Let's just return compacted

$ echo '[{"name":"Bob"}, {"name": "Alice"}, {"name": {"value":"John"}}]' | jsqry 'name' -u
Bob
Alice
{"value":"John"}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.