Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Last active December 30, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/7830976 to your computer and use it in GitHub Desktop.
Save joyrexus/7830976 to your computer and use it in GitHub Desktop.
Tranform json with unix-style filters

Quick demonstration of how to filter/transform newline-delimited JSON in unix-pipeline fashion.

Run npm install to install dependencies.

Try filter.coffee < data.json or ...

cat data.json | filter.coffee

... or ...

URL='https://gist.github.com/joyrexus/7830976/raw/c11e3e10d77777acd61266658e21fea06181a024/data.json.lsv'
get.coffee $URL | filter.coffee
{ "timestamp": 3726297668, "t": [ -2371.2, -4278.67, 1973.98 ], "currentFrameRate": 57.2716, "gestures": [], "hands": [ { "timeVisible": 0.156521, "t": [ 0, 0, 0 ], "stabilizedPalmPosition": [ 71.4505, 128.013, 99.0209 ], "sphereRadius": 48.5946, "direction": [ 0.545751, 0.835466, -0.0644334 ], "id": 40, "palmNormal": [ -0.329128, 0.14301, -0.933393 ], "palmPosition": [ 71.4287, 128.014, 98.9659 ], "palmVelocity": [ -158.824, 5.34524, -402.842 ], "r": [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ], "s": 1, "sphereCenter": [ 69.6372, 132.235, 107.875 ] } ], "id": 42340, "interactionBox": { "size": [ 261.882, 261.882, 191.686 ], "center": [ 0, 240, 0 ] }, "pointables": [ { "touchZone": "hovering", "touchDistance": 0.333333, "tool": false, "direction": [ 0.733056, 0.233466, -0.638845 ], "handId": -1, "id": 36, "length": 34.6309, "stabilizedTipPosition": [ 82.247, 253.895, 62.029 ], "timeVisible": 0.034789, "tipPosition": [ 82.247, 253.895, 62.029 ], "tipVelocity": [ 5.27358, 491.535, -152.441 ] } ], "r": [ [ 0.281917, -0.214347, 0.935189 ], [ 0.0611448, -0.96873, -0.240467 ], [ 0.957488, 0.124974, -0.259995 ] ], "s": -153.668}
{ "timestamp": 3726315050, "t": [ -2369.36, -4269.78, 1973.25 ], "currentFrameRate": 57.3491, "gestures": [], "hands": [ { "timeVisible": 0, "t": [ -1.20433, 0.718803, -2.2152 ], "stabilizedPalmPosition": [ 69.0469, 129.435, 94.5849 ], "sphereRadius": 49.1321, "direction": [ 0.400563, 0.913539, -0.0706783 ], "id": 40, "palmNormal": [ -0.289082, 0.0528033, -0.955847 ], "palmPosition": [ 69.0201, 129.451, 94.5354 ], "palmVelocity": [ -138.572, 82.7066, -254.885 ], "r": [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ], "s": 1, "sphereCenter": [ 69.7615, 131.942, 107.435 ] } ], "id": 42341, "interactionBox": { "size": [ 261.882, 261.882, 191.686 ], "center": [ 0, 240, 0 ] }, "pointables": [ { "touchZone": "hovering", "touchDistance": 0.333333, "tool": false, "direction": [ 0.734636, 0.315176, -0.600811 ], "handId": -1, "id": 36, "length": 35.5817, "stabilizedTipPosition": [ 83.657, 259.111, 61.1662 ], "timeVisible": 0.052171, "tipPosition": [ 83.657, 259.111, 61.1662 ], "tipVelocity": [ 116.457, 520.129, -109.216 ] } ], "r": [ [ 0.281917, -0.214347, 0.935189 ], [ 0.0611448, -0.96873, -0.240467 ], [ 0.957488, 0.124974, -0.259995 ] ], "s": -153.668}
{ "timestamp": 3726332453, "t": [ -2375.22, -4270.91, 1968.8 ], "currentFrameRate": 57.3827, "gestures": [], "hands": [ { "timeVisible": 0.017403, "t": [ -14.2588, -11.0807, -10.8865 ], "stabilizedPalmPosition": [ 67.2896, 130.044, 92.2266 ], "sphereRadius": 48.7864, "direction": [ 0.287253, 0.938918, -0.189522 ], "id": 40, "palmNormal": [ -0.2543, -0.116006, -0.960143 ], "palmPosition": [ 67.2629, 130.053, 92.1908 ], "palmVelocity": [ -100.969, 34.6039, -134.727 ], "r": [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ], "s": 1, "sphereCenter": [ 68.837, 130.112, 106.86 ] } ], "id": 42342, "interactionBox": { "size": [ 261.882, 261.882, 191.686 ], "center": [ 0, 240, 0 ] }, "pointables": [ { "touchZone": "none", "touchDistance": 0.333333, "tool": false, "direction": [ 0.891829, 0.246817, -0.379109 ], "handId": -1, "id": 36, "length": 35.8098, "stabilizedTipPosition": [ 84.1415, 263.188, 60.2956 ], "timeVisible": 0.069574, "tipPosition": [ 84.1415, 263.188, 60.2956 ], "tipVelocity": [ 23.3885, 358.329, -98.087 ] }, { "touchZone": "hovering", "touchDistance": 0.333333, "tool": false, "direction": [ 0.331701, -0.409046, -0.850092 ], "handId": 40, "id": 57, "length": 25.103, "stabilizedTipPosition": [ 37.5933, 121.8, 62.5981 ], "timeVisible": 0, "tipPosition": [ 37.5933, 121.8, 62.5981 ], "tipVelocity": [ -1431.06, -1397.34, -869.242 ] } ], "r": [ [ 0.277301, -0.232373, 0.932259 ], [ 0.0384933, -0.966845, -0.252444 ], [ 0.960012, 0.105889, -0.259162 ] ], "s": -153.596}
#!/usr/bin/env coffee
thru = require 'through'
split = require 'split'
filter = ->
write = (data) ->
if data
d = JSON.parse(data).hands[0].palmPosition
@queue JSON.stringify(d) + "\n" # queue attributes of interest
end = -> @queue null # append EOF string if desired
thru(write, end)
process.stdin
.pipe(split())
.pipe(filter())
.pipe(process.stdout)
#!/usr/bin/env coffee
request = require 'request'
url = process.argv[2]
request(url).pipe(process.stdout)
{
"dependencies": {
"split": "~0.2",
"through": ">=2",
"event-stream": ">=3",
"request": "~2.28.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment