-1

我有一个 JSON 数据文件(如下所示),我正在尝试使用jq实用程序查找字段值。

-如果键名中包含短划线字符,则它工作正常,但字段除外。

如何获取(至少使用)下元素的“ field-2 ”、“ field-three ”或“ field-three.url ”的值?content.book1jq

我尝试了以下方法来获取值,但是对于键名中包含破折号的字段,它给了我以下错误-。我试图反斜杠-字符,但这也无济于事。

发现的错误类型:

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/

4

4 回答 4

6

"-" 用于 jq 中的否定。对于带有“-”等特殊字符的键名,不能使用简化的“.keyname”语法。有几种替代方法,但最可靠的方法是简单地使用 形式.["KEY NAME"],在链接时可以缩写为 ["KEY NAME"],例如.a["b-c"]是 . 的简写.a | .["b-c"]

如果有疑问,请明确使用管道。

如需更多信息,请查阅 jq 手册和/或https://github.com/stedolan/jq/wiki/FAQ

于 2017-08-16T00:40:45.007 回答
2

jq 手册中所述,要处理具有非标识符字符的键,例如-可以使用双引号。

如果您在过滤器周围使用单引号,那么从 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'
于 2017-08-16T00:39:45.657 回答
0

我不知道 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'])"
于 2017-08-16T00:20:47.160 回答
0

嗯..花了一些时间,但最后似乎我们需要引号并反斜杠只是任何包含 a-的键名的双引号。

$ 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/获得了一些帮助/提示

查看更多信息:https ://github.com/stedolan/jq/issues/38#issuecomment-9770240

于 2017-08-16T00:47:09.473 回答