Skip to content

Instantly share code, notes, and snippets.

@chernjie
Created April 11, 2017 09:17
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 chernjie/0408605d8a39cea56fccd4f9099f4ba5 to your computer and use it in GitHub Desktop.
Save chernjie/0408605d8a39cea56fccd4f9099f4ba5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
json2markdowntable () {
local headers=$@
echo $headers | sed "s, , | ,g"
seq $# | while read i; do echo " --- "; done | xargs | sed "s, , | ,g"
json -a $headers -d" | "
}
# json2markdowntable $@
filterByKeys() {
local headersjs="$(echo $@ | xargs -n1 | sed -e s,^,+, -e s.$.+,. | xargs | sed -e s,+,"'",g -e s,^,"[", -e s,$,"]",)"
json -ga -E "
var headersjs = $headersjs;
var that = this;
Object.keys(that).forEach(function(el){
if (that[el] !== null && typeof that[el] === 'object') {
Object.keys(that[el]).forEach(function(ell){
var key = [el, ell].join('.');
that[key] = that[el][ell];
delete that[el][ell];
})
}
if (headersjs.indexOf(el) === -1) {
delete that[el];
}
})
" | json -g
}
filterByKeys $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment