从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给我发电子邮件。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用split函数分割字符串,但没有得到期望的结果   未找到包含derby数据库嵌入架构的sql Java桌面应用程序错误   java elasticsearch vs solr用于定制全文搜索系统   java Android:创建没有startOffset的动画延迟?   java如何查看其他应用程序接收的数据?   java如何在Linux中使用D和classpath选项运行jar文件   java和域设计最佳实践   具有相同内存位置的java数组,将显示为输出   连接到java中的elasticsearch?   Java Playframework重定向到带有Json负载的外部url   java无法在Android平台上使用InputStream为蓝牙socket创建ObjectInputStream   使用POI将Excel日期转换为Java日期,年份未正确显示   oracle从数据库层还是Java层调用webservice?