json jq:嵌套对象,提取顶级id并从内部对象中提取一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27562424/
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
jq: nested object, extract top-level id and lift a value from internal object
提问by tripleee
Given the following xample.json;
鉴于以下xample.json;
[
{
"id": 12345678,
"stuff": { "book": "shelf", "hook": "line", "took": "off", "info-spec": 12 },
"votes": 23
},
{
"id": 12345679,
"stuff": { "book": "maker", "hook": "sinker", "took": "pisin", "info-spec": 23 },
"votes": 1
}
]
I can extract the idand voteseasily:
我可以轻松提取id和votes:
$ jq '.[] | { id, votes }' xample.json
{
"votes": 23,
"id": 12345678
}
{
"votes": 1,
"id": 12345679
}
But what would the query look like to extract idand stuff.info-spec? The obvious (to me) syntax doesn't work at all:
但是,将查询的样子来提取id和stuff.info-spec?明显的(对我来说)语法根本不起作用:
$ jq '.[] | { id, stuff.info-spec }' xample.json
error: syntax error, unexpected '.', expecting '}'
.[] | { id, stuff.info-spec }
^
1 compile error
I tried stuff[info-spec]and stuff["info-spec"]as well but, well, I just don't seem to have any idea how this should be done.
我也尝试过stuff[info-spec],stuff["info-spec"]但是,好吧,我似乎不知道应该如何做到这一点。
The dash in the key name seems to complicate matters even further, but my limited understanding is that I can work around that with double quotes.
键名中的破折号似乎使问题更加复杂,但我有限的理解是我可以用双引号解决这个问题。
$ sed 's/votes/vo-tes/g' xample.json | jq '.[] | { id, "vo-tes" }'
gives the expected output (i.e. similar to above without the dash in "vo-tes").
给出预期的输出(即类似于上面没有“vo-tes”中的破折号)。
I can extract book:
我可以提取book:
$ jq '.[] | .stuff.book' xample.json
but again cannot figure out the syntax for idand book; but also, I cannot extract info-specwith the same syntax:
但又无法弄清楚idand的语法book;而且,我无法info-spec使用相同的语法提取:
$ jq '.[] | .stuff."info-spec"' xample.json
error: syntax error, unexpected QQSTRING_START, expecting IDENT
.[] | .stuff."info-spec"
^
1 compile error
If I take out the quotes, the error message is (predictably) different:
如果我取出引号,错误消息(可预测)是不同的:
$ jq '.[] | .stuff.info-spec' xample.json
error: spec is not defined
.[] | .stuff.info-spec
^^^^
1 compile error
But hey, this works:
但是,嘿,这有效:
$ jq '.[] | .stuff["info-spec"] ' xample.json
12
23
My desired output for this example, then, is
那么,我想要的这个例子的输出是
{
"info-spec": 12,
"id": 12345678
}
{
"info-spec": 23,
"id": 12345679
}
I've looked at the FAQand the jqCookbookbut I can't seem to find anything about a syntax for "lifting" an item from within an object inside another object.
我已经查看了FAQ和jqCookbook,但我似乎找不到任何关于从另一个对象内的一个对象中“提升”一个项目的语法的任何信息。
采纳答案by tripleee
I managed to figure it out.
我设法弄明白了。
$ jq '.[] | { id, "info-spec": .stuff["info-spec"] }' xample.json
{
"info-spec": 12,
"id": 12345678
}
{
"info-spec": 23,
"id": 12345679
}
The key here seems to be to use the newkey: .complex["key"]notation for lifting.
这里的关键似乎是使用newkey: .complex["key"]提升的符号。
回答by Hans Z.
interesting, the issue is indeed the "-" character and this works for me:
有趣的是,问题确实是“-”字符,这对我有用:
jq '.[] | { id, "info-spec": .stuff."info-spec" }' xample.json
{
"id": 12345678,
"info-spec": 12
}
{
"id": 12345679,
"info-spec": 23
}
yet your jqdoes not seem to like that syntax as it broke on the following and mine does not:
但是您jq似乎不喜欢这种语法,因为它在以下方面中断了,而我的则不喜欢:
jq '.[] | .stuff."info-spec"' xample.json
12
23
I use:
我用:
jq --version
jq-1.4
edit: looks like a version issue with <1.4 indeed: https://github.com/stedolan/jq/issues/38
编辑:看起来确实是 <1.4 的版本问题:https: //github.com/stedolan/jq/issues/38

