我有一个 JSON 数据文件(如下所示),我正在尝试使用jq实用程序查找字段值。
-
如果键名中包含短划线字符,则它工作正常,但字段除外。
如何获取(至少使用)下元素的“ field-2 ”、“ field-three ”或“ field-three.url ”的值?content.book1
jq
我尝试了以下方法来获取值,但是对于键名中包含破折号的字段,它给了我以下错误-
。我试图反斜杠-
字符,但这也无济于事。
发现的错误类型:
jq: error (at <stdin>:27): null (null) and number (2) cannot be subtracted
jq: 1 compile error
jq: error: three/0 is not defined at <top-level>
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
命令:
$ cat /tmp/my.data.json
{
"pages": {
"book1": [
"page1",
"page2-para1",
"page3-para1-sentence1",
"page3-para2-sentence3-word4"
]
},
"content": {
"book1": {
"name": "giga",
"url": "-",
"field1": "value1",
"field-2": "value-2",
"field-three": {
"name": "THIRD",
"url": "book1/field-three/",
"short-url": "book1/field-three/chota-chetan"
},
"authur": {
"name": "lori CHUCK",
"displayIndex": 4
},
"route": "/in-gc/hindi-chini-bhai-bhai"
}
}
}
$ cat /tmp/my.data.json| jq ".pages"
{
"book1": [
"page1",
"page2-para1",
"page3-para1-sentence1",
"page3-para2-sentence3-word4"
]
}
$ cat /tmp/my.data.json| jq ".pages.book1[0]"
"page1"
$ cat /tmp/my.data.json| jq ".pages.book1[1]"
"page2-para1"
$ cat /tmp/my.data.json| jq ".content"
{
"book1": {
"name": "giga",
"url": "-",
"field1": "value1",
"field-2": "value-2",
"field-three": {
"name": "THIRD",
"url": "book1/field-three/"
},
"authur": {
"name": "lori CHUCK",
"displayIndex": 4
},
"route": "/in-gc/hindi-chini-bhai-bhai"
}
}
$ cat /tmp/my.data.json| jq ".content.book1"
{
"name": "giga",
"url": "-",
"field1": "value1",
"field-2": "value-2",
"field-three": {
"name": "THIRD",
"url": "book1/field-three/"
},
"authur": {
"name": "lori CHUCK",
"displayIndex": 4
},
"route": "/in-gc/hindi-chini-bhai-bhai"
}
$ cat /tmp/my.data.json| jq ".content.book1.name"
"giga"
$ cat /tmp/my.data.json| jq ".content.book1.field1"
"value1"
$ cat /tmp/my.data.json| jq ".content.book1.field-2"
jq: error (at <stdin>:27): null (null) and number (2) cannot be subtracted
$ cat /tmp/my.data.json| jq ".content.book1.field-three"
jq: error: three/0 is not defined at <top-level>, line 1:
.content.book1.field-three
jq: 1 compile error
$ cat /tmp/my.data.json| jq ".content.book1.field-three.url"
jq: error: three/0 is not defined at <top-level>, line 1:
.content.book1.field-three.url
jq: 1 compile error
$ cat /tmp/my.data.json| jq ".content.book1.field\-2"
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.content.book1.field\-2
jq: 1 compile error
$ cat /tmp/my.data.json| jq ".content.book1.field\\-2"
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.content.book1.field\-2
jq: 1 compile error
$ cat /tmp/my.data.json| jq ".content.book1.'field-2'"
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.content.book1.'field-2'
jq: 1 compile error
$ cat /tmp/my.data.json| jq ".content.book1.authur"
{
"name": "lori CHUCK",
"displayIndex": 4
}
$ cat /tmp/my.data.json| jq ".content.book1.route"
"/in-gc/hindi-chini-bhai-bhai"
$
PS:
我已经知道egrep
了,所以这不是我要找的。
cat /tmp/my.data.json| jq ".content.book1"|egrep "short-url|field-2"
"field-2": "value-2",
"short-url": "book1/field-three/chota-chetan"
有人在这里做得很好:https ://jqplay.org/