Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsonfilter

Package Overview
Dependencies
Maintainers
4
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonfilter - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

test.json

6

cli.js
#!/usr/bin/env node
var fs = require('fs')
var minimist = require('minimist')
var args = minimist(process.argv.slice(2))
var jsonfilter = require('./')
var firstArg = process.argv[2]
process.stdin.pipe(jsonfilter(firstArg)).on('data', function(o) {
var firstArg = args._[0]
process.stdin.pipe(jsonfilter(firstArg, args)).on('data', function(o) {
process.stdout.write(o)

@@ -8,0 +10,0 @@ }).on('end', function() {

@@ -7,2 +7,3 @@ ## Collaborators

<tr><th align="left">jlord</th><td><a href="https://github.com/jlord">GitHub/jlord</a></td></tr>
<tr><th align="left">groundwater</th><td><a href="https://github.com/groundwater">GitHub/groundwater</a></td></tr>
</tbody></table>
var JSONStream = require('JSONStream')
var combiner = require('stream-combiner')
var through = require('through2')
module.exports = function(filter) {
return combiner(
JSONStream.parse(filter),
JSONStream.stringify('', '\n', '')
)
module.exports = function(filter, opts) {
if (!opts) opts = {}
var pipeline = [
JSONStream.parse(filter)
]
if (opts.match) {
pipeline.push(createFunctionStream(opts.match, function(output, object, next) {
if (!!output) this.push(object)
next()
}))
}
pipeline.push(JSONStream.stringify('', '\n', ''))
return combiner.apply(null, pipeline)
}
function createFunctionStream(func, customTransform) {
var funcStr = 'var that = ' + func + ';\n return that;'
var compiled = new Function(funcStr)
function transform(obj, enc, next) {
var out = compiled.call(obj, obj)
if (customTransform) {
customTransform.call(this, out, obj, next)
} else {
this.push(out)
next()
}
}
return through.obj(transform)
}
{
"name": "jsonfilter",
"version": "1.0.2",
"version": "1.1.0",
"bin": {

@@ -13,7 +13,10 @@ "jsonfilter": "cli.js"

"JSONStream": "^0.8.4",
"stream-combiner": "^0.2.1"
"stream-combiner": "^0.2.1",
"through2": "^0.6.3"
},
"devDependencies": {},
"devDependencies": {
"minimist": "^1.1.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "cat test.json | jsonfilter dog"
},

@@ -20,0 +23,0 @@ "repository": {

# jsonfilter
Streaming JSON filtering on the command line.
Streaming JSON filtering on the command line. Supports JSON querying and expression based filtering.

@@ -16,3 +16,3 @@ Works great for JSON datasets that are too big to JSON.parse() or for situations where you want to start reading data immediately.

```
jsonfilter <filter>
jsonfilter <selector> [--match="filter expression"]
```

@@ -26,8 +26,15 @@

`"name"` matches the key `name` in an object and returns the value.
Emit the value of a particular key by naming it, e.g. `"name"` matches the key `name` in an object and returns the value:
```
$ echo '{"name": "Joe Blogs", "age": 28}' | jsonfilter "name"
"Joe Blogs"
```
`rows.*` matches any child elements of `rows`, e.g.:
```BASH
$ echo '{ name: "foo", type: "bar"}, { name: "foobar", type: "barfoo"}' | jsonfilter "name"
# "foo"
# "foobar"
$ echo '{"name": "foo", "type": "bar"}{"name": "foobar", "type": "barfoo"}' | jsonfilter "name"
"foo"
"foobar"
```

@@ -39,4 +46,4 @@

$ echo '{"rows": [ {"this object": "will be matched"}, {"so will": "this one"} ]}' | jsonfilter "rows.*"
# {"this object": "will be matched"}
# {"so will": "this one"}
{"this object": "will be matched"}
{"so will": "this one"}
```

@@ -48,3 +55,3 @@

$ echo '{"rows": [ {"doc": {"this object": "will be matched"}, "foo": "bar"} ]}' | jsonfilter "rows.*.doc"
# {'this object': 'will be matched'}
{'this object': 'will be matched'}
```

@@ -56,3 +63,19 @@

$ echo '{"rows": [ {"foo": {"bar": {"baz": {"taco": {"doc": "woo"}}}}} ]}' | jsonfilter "rows..doc"
# "woo"
"woo"
```
## matching
by default all matched objects are emitted. You can supply a custom JS expression to filter out matching objects with the `--match` option.
```BASH
$ echo '{"name": "foo", "type": "bar"}{"name": "foobar", "type": "barfoo"}' | jsonfilter "name" --match="this === 'foo'"
# foo
$ echo '{"name": "foo", "type": "bar"}{"name": "foobar", "type": "barfoo"}' | jsonfilter --match="this.name === 'foo'"
{"name": "foo", "type": "bar"}
$ echo '{"name": "foo", "type": "bar"}{"name": "foobar", "type": "barfoo"}' | jsonfilter --match="this.name.indexOf('foo') > -1"
{"name": "foo", "type": "bar"}
{"name": "foobar", "type": "barfoo"}
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc