Ace
Ace

Reputation: 493

How to extract the string based on the value from another field?

Please consider the following JSON in packer_manifest.json:

{
  "builds": [
    {
      "name": "amazon-ebs",
      "builder_type": "amazon-ebs",
      "build_time": 1490743286,
      "files": null,
      "artifact_id": "us-east-1:ami-ae4af3b8",
      "packer_run_uuid": "76afaf47-2ac2-8b50-da43-ea5bff541ae6"
    },
    {
      "name": "amazon-ebs",
      "builder_type": "amazon-ebs",
      "build_time": 1490744203,
      "files": null,
      "artifact_id": "us-east-1:ami-994ff68f",
      "packer_run_uuid": "d5655333-4240-bbf5-8b08-94be7606536d"
    },
    {
      "name": "amazon-ebs",
      "builder_type": "amazon-ebs",
      "build_time": 1490744956,
      "files": null,
      "artifact_id": "us-east-1:ami-e241f8f4",
      "packer_run_uuid": "5f7efb09-4b45-9239-ffe6-903a0aed26e4"
    },
    {
      "name": "amazon-ebs",
      "builder_type": "amazon-ebs",
      "build_time": 1490745669,
      "files": null,
      "artifact_id": "us-east-1:ami-4046ff56",
      "packer_run_uuid": "69fd6d30-fdfd-6778-54ac-338abbea2d88"
    },
    {
      "name": "amazon-ebs",
      "builder_type": "amazon-ebs",
      "build_time": 1490746077,
      "files": null,
      "artifact_id": "us-east-1:ami-dc3b82ca",
      "packer_run_uuid": "4c74241d-f7e2-bdad-6fa7-3fb84e56c9b3"
    },
    {
      "name": "amazon-ebs",
      "builder_type": "amazon-ebs",
      "build_time": 1490806537,
      "files": null,
      "artifact_id": "us-east-1:ami-2c4cf43a",
      "packer_run_uuid": "afcbe826-1a0a-7042-35cb-3e3c2569b48b"
    },
    {
      "name": "amazon-ebs",
      "builder_type": "amazon-ebs",
      "build_time": 1491921968,
      "files": null,
      "artifact_id": "us-east-1:ami-69a8207f",
      "packer_run_uuid": "57ad32f1-6291-7a60-5ee9-3bf46aacd288"
    },
    {
      "name": "amazon-ebs",
      "builder_type": "amazon-ebs",
      "build_time": 1491940019,
      "files": null,
      "artifact_id": "us-east-1:ami-035ad215",
      "packer_run_uuid": "a0124439-8002-9d13-59bf-43fdcef6eb5e"
    },
    {
      "name": "amazon-ebs",
      "builder_type": "amazon-ebs",
      "build_time": 1491940543,
      "files": null,
      "artifact_id": "us-east-1:ami-7e5fd768",
      "packer_run_uuid": "42a0f104-460f-789a-6a86-37ea6b9fbf93"
    },
    {
      "name": "amazon-ebs",
      "builder_type": "amazon-ebs",
      "build_time": 1491941479,
      "files": null,
      "artifact_id": "us-east-1:ami-0252da14",
      "packer_run_uuid": "13fd9737-e41f-ce82-b991-b363fae971b5"
    },
    {
      "name": "amazon-ebs",
      "builder_type": "amazon-ebs",
      "build_time": 1492026265,
      "files": null,
      "artifact_id": "us-east-1:ami-a3159eb2",
      "packer_run_uuid": "83d55673-620e-05ee-2983-24cab0a009d5"
    }
  ],
  "last_run_uuid": "83d55673-620e-05ee-2983-24cab0a009d5"
}

I am trying to extract the string us-east-1:ami-a3159eb2 only using jq.

I would imagine I need to execute something like this:

jq -r '. | .builds[].artifact_id(where "packer_run_uuid" == .last_run_uuid)'

Upvotes: 3

Views: 338

Answers (2)

peak
peak

Reputation: 116640

  1. The initial .| in your answer is not needed.

  2. Since the "last_run_uuid" key is at the top-level, it is worthwhile extracting its value first, as in the following filter:

.["last_run_uuid"] as $last 
| .builds[]
| select( $last == .["packer_run_uuid"] )
| .artifact_id
  1. Since in your case the key names are sufficiently ordinary, you can also write:

 .last_run_uuid as $last | .builds[] | select( $last == .packer_run_uuid) | .artifact_id
  1. If the schema of a large JSON document is uncertain, it may help to determine the "implicit schema" so that one can be confident in the correctness of a query. Using the jq-defined schema inference engine available at schema.jq (see also Issue 748), the input given by the OP results in the following JSON schema:

{
  "builds": [
    {
      "name": "string",
      "builder_type": "string",
      "build_time": "number",
      "files": "null",
      "artifact_id": "string",
      "packer_run_uuid": "string"
    }
  ],
  "last_run_uuid": "string"
}

Upvotes: 1

Bertrand Martel
Bertrand Martel

Reputation: 45352

Use select to filter a specific document :

jq -r '.last_run_uuid as $uuid | .builds[] | select(.packer_run_uuid == $uuid) | .artifact_id' data.json

Upvotes: 3

Related Questions