json检查命令行客户端
json-inspect的Python项目详细描述
用于检查和处理json文件的命令行工具。当前 支持的子命令包括
目录
安装
您只需使用python实用程序的普通方法进行安装,例如
pip install json-inspect
子命令
每个子命令都有自己的帮助文件和选项,并提供不同的服务 目的。请务必阅读每个命令的文档,因为它可能没有 像其他人一样工作。查看最新的文档 可用的子命令,运行带有-h选项的实用程序 提供子命令。
$ json-inspect -h usage: json-inspect [-h] [-v] [-f FILE] {histo,ext,keys,validate,format} ... Utility for inspecting JSON files/input positional arguments: {histo,ext,keys,validate,format} histo Create histograms from JSON values ext Extract values from JSON keys Lists keys in a JSON document validate Validate text input as JSON (coming soon maybe) format Nicely format JSON input (coming soon maybe) optional arguments: -h, --help show this help message and exit -v, --version show program's version number and exit -f FILE, --file FILE JSON file to read in. If not provided STDIN will be used
请注意,有一些全局选项。最重要的是 此实用程序需要某种json输入才能工作。这是 使用全局-f选项定义,或通过 STDIN。
历史
如果您正在处理大量的json对象/数组,那么它可能 了解哪些字段存在,它们包含哪些值,以及 两者的频率。从帮助文件开始
usage: json-inspect histo [-h] [-p PREFIX] [-c] paths [paths ...] Generate a histogram based on values found using a JSON search expression positional arguments: paths search paths to create histograms for, prefixed with optional value from the --prefix option optional arguments: -h, --help show this help message and exit -p PREFIX, --prefix PREFIX String to prefix all search-paths with -c, --conflate Conflate non-empty responses to the same value
- -p选项将允许您为所有搜索路径加前缀。 如果要执行多个具有 深嵌套json搜索的公共前缀。
- -c选项将把直方图合并为两个值, __none__和__some__。第一个意思是没有键/值 在您的搜索路径和后一个搜索路径中为给定的查找找到 这意味着找到了a值。如果你只关心 关于出现的频率。
搜索路径是一个点分隔的表达式,用于遍历json 物体。让我们开始的一个例子是
[].*.user.demographic.regions.[].name
首先,请注意搜索表达式包含3种类型的标记
- []-表示数组。数组中的每个项都被收集并 将由下一个标记处理
- *-表示应在其中收集所有字段的对象 将由下一个标记处理
- TOKEN-对象的字段值。它的价值将被收集 并由下一个标记处理
对于我们上面的例子,下面的json会让我们满意 文档
[{"facebook":{"user":{"demographic":{"regions":[{"name":"US"},{"name":"Kentucky"},{"name":"Louisville"}]}}},"google":{"user":{"demographic":{"regions":[{"name":"US"},{"name":"Kentucky"},{"name":"Highland Heights"}]}}}},{"twitter":{"user":{"demographic":{"regions":[{"name":"UK"},{"name":"Wales"}]}}}}]
运行histo子命令,我们将得到如下输出
cat test.json | json-inspect histo '[].*.user.demographic.regions.[].name'[].*.user.demographic.regions.[].name: Highland Heig |######################### | (1) US |################################################## | (2) Louisville |######################### | (1) Kentucky |################################################## | (2) UK |######################### | (1) Wales |######################### | (1)
条形图表示相对于 最右边列中每个元素的查找总数。
外部
提取命令ext用于从json中提取数据 文件夹。它支持与^{tt11}相同的前缀和搜索表达式$ 以及其他一些价值输出选项。
$ json-inspect ext -h usage: json-inspect ext [-h] [-p PREFIX] [-d DELIM] [-F] paths [paths ...] Extract values from JSON using a JSON search expression positional arguments: paths search paths to return values for optional arguments: -h, --help show this help message and exit -p PREFIX, --prefix PREFIX String to prefix all search-paths with -d DELIM, --delimiter DELIM String to delimit all results by -F, --flatten Flatten array and object values. For objects, only the values (not the keys) are retained in the falttened values
使用来自histo命令的相同输入测试json文件,我们可以 参见输出示例
$ cat test.json | json-inspect ext "[].*.user.demographic.regions.[].*" Louisville,Kentucky,US,Highland Heights,Kentucky,US,Wales,UK $ cat test.json | json-inspect ext -d '|' "[].*.user.demographic.regions.[].*" Louisville|Kentucky|US|Highland Heights|Kentucky|US|Wales|UK
按键
keys命令用于列出json中找到的所有键 文件。可以提供标志来筛选要提取的键。
$ json-inspect keys -h usage: json-inspect keys [-h] [-n] [-o] [-p] [-e] List all keys within the JSON document using a period-delimited notation similar to search-paths optional arguments: -h, --help show this help message and exit -n, --exclude-null Exclude keys that contain a null value -o, --exclude-empty-objects Exclude keys that contain an empty object -p, --exclude-empty-primitives Exclude keys that contain an empty primitive value (zero and empty string) -e, --exclude-empty-array Exclude keys that contain an empty array value
使用
"null": null, "empty_object": {}, "empty_array": [], "empty_string": "", "empty_int": 0, "empty_float": 0.0,
我们可以进行一些示例调用,例如
bash $ cat test.json | json-inspect keys facebook.null facebook.empty_object facebook.user.demographic.regions.name facebook.empty_float facebook.empty_array # ...
bash # filter all keys with empty values $ cat test.json | json-inspect keys -nope facebook.user.demographic.regions.name google.user.demographic.regions.name twitter.user.demographic.regions.name
计划改进
[]重构可测试的代码(可能编写一些测试)[]添加支持 对于**