bash 在shell中逐列解析Json数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19511851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing Json data columnwise in shell
提问by The_Lost_Avatar
When I run a command I get a response like this
当我运行命令时,我得到这样的响应
{
"status": "available",
"managed": true,
"name":vdisk7,
"support":{
"status": "supported"
},
"storage_pool": "pfm9253_pfm9254_new",
"id": "ff10abad"-2bf-4ef3-9038-9ae7f18ea77c",
"size":100
},
and hundreds of this type of lists or dictionaries I want a command that does such sort of a thing
以及数百个这种类型的列表或字典 我想要一个执行此类操作的命令
if name = "something",
get the id
Any links that would help me in learning such sort of commands would be highly appreciated
任何可以帮助我学习此类命令的链接将不胜感激
I have tried
我试过了
awk '{if ($2 == "something") print $0;}'
awk '{if ($2 == "something") 打印 $0;}'
But I think the response is in Json so the colum wise awk formatting is not working.
但我认为响应是在 Json 中,所以 colum wise awk 格式不起作用。
Also it's just a single command that I need to run so I would prefer not to use any external library.
此外,它只是我需要运行的一个命令,因此我不想使用任何外部库。
回答by Waterlink
JSON parser is better for this task
JSON 解析器更适合此任务
awk
and sed
are utilities to parse line-oriented text, but not json. What if your json formatting will change ? (some lines will go on one line ?).
awk
和sed
是解析面向行的文本的实用程序,但不是 json。如果您的 json 格式会改变怎么办?(有些行会在一行上?)。
You should use any standard json parser out there. Or use some powerful scripting language, such as PHP, Python, Ruby, etc.
您应该使用任何标准的 json 解析器。或者使用一些强大的脚本语言,比如PHP、Python、Ruby等。
I can provide you with example on how to do it with python.
我可以为您提供有关如何使用 python 执行此操作的示例。
What if I can't use powerful scripting language ?
如果我不能使用强大的脚本语言怎么办?
If you totally unable to use python, then there is utility jq
out there: link
如果您完全无法使用 python,那么那里有实用程序jq
:链接
If you have some recent distro, jq
maybe already in repositories (example: Ubuntu 13.10 has it in repos).
如果您有一些最近的发行版,jq
可能已经在存储库中(例如:Ubuntu 13.10 在存储库中有它)。
I can use python!
我可以使用蟒蛇!
I would do that using simple python inline script.
我会使用简单的 python 内联脚本来做到这一点。
For example we have some some_command
that returns json as a result.
例如,我们有一些some_command
结果返回 json。
We have to get value of data["name"]
.
我们必须得到 的值data["name"]
。
Here we go:
开始了:
some_command | python -c "import json, sys; print json.load(sys.stdin)['name']"
It will output vdisk7
in your case
它会vdisk7
在你的情况下输出
For this to work you need to be sure, json is fully valid.
为此,您需要确保 json 完全有效。
If you have a list of json objects:
如果您有一个 json 对象列表:
[
{
...
"name": "vdisk17"
...
},
{
...
"name": "vdisk18"
...
},
{
...
"name": "vdisk19"
...
},
...
]
You could use some list comprehensions:
您可以使用一些列表推导式:
some_command | python -c "import json, sys; [sys.stdout.write(x['name'] + '\n') for x in json.load(sys.stdin)]"
It will output:
它会输出:
vdisk17
vdisk18
vdisk19