从json文档中搜索并选择位。
jsongrep的Python项目详细描述
jsongrep是一个shell工具,用于从json文档中提取值。它支持类shell的属性名全局处理,并发出由换行符分隔的匹配值。
示例
让我们从一个真实的例子开始。
让我们从twitter的json提要中获取最后10条tweets中的偶数条http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses-public_timeline
$ curl -s 'http://twitter.com/statuses/public_timeline.json' | jsongrep '[02468].text' ARGHHHHHH. facebook is being gay 5-5 in the darts between Barney and Whitlock. Amazing. #darts I wonder if I'm still located on 5th ave? Estou de volta a internet .... Essa chuva n para ! Estou de boa com a minha familia .
是的,这正是我所期望的。
现在假设您有一个json文档,如tests/ongz.json中的文档,它看起来如下:
{ "bah" : { "feh" : true, "foo" : 3, "lah" : "songz" }, "blah" : { "lol" : "gongz" }, "arr" : [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ] }
jsongrep将允许您匹配结构模式,其中.(dot)分隔嵌套属性。
让我们全局搜索属性名称:
$ jsongrep 'b*.l*' tests/ongz.json gongz songz
也适用于数组:
$ jsongrep 'arr.?' tests/ongz.json a b c d e f g h i j
请注意,尽管数组有数字索引,但我们仍在进行全局搜索:
$ jsongrep 'arr.2?' tests/ongz.json u v w x y z
如果指定了json子树,则返回结果:
$ jsongrep 'bah' tests/ongz.json {"foo": 3, "lah": "songz", "feh": true}
语法
jsongrep当前支持属性名中的普通shell glob模式:
? Matches any one character * Matches any number of characters within a field [seq] Matches any of the characters in seq [!seq] Matches any of the characters not in seq
点是字段分隔符。
用法
Usage: jsongrep [options] [PATTERN | -e PATTERN [-e PATTERN ...]] [FILE] Parses JSON data structurally to select a subset of data. Options: --version show program's version number and exit -h, --help show this help message and exit -e PATTERNS, --pattern=PATTERNS Additional patterns to match. -D, --detect-encoding Attempts to detect the character encoding of input if LC_TYPE and file.encoding provide insufficient hints. (Slow) [default: False]
待办事项
- 支持星形(**)生成子图的非贪婪匹配
- 支持Unicode,以模式转义
- 支持扩展regexp
- 不支持模式匹配查找
- 选项:
- 模式中的属性分隔符(。默认情况下)
- 输出分隔符(默认为换行符)
- 在输出中引用字符串值?
- 输出中bool值是1/0还是真/假?
反馈
在http://github.com/dsc/jsongrep开罚单,或在dsc@less.ly给我发电子邮件。