使用UNIX工具解析JSON [英] Parsing JSON with UNIX tools

查看:188
本文介绍了使用UNIX工具解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析从curl请求返回的JSON,如下所示:

  curl'http:// twitter .com / users / username.json'| 
sed -e's / [{}] /''/ g'|
awk -v k =text'{n = split($ 0,a,,); for(i = 1; i <= n; i ++)print a [i]}'

上面将JSON分成字段,例如:

 %... 
geo_enabled:false
friends_count:245
profile_text_color:000000
status:in_reply_to_screen_name:null
source:web
truncated:false
text:我的状态
favorited:false
%...

$ b b

如何打印特定字段(由 -vk = text 表示)?

解决方案

有许多工具专门设计用于从命令行操纵JSON,将比使用Awk更容易和更可靠,例如< a href =https://stedolan.github.io/jq/ =nofollow noreferrer> jq

  curl -s'https://api.github.com/users/lambda'| jq -r'.name'

您也可以使用可能已经安装在您的系统,如Python使用 json 模块,因此避免任何额外的依赖,同时仍然有一个正确的JSON解析器的好处。下面假设你想使用UTF-8,原始的JSON应该被编码,并且是大多数现代终端使用的:



Python 2:

  export PYTHONIOENCODING = utf8 
curl -s'https://api.github.com/users/lambda'| \
python -cimport sys,json; print json.load(sys.stdin)['name']

Python 3:

  curl -s'https://api.github.com/用户/ lambda'| \ 
python3 -cimport sys,json; print(json.load(sys.stdin)['name'])

历史记录



此回答最初建议 jsawk ,这应该仍然可以工作,但是使用比 jq 有点麻烦,并且取决于正在安装的独立JavaScript解释器这比Python解释器更常见,因此上面的答案可能是更好的:

  curl -s'https:// api .github.com / users / lambda'| jsawk -a'return this.name'

这个回答原来也使用Twitter API从问题,但是该API不再工作,使得难以复制示例以进行测试,并且新的Twitter API需要API密钥,因此我已经切换到使用GitHub API,无需API密钥即可轻松使用。原始问题的第一个答案是:

  curl'http://twitter.com/users/username.json' | jq -r'.text'


I'm trying to parse JSON returned from a curl request, like so:

curl 'http://twitter.com/users/username.json' |
    sed -e 's/[{}]/''/g' | 
    awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}'

The above splits the JSON into fields, for example:

% ...
"geo_enabled":false
"friends_count":245
"profile_text_color":"000000"
"status":"in_reply_to_screen_name":null
"source":"web"
"truncated":false
"text":"My status"
"favorited":false
% ...

How do I print a specific field (denoted by the -v k=text)?

解决方案

There are a number of tools specifically designed for the purpose of manipulating JSON from the command line, and will be a lot easier and more reliable than doing it with Awk, such as jq:

curl -s 'https://api.github.com/users/lambda' | jq -r '.name'

You can also do this with tools that are likely already installed on your system, like Python using the json module, and so avoid any extra dependencies, while still having the benefit of a proper JSON parser. The following assume you want to use UTF-8, which the original JSON should be encoded in and is what most modern terminals use as well:

Python 2:

export PYTHONIOENCODING=utf8
curl -s 'https://api.github.com/users/lambda' | \
    python -c "import sys, json; print json.load(sys.stdin)['name']"

Python 3:

curl -s 'https://api.github.com/users/lambda' | \
    python3 -c "import sys, json; print(json.load(sys.stdin)['name'])"

Historical notes

This answer originally recommended jsawk, which should still work, but is a little more cumbersome to use than jq, and depends on a standalone JavaScript interpreter being installed which is less common than a Python interpreter, so the above answers are probably preferable:

curl -s 'https://api.github.com/users/lambda' | jsawk -a 'return this.name'

This answer also originally used the Twitter API from the question, but that API no longer works, making it hard to copy the examples to test out, and the new Twitter API requires API keys, so I've switched to using the GitHub API which can be used easily without API keys. The first answer for the original question would be:

curl 'http://twitter.com/users/username.json' | jq -r '.text'

这篇关于使用UNIX工具解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆