0.7.2 • Published 6 years ago

jq-in-the-browser v0.7.2

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

jq-in-the-browser

jq-in-the-browser is a JavaScript port of jq. Try it online

Instead of processing serialized data, jq-in-the-browser processes JavaScript objects. It is written from scratch and is relatively small. (~33 kB, ~6.1 kB gzipped)

Install

npm install --save jq-in-the-browser

Usage

import jq from 'jq-in-the-browser'

const query = jq('{"names": [.[] | .name]}')

query([
  {"name": "Mary", "age": 22},
  {"name": "Rupert", "age": 29},
  {"name": "Jane", "age": 11},
  {"name": "John", "age": 42}
])

Output:

{
  "names": [
    "Mary",
    "Rupert",
    "Jane",
    "John"
  ]
}

Comparison with alternatives

jq-web

  • jq-web is an emcripten port of jq, thus it implements all of its features
  • ... but it's also too big for many purposes (in the megabytes)
  • jq-in-the-browser is written from scratch, but is more limited in features
  • ... and also much much smaller :-)

node-jq

  • node-jq is great, but it doesn't work in the browser.

something else?

If you know an alternative, feel free to create a pull request. :-)

Supported features

FeatureExample
Identity., .
Array Index.[0], .[1 ], .[-1], .[ 1][0], .[1][1].x, .[1][1].x[0], .[ -1 ]
Object Identifier-Index.foo, .bar, .bar.x, .foo[1]
Generic Object Index.["foo"], .["bar"].x, .bar[ "y"], .["2bar"], .["a b" ]
Pipe.a \| .b, .a\|.b
Parentheses( .a), ((.a)), (-1 ), (-5.5), (.4), (. \| .)
Addition (numbers)1 + 1, .a + [.b][0], .b + .a, 3 + 4.1 + .a, 3 + (-3)
Subtraction (numbers).a - .b, .b - .a, 4- 3, -3 -(4)
Multiplication (numbers)1 * 1, .a * [.b][0], .b * .a, 3 * 4.1 * .a, 3 * (-.3)
Modulo (numbers)1 % 1, .a % [.b][0], .b % .a, 3 % 4 % .a
Division (numbers).a / .b, .b / .a, 4/ 3, -3/(4), -1.1 + (3 * (((.4 - .b) / .a) + .b))
Array Construction[], [ ], [4], [ -6, [0]], [7 \| 4], [.], [. \| [6]], [5, 6] \| .
Object Construction{}, { }, {"foo": 6}, {"foo": 6, "bar": [5, 3]}, {"x": 3} \| {"y": .x}, {foo: "bar"}, {({"a": "b"} \| .a): true}, {"a": 4, "b": 3, "c": -1, "d": "f"}
Integer literal3, 6, -4, 0, 8
Float literal.3, 6.0, -4.001, 3.14, 0.1
Boolean literaltrue, false
Double quote String literal"true", "false", "foo", ["ba'r"]
length[] \| length, length
keyskeys
keys_unsortedkeys_unsorted
to_entries. \| to_entries
from_entries. \| from_entries
reverse. \| reverse
mapmap(.+1 ), . \| map( {foo: .})
map_valuesmap_values(.+1 ), . \| map_values( {foo: .})
with_entrieswith_entries({key: .key, value: (2 * .value)}), with_entries({key: "a", value: (2 * .value)})
tonumbertonumber
tostringtostring
sortsort, [4, 5, 6] \| sort
sort_bysort_by(-.), sort_by(1 + .), sort_by(1)
joinjoin(", "), join(""), join(.[0])
Additive inverse-(1 + 3), -(-1), .a \| -(.b), [--1]
Array Construction[], [4]
Array/Object Value Iterator.[], .[ ]
Array/Object Value Iterator 2.["foo"][], .foo[]
Pipe.[] \| ., .[] \| .name
Stream as object value{names: .[] \| .name}, {"names": .[] \| .name, "ages": .[] \| .age}, {"names": .[] \| .name, "x": 3}, {"names": 5.4, "x": .[] \| .age}, {names: 5.4, ages: .[] \| .age, ages2: .[] \| .id}
Array/String slice.[2:4], .[0:1]
0.7.2

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago