如何查询我的JSON文件中的特定键

gk7wooem  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(100)

我正在处理一个项目,我想使用Jquery查询一个JSon,但是我得到了一个错误:

jq: error: syntax error, unexpected IDENT, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
..id  
jq: 1 compile error
Error: Process completed with exit code 3.

我使用的命令是:

jq '..id' new_file.json

查询名为new_file. json的JSON文件
下面是我的JSON文件:

[
  {
    "type": "users_export",
    "status": "pending",
    "connection_id": "con_kmZIjREZWYzt39JI",
    "format": "json",
    "limit": 5,
    "fields": [
      {
        "name": "user_id"
      },
      {
        "name": "user_id",
        "export_as": "provider"
      },
      {
        "name": "username"
      },
      {
        "name": "username",
        "export_as": "provider"
      },
      {
        "name": "nickname"
      },
      {
        "name": "email"
      },
      {
        "name": "email"
      },
      {
        "name": "identities[0].connection",
        "export_as": "provider"
      },
      {
        "name": "email_verified",
        "export_as": "provider"
      }
    ],
    "connection": "dev-default-evoyanbs",
    "created_at": "2022-11-16T17:45:58.429Z",
    "id": "job_aztDgKXWT8g8iZ5T"
  }
]

我想要的' job_aztDgKXWT8g8iZ5T '作为我的输出,但我得到了上述错误,可以有人请帮助我,谢谢。

kb5ga3dv

kb5ga3dv1#

由于您有一个阵列,因此命令应为:

jq '.[].id' new_file.json

而结果将是:"job_aztDgKXWT8g8iZ5T" .
如果你想去掉输出中的双引号,你可以在命令中添加raw-output标志,如下所示:

jq -r '.[].id' new_file.json

jq --raw-output '.[].id' new_file.json

相关问题