蟒蛇的JQ
jqfp的Python项目详细描述
JQFPY
JQ太难了
JQ太难了,至少对我来说。
例如,仅当use为true时从下面的json数据中提取密钥名。
{"apps":{"foo":{"use":true},"bar":{"use":true},"boo":{"use":true},"bee":{"use":false}}}
JQ的答案是什么?(花了30多分钟,我过去的挑战)。
$ cat data.json | jq '.apps | . as $$o | keys | map(select($$o[.].use))'[ "bar", "boo", "foo"]
如果你有python的知识,这是很小的一行程序,不是吗?
$ cat data.json | jqfpy '[k for k, opts in get("apps").items() if opts["use"]]'[ "foo", "bar", "boo"]
(get()
是一个特殊函数,类似于json.load(sys.stdin).get
。)
安装
$ pip install jqfpy
如何使用
描述语法
托多。
教程
这是jqfpy版本的jq's Tutorial
$aliasjsonDATA="curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'"# jq. $ jsonDATA | jq '.'# jqfpy. $ jsonDATA | jqfpy 'get()'
# jq. $ jsonDATA | jq '.[0]'# jqfpy. $ jsonDATA | jqfpy 'get()[0]'
# jq. $ jsonDATA | jq '.[0] | {message: .commit.message, name: .commit.committer.name}'# jqfpy. $ jsonDATA | jqfpy 'd = get()[0]; {"message": get("commit/message", d), "name": get("commit/committer/name", d)}'# or $ jsonDATA | jqfpy '{"message": get("0/commit/message"), "name": get("0/commit/committer/name")}'
# jq. $ jsonDATA | jq '.[] | {message: .commit.message, name: .commit.committer.name}'# jqfpy. $ jsonDATA | jqfpy --squash 'L = get(); [{"message": get("commit/message", d), "name": get("commit/committer/name", d)} for d in L]'
# jq. $ jsonDATA | jq '[.[] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]'# jqfpy. $ jsonDATA |'L = get(); [{"message": get("commit/message", d), "name": get("commit/committer/name", d), "parents": [p["html_url"] for p in d["parents"]]} for d in L]'# or (using h.pick)$ jsonDATA |'L = get(); [h.pick("commit/message@message", "commit/committer/name@name", "parents[]/html_url@parents", d=d) for d in L]'
附加值
其他格式支持
jqfpy支持其他格式(但这是一个实验特性)
- 山药
- LTSV
如果你想使用yaml支持的版本。通过下面的命令安装。
$ pip install jqfpy[yaml]
使用--input-format,-i
选项和--output-format,-o
选项调用jqfpy。
02data.yaml
person:name:fooage:20nickname:fool
$ cat 02data.yaml | jqfpy -i yaml 'get("person")'{ "name": "foo", "age": 20, "nickname": "fool"}$ cat 02data.yaml | jqfpy -i yaml -o ltsv 'get("person")'name:foo age:20 nickname:fool
帮助函数
包括助手函数。
- pick()
- 省略()
- 展平()
- chunk()
- loadfile()
- dumpfile()
pick()
$ cat 02data.yaml | jqfpy -i yaml 'h.pick("person/name", "person/age")'{ "person": { "name": "foo", "age": 20 }}$ cat 02data.yaml | jqfpy -i yaml 'h.pick("person/name@name", "person/age@age")'{ "name": "foo", "age": 20}
省略()
$ cat 02data.yaml | jqfpy -i yaml 'h.omit("person/nickname")'{ "person": { "name": "foo", "age": 20 }}
扁平化()
$ seq 15| jqfpy --slurp -c 'L = get(); [L, L]'[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]$ seq 15| jqfpy --slurp -c 'L = get(); h.flatten([L, L], n=1)'[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
chunk()
$ seq 110| jqfpy --slurp -c 'h.chunk(get(), n=3)'[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
loadfile()
$ ls *.json a.json b.json$echo'["a", "b"]'| jqfpy '{name: h.loadfile(f"{name}.json") for name in get()}'
--here
和--relative-path
选项。
# see ./x.json $ jqfpy 'h.loadfile("x.json")' a/b/main.json # see ./a/b/x.json $ jqfpy --here a/b/ 'h.loadfile("x.json")' a/b/main.json # see ./a/b/x.json $ jqfpy --relative-path 'h.loadfile("x.json")' a/b/main.json
转储文件()
$ echo {"person0.json": {"name": "foo", "age": 20}, "person1.json": {"name": "bar}} | jqfpy '[h.dumpfile(v, fname) for fname, v in get().item()]' > /dev/null
具有--additionals
匹配.py
importredefmatch(rx,text):iftextisNone:returnFalsereturnre.search(rx,text)
$ cat examples/additionals/00data.json | jqfpy --additionals=./match.py '[d for d in get("constraint") if h.match("^1\..+", d.get("version"))]'[ { "name": "github.com/Masterminds/vcs", "version": "1.11.0" }, { "name": "github.com/boltdb/bolt", "version": "1.0.0" }]
0.6.1
- 修正
--show-none
选项,总是被忽略,在0.6.0中
0.6.0
- 添加
--show-none
(默认情况下,如果返回值为none,则不写入stdout) - 添加原始格式
- 添加短选项
-a
和-u
(删除--ascii-output
asa-a
)
0.5.4
- 更改参数顺序,h.dumpfile(41)
- 修复get()的错误(37)
0.5.3
- 修复h.dumpfile()的错误
0.5.2
- 添加h.loadfile()函数(35)
0.5.1
- h.dumpfile(),带raw选项
0.5.0
- 删除python 3.5
- yaml输出,包含哈希的单行字符串,总是带引号(28)
- 将“/foo”视为“foo”(lstripped)
- 添加h.dumpfile()函数(31)
0.4.2
- 支持Python3.4
- 自定义附加选项(--additionals选项)
- 新的助手——h.flatten、h.flatten1和h.chunk
0.4.1
- 当json加载时,ordered是默认值
0.4.0
- 实验性LTSV支持
- 添加助手函数(h.omit(),h.pick())
- 扩展get()函数的函数
- 重命名选项(-show code only to--show code)
0.3.2
- 在没有参数的情况下调用时显示帮助的future将被删除
0.3.0
- 实验性的yaml格式支持
0.2.0
--slurp
和--unbuffered
的正确行为
- 支持通过类似json指针的格式访问数据
- 紧凑型输出支持
- 多文件支持
- 仅执行一次代码
0.1.0
- 添加一些选项
- nocode与
jq .
(js == jq .
)相同 - 出现错误时显示pycode
0.0.1
- 首次发布