0

I have a JSON output like the one below and need to find the value of appId of the exact match of "name": "exampleAppName01" using JQ. I could narrow it down to the exact name, but I have no idea how to extract other values that matched with it.

The JSON output consists a lot of blocks like this. Simply I need to find the appId if the name matches what we provide.

{
  "apps": [
    {
      "appId": "exampleAppID01",
      "appArn": "arn:aws:amplify:eu-west-1:xxxxxxxxxxxxx:apps/exampleAppID01",
      "name": "exampleAppName01",
      "tags": {
        "pci_scope": "none",
        "project": "exampleRepoName01",
        "environment": "dev-default",
        "workspace": "default",
        "terraform": "true"
      },
      "description": "Amplify App for the exapmle project.",
      "platform": "WEB_DYNAMIC",
      "createTime": "2022-08-09T12:26:10.472000+05:30",
      "updateTime": "2022-08-16T13:18:31.658000+05:30",
      "iamServiceRoleArn": "arn:aws:iam::xxxxxxxxxxxxx:role/exampleAppName01-role",
      "environmentVariables": {
        "APP_DEBUG": "true",
        "_LIVE_UPDATES": "[{\"pkg\":\"@aws-amplify/cli\",\"type\":\"npm\",\"version\":\"latest\"},{\"pkg\":\"next-version\",\"type\":\"internal\",\"version\":\"latest\"},{\"pkg\":\"yarn\",\"type\":\"npm\",\"version\":\"latest\"},{\"pkg\":\"node\",\"type\":\"nvm\",\"version\":\"16.13.0\"}]"
      },
      "defaultDomain": "exampleAppID01.amplifyapp.com",
      "enableBranchAutoBuild": true,
      "enableBranchAutoDeletion": true,
      "enableBasicAuth": false,
      "customRules": [
        {
          "source": "/<*>",
          "target": "https://xxxxxxxxxxxx.cloudfront.net/<*>",
          "status": "200"
        },
        {
          "source": "/<*>",
          "target": "https://xxxxxxxxxxxx.cloudfront.net/<*>",
          "status": "200"
        },
        {
          "source": "/<*>",
          "target": "https://xxxxxxxxxxxx.cloudfront.net/<*>",
          "status": "200"
        }
      ],
      "buildSpec": "version: 1\nbackend:\n  phases:\n    build:\n      commands:\n        - '# Execute Amplify CLI with the helper script'\n        - amplifyPush --simple\nfrontend:\n  phases:\n    preBuild:\n      commands:\n        - yarn install\n    build:\n      commands:\n        - yarn run lint\n        - yarn run build\n  artifacts:\n    baseDirectory: .next\n    files:\n      - '**/*'\n  cache:\n    paths:\n      - node_modules/**/*\ntest:\n  artifacts:\n    baseDirectory: cypress\n    configFilePath: '**/mochawesome.json'\n    files:\n      - '**/*.png'\n      - '**/*.mp4'\n  phases:\n    preTest:\n      commands:\n        - echo \"test\"\n#test:\n#  artifacts:\n#    baseDirectory: cypress\n#    configFilePath: '**/mochawesome.json'\n#    files:\n#      - '**/*.png'\n#      - '**/*.mp4'\n#  phases:\n#    preTest:\n#      commands:\n#        - npm install\n#        - npm install start-server-and-test cypress mocha mochawesome mochawesome-merge mochawesome-report-generator --save-dev\n#    test:\n#      commands:\n#        # We need to set the NEXTAUTH_URL to localhost for the cypress tests but leave them as they were coming from the set environment variables for the deployed app\n#        - NEXTAUTH_URL=http://localhost:3000\n#        - npm run build\n#        - npx start-test 'yarn start' 3000 'npx cypress run --reporter mochawesome --reporter-options \"reportDir=cypress/report/mochawesome-report,overwrite=false,html=false,json=true,timestamp=mmddyyyy_HHMMss\"'\n#    postTest:\n#      commands:\n#        - npx mochawesome-merge cypress/report/mochawesome-report/mochawesome*.json > cypress/report/mochawesome.json\n",
      "customHeaders": "",
      "enableAutoBranchCreation": true,
      "autoBranchCreationPatterns": [
        "example-*"
      ],
      "autoBranchCreationConfig": {
        "stage": "NONE",
        "enableAutoBuild": true,
        "environmentVariables": {},
        "enableBasicAuth": false,
        "enablePullRequestPreview": false
      },
      "repositoryCloneMethod": "SSH"
    },
    {
      "appId": "exampleAppID02",
      "appArn": "arn:aws:amplify:eu-west-1:xxxxxxxxxxxxx:apps/exampleAppID02",
      "name": "exampleAppName02",
      "tags": {
        "pci_scope": "none",
        "project": "exampleRepoName02",
        "environment": "dev-default",
        "workspace": "default",
        "terraform": "true"
      },
      "description": "Amplify App for the exapmle project.",
      "platform": "WEB_DYNAMIC",
      "createTime": "2022-08-09T12:26:10.472000+05:30",
      "updateTime": "2022-08-16T13:18:31.658000+05:30",
      "iamServiceRoleArn": "arn:aws:iam::xxxxxxxxxxxxx:role/exampleAppName02-role",
      "environmentVariables": {
        "APP_DEBUG": "true",
        "_LIVE_UPDATES": "[{\"pkg\":\"@aws-amplify/cli\",\"type\":\"npm\",\"version\":\"latest\"},{\"pkg\":\"next-version\",\"type\":\"internal\",\"version\":\"latest\"},{\"pkg\":\"yarn\",\"type\":\"npm\",\"version\":\"latest\"},{\"pkg\":\"node\",\"type\":\"nvm\",\"version\":\"16.13.0\"}]"
      },
      "defaultDomain": "exampleAppID02.amplifyapp.com",
      "enableBranchAutoBuild": true,
      "enableBranchAutoDeletion": true,
      "enableBasicAuth": false,
      "customRules": [
        {
          "source": "/<*>",
          "target": "https://xxxxxxxxxxxx.cloudfront.net/<*>",
          "status": "200"
        },
        {
          "source": "/<*>",
          "target": "https://xxxxxxxxxxxx.cloudfront.net/<*>",
          "status": "200"
        },
        {
          "source": "/<*>",
          "target": "https://xxxxxxxxxxxx.cloudfront.net/<*>",
          "status": "200"
        }
      ],
      "buildSpec": "version: 1\nbackend:\n  phases:\n    build:\n      commands:\n        - '# Execute Amplify CLI with the helper script'\n        - amplifyPush --simple\nfrontend:\n  phases:\n    preBuild:\n      commands:\n        - yarn install\n    build:\n      commands:\n        - yarn run lint\n        - yarn run build\n  artifacts:\n    baseDirectory: .next\n    files:\n      - '**/*'\n  cache:\n    paths:\n      - node_modules/**/*\ntest:\n  artifacts:\n    baseDirectory: cypress\n    configFilePath: '**/mochawesome.json'\n    files:\n      - '**/*.png'\n      - '**/*.mp4'\n  phases:\n    preTest:\n      commands:\n        - echo \"test\"\n#test:\n#  artifacts:\n#    baseDirectory: cypress\n#    configFilePath: '**/mochawesome.json'\n#    files:\n#      - '**/*.png'\n#      - '**/*.mp4'\n#  phases:\n#    preTest:\n#      commands:\n#        - npm install\n#        - npm install start-server-and-test cypress mocha mochawesome mochawesome-merge mochawesome-report-generator --save-dev\n#    test:\n#      commands:\n#        # We need to set the NEXTAUTH_URL to localhost for the cypress tests but leave them as they were coming from the set environment variables for the deployed app\n#        - NEXTAUTH_URL=http://localhost:3000\n#        - npm run build\n#        - npx start-test 'yarn start' 3000 'npx cypress run --reporter mochawesome --reporter-options \"reportDir=cypress/report/mochawesome-report,overwrite=false,html=false,json=true,timestamp=mmddyyyy_HHMMss\"'\n#    postTest:\n#      commands:\n#        - npx mochawesome-merge cypress/report/mochawesome-report/mochawesome*.json > cypress/report/mochawesome.json\n",
      "customHeaders": "",
      "enableAutoBranchCreation": true,
      "autoBranchCreationPatterns": [
        "example-*"
      ],
      "autoBranchCreationConfig": {
        "stage": "NONE",
        "enableAutoBuild": true,
        "environmentVariables": {},
        "enableBasicAuth": false,
        "enablePullRequestPreview": false
      },
      "repositoryCloneMethod": "SSH"
    }
  ]
}
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
anXler
  • 175
  • 1
  • 1
  • 12
  • Nope, it gave me errors. – anXler Aug 22 '22 at 08:12
  • 1
    Just `jq -r '.apps[] | select(.name == "exampleAppName01")? | .appId'`, is what you need. All that differs from the duplicate are the field names – Inian Aug 22 '22 at 08:13
  • Thanks, @Inian, for this. I am new to this and used it without "?" in the end. What is the use of it? – anXler Aug 22 '22 at 08:19
  • @anXler The [Error Suppression Operator](https://stedolan.github.io/jq/manual/#ErrorSuppression%2FOptionalOperator%3A%3F) `?` is shorthand for a [`try` expression](https://stedolan.github.io/jq/manual/#try-catch) using the [`empty` expression](https://stedolan.github.io/jq/manual/#empty) as its exception handler. – pmf Aug 22 '22 at 15:35

0 Answers0