使用 JQ 工具实用程序解析或查看 JSON 数据字段,其中字段名称在键名中有一个“-”破折号
Parse or view JSON data fields using JQ tool utility where field names have a "-" dash in the key name
我有一个 JSON 数据文件(如下所示),我正在尝试使用 jq 实用程序查找字段值。
如果键名称中包含 -
短划线字符,则它工作正常,但字段除外。
如何获取“field-2”、“field-three”或“”的值field-three.url" for element under content.book1
(using jq
at least)?
我尝试了以下方法来获取值,但对于键名称中包含破折号 -
的字段,它给我以下错误。我试图反斜杠 -
字符,但这也没有帮助。
找到的错误类型:
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/
我不知道 jq,但是你把 python 放在标签里所以:
$ cat test.json | python -c "import sys, json; print(json.load(sys.stdin)['content']['book1']['field-three']['name'])"
THIRD
或不带管道:
$ python -c "import json; print(json.load(open('test.json'))['content']['book1']['field-three']['name'])"
如 jq manual 中所述,要处理包含 -
等非标识符字符的键,您可以使用双引号。
从 shell 开始,如果您在过滤器周围使用单引号,这是最简单的。例如,尝试以下命令:
cat /tmp/my.data.json | jq '.pages'
cat /tmp/my.data.json | jq '.pages.book1[0]'
cat /tmp/my.data.json | jq '.pages.book1[1]'
cat /tmp/my.data.json | jq '.content'
cat /tmp/my.data.json | jq '.content.book1'
cat /tmp/my.data.json | jq '.content.book1.name'
cat /tmp/my.data.json | jq '.content.book1.field1'
cat /tmp/my.data.json | jq '.content.book1."field-2"'
cat /tmp/my.data.json | jq '.content.book1."field-three"'
cat /tmp/my.data.json | jq '.content.book1."field-three".url'
cat /tmp/my.data.json | jq '.content.book1.authur'
cat /tmp/my.data.json | jq '.content.book1.route'
"-"用于jq中的否定。对于带有“-”等特殊字符的键名,不能使用简化的“.keyname”语法。有几种选择,但最稳健的是简单地使用 .["KEY NAME"]
形式,在链接时可以缩写为 ["KEY NAME"],例如对于 .a | .["b-c"]
,.a["b-c"]
是 shorthand。
如有疑问,请明确使用管道。
更多信息请查阅jq手册and/orhttps://github.com/stedolan/jq/wiki/FAQ
Hm..花了一些时间,但最后似乎我们需要 DOUBLE QUOTE 它并反斜杠只是包含 -
的任何键名的双引号在它的名字里。
$ cat /tmp/my.data.json| jq ".content.book1.\"field-2\""
"value-2"
$ cat /tmp/my.data.json| jq ".content.book1.\"field-three\".\"url\""
"book1/field-three/"
或
如果您将所有内容都用单引号 '
括起来,那么我们不需要反斜杠 "
双引号,而是对名称中带有 -
的键名使用双引号。
$ cat /tmp/my.data.json| jq '.content.book1."field-three"."url"'
"book1/field-three/"
希望对您有所帮助!从 https://jqplay.org/
拿了一些 help/hint
查看更多:https://github.com/stedolan/jq/issues/38#issuecomment-9770240
我有一个 JSON 数据文件(如下所示),我正在尝试使用 jq 实用程序查找字段值。
如果键名称中包含 -
短划线字符,则它工作正常,但字段除外。
如何获取“field-2”、“field-three”或“”的值field-three.url" for element under content.book1
(using jq
at least)?
我尝试了以下方法来获取值,但对于键名称中包含破折号 -
的字段,它给我以下错误。我试图反斜杠 -
字符,但这也没有帮助。
找到的错误类型:
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/
我不知道 jq,但是你把 python 放在标签里所以:
$ cat test.json | python -c "import sys, json; print(json.load(sys.stdin)['content']['book1']['field-three']['name'])"
THIRD
或不带管道:
$ python -c "import json; print(json.load(open('test.json'))['content']['book1']['field-three']['name'])"
如 jq manual 中所述,要处理包含 -
等非标识符字符的键,您可以使用双引号。
从 shell 开始,如果您在过滤器周围使用单引号,这是最简单的。例如,尝试以下命令:
cat /tmp/my.data.json | jq '.pages'
cat /tmp/my.data.json | jq '.pages.book1[0]'
cat /tmp/my.data.json | jq '.pages.book1[1]'
cat /tmp/my.data.json | jq '.content'
cat /tmp/my.data.json | jq '.content.book1'
cat /tmp/my.data.json | jq '.content.book1.name'
cat /tmp/my.data.json | jq '.content.book1.field1'
cat /tmp/my.data.json | jq '.content.book1."field-2"'
cat /tmp/my.data.json | jq '.content.book1."field-three"'
cat /tmp/my.data.json | jq '.content.book1."field-three".url'
cat /tmp/my.data.json | jq '.content.book1.authur'
cat /tmp/my.data.json | jq '.content.book1.route'
"-"用于jq中的否定。对于带有“-”等特殊字符的键名,不能使用简化的“.keyname”语法。有几种选择,但最稳健的是简单地使用 .["KEY NAME"]
形式,在链接时可以缩写为 ["KEY NAME"],例如对于 .a | .["b-c"]
,.a["b-c"]
是 shorthand。
如有疑问,请明确使用管道。
更多信息请查阅jq手册and/orhttps://github.com/stedolan/jq/wiki/FAQ
Hm..花了一些时间,但最后似乎我们需要 DOUBLE QUOTE 它并反斜杠只是包含 -
的任何键名的双引号在它的名字里。
$ cat /tmp/my.data.json| jq ".content.book1.\"field-2\""
"value-2"
$ cat /tmp/my.data.json| jq ".content.book1.\"field-three\".\"url\""
"book1/field-three/"
或
如果您将所有内容都用单引号 '
括起来,那么我们不需要反斜杠 "
双引号,而是对名称中带有 -
的键名使用双引号。
$ cat /tmp/my.data.json| jq '.content.book1."field-three"."url"'
"book1/field-three/"
希望对您有所帮助!从 https://jqplay.org/
拿了一些 help/hint查看更多:https://github.com/stedolan/jq/issues/38#issuecomment-9770240