我试图解析从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":"My status"
"favorited":false
% ...

我如何打印一个特定的字段(由-v k=文本表示)?


当前回答

为了快速提取特定键的值,我个人喜欢使用“grep -o”,它只返回正则表达式的匹配项。例如,要从tweets中获取“text”字段,如下所示:

grep -Po '"text":.*?[^\\]",' tweets.json

这个正则表达式比你想象的更健壮;例如,它可以很好地处理包含逗号和转义引号的字符串。我想再做点工作,你就能做出一个保证能提取值的程序,如果它是原子的。(如果它有嵌套,那么正则表达式当然不能这样做。)

为了进一步清除(尽管保持字符串的原始转义),您可以使用如下代码:| perl -pe 's/"text"://;/ / s / ^”;s /, / /美元”。(我这样做是为了分析。)

对于那些坚持认为你应该使用真正的JSON解析器的人——是的,这对于正确性是必不可少的,但是

To do a really quick analysis, like counting values to check on data cleaning bugs or get a general feel for the data, banging out something on the command line is faster. Opening an editor to write a script is distracting. grep -o is orders of magnitude faster than the Python standard json library, at least when doing this for tweets (which are ~2 KB each). I'm not sure if this is just because json is slow (I should compare to yajl sometime); but in principle, a regex should be faster since it's finite state and much more optimizable, instead of a parser that has to support recursion, and in this case, spends lots of CPU building trees for structures you don't care about. (If someone wrote a finite state transducer that did proper (depth-limited) JSON parsing, that would be fantastic! In the meantime we have "grep -o".)

为了编写可维护的代码,我总是使用真正的解析库。我还没有尝试过jsawk,但如果它工作得很好,这将解决第1点。

最后一个更古怪的解决方案:我写了一个脚本,使用Python json并将你想要的键提取到制表符分隔的列中;然后我通过awk的包装器,允许对列进行命名访问。这里:json2tsv和tsvawk脚本。对于这个例子,它将是:

json2tsv id text < tweets.json | tsvawk '{print "tweet " $id " is: " $text}'

这种方法没有解决第2点,比单一的Python脚本效率更低,而且有点脆弱:它强制将字符串值中的换行符和制表符规范化,以更好地处理awk的字段/记录分隔视图。但它确实让您停留在命令行上,比grep -o更正确。

其他回答

为了快速提取特定键的值,我个人喜欢使用“grep -o”,它只返回正则表达式的匹配项。例如,要从tweets中获取“text”字段,如下所示:

grep -Po '"text":.*?[^\\]",' tweets.json

这个正则表达式比你想象的更健壮;例如,它可以很好地处理包含逗号和转义引号的字符串。我想再做点工作,你就能做出一个保证能提取值的程序,如果它是原子的。(如果它有嵌套,那么正则表达式当然不能这样做。)

为了进一步清除(尽管保持字符串的原始转义),您可以使用如下代码:| perl -pe 's/"text"://;/ / s / ^”;s /, / /美元”。(我这样做是为了分析。)

对于那些坚持认为你应该使用真正的JSON解析器的人——是的,这对于正确性是必不可少的,但是

To do a really quick analysis, like counting values to check on data cleaning bugs or get a general feel for the data, banging out something on the command line is faster. Opening an editor to write a script is distracting. grep -o is orders of magnitude faster than the Python standard json library, at least when doing this for tweets (which are ~2 KB each). I'm not sure if this is just because json is slow (I should compare to yajl sometime); but in principle, a regex should be faster since it's finite state and much more optimizable, instead of a parser that has to support recursion, and in this case, spends lots of CPU building trees for structures you don't care about. (If someone wrote a finite state transducer that did proper (depth-limited) JSON parsing, that would be fantastic! In the meantime we have "grep -o".)

为了编写可维护的代码,我总是使用真正的解析库。我还没有尝试过jsawk,但如果它工作得很好,这将解决第1点。

最后一个更古怪的解决方案:我写了一个脚本,使用Python json并将你想要的键提取到制表符分隔的列中;然后我通过awk的包装器,允许对列进行命名访问。这里:json2tsv和tsvawk脚本。对于这个例子,它将是:

json2tsv id text < tweets.json | tsvawk '{print "tweet " $id " is: " $text}'

这种方法没有解决第2点,比单一的Python脚本效率更低,而且有点脆弱:它强制将字符串值中的换行符和制表符规范化,以更好地处理awk的字段/记录分隔视图。但它确实让您停留在命令行上,比grep -o更正确。

有XML文件的人可能想看看我的Xidel。它是一个命令行界面,无依赖的JSONiq处理器。(也就是说,它还支持XQuery进行XML或JSON处理。)

问题中的例子是:

 xidel -e 'json("http://twitter.com/users/username.json")("name")'

或者用我自己的非标准扩展语法:

 xidel -e 'json("http://twitter.com/users/username.json").name'

使用node . js

如果系统安装了Node.js,则可以在JSON中使用-p print和-e evaluate脚本标志。解析以提取所需的任何值。

一个简单的例子,使用JSON字符串{"foo": "bar"}并取出"foo"的值:

node -pe 'JSON.parse(process.argv[1]).foo' '{ "foo": "bar" }'

输出:

bar

因为我们可以访问cat和其他实用程序,我们可以对文件使用这个:

node -pe 'JSON.parse(process.argv[1]).foo' "$(cat foobar.json)"

输出:

bar

或包含JSON的URL等任何其他格式:

node -pe 'JSON.parse(process.argv[1]).name' "$(curl -s https://api.github.com/users/trevorsenior)"

输出:

Trevor Senior

你有多种选择。 您可以使用trdsql[1]来解析和转换JSON/CSV输入。以你为榜样;

trdsql "select attr1,attr2 from sample.json"

你也可以像SQL一样使用where子句。输出在CSV, JSON等。非常方便的工具。

根据我的经验,trdsql在处理属性嵌套值时有点问题,所以我在适当的时候使用qp[2]找到了一个解决方案。

cat sample.json | qp 'select attr1, attr2.detail.name where attr3=10'

注意这里没有FROM。

为了查看结果,您可以使用超快速命令行json查看器工具jless来查看输出[3]。

Clickhouse来了个新人。您可以从[4]中看到它的功能。

https://github.com/noborus/trdsql https://jless.io https://github.com/f5io/qp https://clickhouse.com/blog/extracting-converting-querying-local-files-with-sql-clickhouse-local

我用它从ffprobe JSON输出中提取视频时长:

MOVIE_INFO=`ffprobe "path/to/movie.mp4"  -show_streams -show_format -print_format json -v quiet`
MOVIE_SECONDS=`echo "$MOVIE_INFO"|grep -w \"duration\" |tail -1 | cut -d\" -f4 |cut -d \. -f 1`

它可以用来从任何JSON文件中提取值:

value=`echo "$jsondata" | grep -w \"key_name\" |tail -1 | cut -d\" -f4