Home > other >  How to query my JSON file for specific key
How to query my JSON file for specific key

Time:11-18

I am working on a project where I want to query a JSon using Jquery but I am getting an error:

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.

I am using the command:

jq '..id' new_file.json

To query my JSON file which is named new_file.json

Here is my JSON file:

[
  {
    "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"
  }
]

I want the 'job_aztDgKXWT8g8iZ5T' as my output but I am getting the above mentioned error, can someone please help me out, thanks.

CodePudding user response:

As you have an array, the command should be:

jq '.[].id' new_file.json

And the result will be: "job_aztDgKXWT8g8iZ5T".

If you want to get rid of double quotes in your output, you can add raw-output flag in your command, like this:

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

or

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