0

I'm learning python and JSON library and trying to find out how to extract a specific key/value from this JSON response. I'm using AWS Lambda and the output is not like exactly JSON as it has "\ inside. I'm not sure how I can extract one value from there.

Any ideas to get releaseVersion value here? thanks!

Code Snippet:

import json
import boto3
client = boto3.client('eks')
def lambda_handler(event, context):
        response = client.describe_nodegroup(
            clusterName='eks-cluster',
            nodegroupName='Eks-Node-1'
        )
        return json.dumps(response, default=str)

Output:

    "{\"ResponseMetadata\": {\"RequestId\": \"xxxxxx\", \"HTTPStatusCode\": 200, \"HTTPHeaders\": {\"date\": \"Sun, 20 Jun 2021 03:00:28 GMT\", \"content-type\": \"application/json\", \"content-length\": \"1294\", \"connection\": \"keep-alive\", \"x-amzn-requestid\": \"c1e99860-22df-4b9e-aa6c-ffbc3c7690f7\", \"access-control-allow-origin\": \"*\", \"access-control-allow-headers\": \"*,Authorization,Date,X-Amz-Date,X-Amz-Security-Token,X-Amz-Target,content-type,x-amz-content-sha256,x-amz-user-agent,x-amzn-platform-id,x-amzn-trace-id\",
     \"x-amz-apigw-id\": \"BNBT7F0mCYcFRzg=\", \"access-control-allow-methods\": \"GET,HEAD,PUT,POST,DELETE,OPTIONS\", \"access-control-expose-headers\": \"x-amzn-errortype,x-amzn-errormessage,x-amzn-trace-id,x-amzn-requestid,x-amz-apigw-id,date\", \"x-amzn-trace-id\": \"Root=1-60ceaf4c-5e181ebd4bdd49bd77c982b8\"}, \"RetryAttempts\": 0}, \"nodegroup\": 
{\"nodegroupName\": \"Eks-Node-1\", \"nodegroupArn\": \"arn:aws:eks:us-east-2:xxxxxx:nodegroup/eks-cluster/Eks-Node-1/cebd1393-ca31-cd8f-6d28-9bdd22617dcd\", \"clusterName\": \"eks-cluster\", \"version\": \"1.18\", \"releaseVersion\": \"1.18.9-20210526\", \"createdAt\": \"2021-06-20 02:02:23.899000+00:00\", \"modifiedAt\": \"2021-06-20 02:52:31.397000+00:00\", \"status\": \"ACTIVE\", \"capacityType\": 
    \"SPOT\", \"scalingConfig\": {\"minSize\": 2, \"maxSize\": 2, \"desiredSize\": 2}, \"instanceTypes\": [\"t3.micro\"], \"subnets\": [\"subnet-5b0dec30\", \"subnet-dff27493\", \"subnet-fbca9581\"], \"remoteAccess\": {\"ec2SshKey\": \"ssht\"}, \"amiType\": \"AL2_x86_64\", \"nodeRole\": 
    \"arn:aws:iam::xxxxxxxx:role/EKS-NodeGroups-Role\", \"labels\": {}, \"resources\": {\"autoScalingGroups\": [{\"name\": 
    \
"eks-cebd139xxxxxxdd22617dcd\"}], \"remoteAccessSecurityGroup\": \"sg-0810575351f982840\"}, \"diskSize\": 4, \"health\": {\"issues\": []}, \"tags\": {}}}"
1
  • That's regular JSON, you're just looking at it in its Python string literal form. Try printing it out (print(json.dumps(response))) and it'll look friendlier. Commented Jun 20, 2021 at 3:33

2 Answers 2

1

The response variable should contain the data in a Python dictionary object.

The boto3 documentation for describe_nodegroup() shows the output as:

{
    'nodegroup': {
        'nodegroupName': 'string',
        'nodegroupArn': 'string',
        'clusterName': 'string',
        'version': 'string',
        'releaseVersion': 'string',
        ...
        },
     ...
    }
}

Therefore, you should just be able to use: return response['nodegroup']['releaseVersion']

Sign up to request clarification or add additional context in comments.

Comments

0

I pasted your string in between the parens in a print(...) and got this:

{
  "ResponseMetadata": {
    "RequestId": "xxxxxx",
    "HTTPStatusCode": 200,
    "HTTPHeaders": {
      "date": "Sun, 20 Jun 2021 03:00:28 GMT",
      "content-type": "application/json",
      "content-length": "1294",
      "connection": "keep-alive",
      "x-amzn-requestid": "c1e99860-22df-4b9e-aa6c-ffbc3c7690f7",
      "access-control-allow-origin": "*",
      "access-control-allow-headers": "*,Authorization,Date,X-Amz-Date,X-Amz-Security-Token,X-Amz-Target,content-type,x-amz-content-sha256,x-amz-user-agent,x-amzn-platform-id,x-amzn-trace-id",
      "x-amz-apigw-id": "BNBT7F0mCYcFRzg=",
      "access-control-allow-methods": "GET,HEAD,PUT,POST,DELETE,OPTIONS",
      "access-control-expose-headers": "x-amzn-errortype,x-amzn-errormessage,x-amzn-trace-id,x-amzn-requestid,x-amz-apigw-id,date",
      "x-amzn-trace-id": "Root=1-60ceaf4c-5e181ebd4bdd49bd77c982b8"
    },
    "RetryAttempts": 0
  },
  "nodegroup": {
    "nodegroupName": "Eks-Node-1",
    "nodegroupArn": "arn:aws:eks:us-east-2:xxxxxx:nodegroup/eks-cluster/Eks-Node-1/cebd1393-ca31-cd8f-6d28-9bdd22617dcd",
    "clusterName": "eks-cluster",
    "version": "1.18",
    "releaseVersion": "1.18.9-20210526",
    "createdAt": "2021-06-20 02:02:23.899000+00:00",
    "modifiedAt": "2021-06-20 02:52:31.397000+00:00",
    "status": "ACTIVE",
    "capacityType": "SPOT",
    "scalingConfig": {
      "minSize": 2,
      "maxSize": 2,
      "desiredSize": 2
    },
    "instanceTypes": [
      "t3.micro"
    ],
    "subnets": [
      "subnet-5b0dec30",
      "subnet-dff27493",
      "subnet-fbca9581"
    ],
    "remoteAccess": {
      "ec2SshKey": "ssht"
    },
    "amiType": "AL2_x86_64",
    "nodeRole": "arn:aws:iam::xxxxxxxx:role/EKS-NodeGroups-Role",
    "labels": {},
    "resources": {
      "autoScalingGroups": [
        {
          "name": "eks-cebd139xxxxxxdd22617dcd"
        }
      ],
      "remoteAccessSecurityGroup": "sg-0810575351f982840"
    },
    "diskSize": 4,
    "health": {
      "issues": []
    },
    "tags": {}
  }
}

So what value do you want get out of that structure? The nodegroup would be response['nodegroup'], its name is response['nodegroup']['nodegroupName'], its ARN is response['nodegroup']['nodegroupArn'], etc.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.