jq,什么时候用点,什么时候不用
jq, when to use the dot and when not
{
"data": {
"search": {
"repositoryCount": 24,
"edges": [
{
"node": {
"name": "leumi-leumicard-bank-data-scraper",
"url": "https://github.com/Urigo/leumi-leumicard-bank-data-scraper",
"description": "Open bank data for Leumi bank and Leumi card credit card",
. . .
},
{
"node": {
"name": "puppeteer-demo",
"url": "https://github.com/xJkit/puppeteer-demo",
"description": "A demo for website scrapping by my puppet :>",
. . .
如果要使用jq
到select data
,那么它前面需要一个点(.
)。即:
jq 'data'
jq: error: data/0 is not defined at <top-level>, line 1:
data
jq: 1 compile error
然而 jq '.data'
工作正常,selected 数据变为:
{
"search": {
"repositoryCount": 24,
"edges": [
{
...
如果用jq
到selectsearch
,后管道,那么不会 前面需要一个点 (.
)。即:
$ jq '.data | {.search} '
jq: error: syntax error, unexpected FIELD (Unix shell quoting issues?) at <top-level>, line 1:
.data | {.search}
jq: 1 compile error
然而 jq '.data.search'
工作正常。
此外,一个更复杂的例子,
jq '.data.search.edges[] | {node} '
工作正常,但是
jq '.data.search.edges[] | {node.name} '
给出:
jq: error: syntax error, unexpected FIELD, expecting '}' (Unix shell quoting issues?) at <top-level>, line 1:
.data.search.edges[] | {node.name}
jq: 1 compile error
所以,总而言之,我很困惑何时使用点 (.
) 以及何时不使用点 jq
。请帮忙。谢谢。
如果您从考虑所涉及的完整数据管道开始,并认识到表达式何时只是缩写形式,也许事情会更清楚。
在构建一个完整的管道时,关于"dots"的基本原则很简单:
.
指的是输入
.foo
用于访问键 "foo" 的值
.[]
用于扩展数组或对象
有许多允许的缩写。最让您感到困惑的两个是:
.foo.bar
对于 .foo | .bar
{foo}
对于 {"foo": .foo}
另一个重要的缩写是:
E[]
for E | .[]
其中 E 是一个合适的紧凑表达式
记住这些原则和例子,你应该能够掌握jq文档中解释的细节。
{
"data": {
"search": {
"repositoryCount": 24,
"edges": [
{
"node": {
"name": "leumi-leumicard-bank-data-scraper",
"url": "https://github.com/Urigo/leumi-leumicard-bank-data-scraper",
"description": "Open bank data for Leumi bank and Leumi card credit card",
. . .
},
{
"node": {
"name": "puppeteer-demo",
"url": "https://github.com/xJkit/puppeteer-demo",
"description": "A demo for website scrapping by my puppet :>",
. . .
如果要使用jq
到select data
,那么它前面需要一个点(.
)。即:
jq 'data'
jq: error: data/0 is not defined at <top-level>, line 1:
data
jq: 1 compile error
然而 jq '.data'
工作正常,selected 数据变为:
{
"search": {
"repositoryCount": 24,
"edges": [
{
...
如果用jq
到selectsearch
,后管道,那么不会 前面需要一个点 (.
)。即:
$ jq '.data | {.search} '
jq: error: syntax error, unexpected FIELD (Unix shell quoting issues?) at <top-level>, line 1:
.data | {.search}
jq: 1 compile error
然而 jq '.data.search'
工作正常。
此外,一个更复杂的例子,
jq '.data.search.edges[] | {node} '
工作正常,但是
jq '.data.search.edges[] | {node.name} '
给出:
jq: error: syntax error, unexpected FIELD, expecting '}' (Unix shell quoting issues?) at <top-level>, line 1:
.data.search.edges[] | {node.name}
jq: 1 compile error
所以,总而言之,我很困惑何时使用点 (.
) 以及何时不使用点 jq
。请帮忙。谢谢。
如果您从考虑所涉及的完整数据管道开始,并认识到表达式何时只是缩写形式,也许事情会更清楚。
在构建一个完整的管道时,关于"dots"的基本原则很简单:
.
指的是输入.foo
用于访问键 "foo" 的值
.[]
用于扩展数组或对象
有许多允许的缩写。最让您感到困惑的两个是:
.foo.bar
对于.foo | .bar
{foo}
对于{"foo": .foo}
另一个重要的缩写是:
E[]
forE | .[]
其中 E 是一个合适的紧凑表达式
记住这些原则和例子,你应该能够掌握jq文档中解释的细节。