我有一个 JSON 数据文件(如下所示),我正在尝试使用查找字段值jq https://stedolan.github.io/jq/tutorial/公用事业。
如果键名称包含一个字段,则它工作正常-
其中的破折号字符。
我怎样才能得到“的值field-2", "第三场" or "字段三.url“对于下面的元素content.book1
(using 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/ https://jqplay.org/