Home > OS >  Decoding string in Java returned from AWS
Decoding string in Java returned from AWS

Time:01-04

I am returning Data from my AWS account using

        GetRoleRequest getRoleRequest = GetRoleRequest.builder().roleName("ec2_role_powered").build();
    GetRoleResponse listRoles = iam.getRole(getRoleRequest);
    Role role = listRoles.role();
    String service = role.assumeRolePolicyDocument();
    System.out.println(service);

It is returning role.assumeRolePolicyDocument(); in the following format:

{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}

While it is supposed to be in JSON format or something as it is like this in AWS CloudShell console:

enter code here

How am I supposed to get a string like shown in the picture or decode the { like characters to their original format?

CodePudding user response:

string "{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}" is url encoding.

String service = role.assumeRolePolicyDocument();
System.out.println(java.net.URLDecoder.decode(service, StandardCharsets.UTF_8));
  •  Tags:  
  • Related