How do I get a key-value pair out of a JSON string using jq

Viewed 182

This is what I have:

{
    "ARN": "sdfsdfsdfsdfds",
    "Name": "sdfsdfsdfsdsf",
    "VersionId": "sdfsdfsdfsdfsdf",
    "SecretString": "{\"username\":\"myusername\",\"password\":\"mypassword\"}",
    "VersionStages": [
        "AWSCURRENT"
    ],
    "CreatedDate": 1541641750.607
}

How do I get mykey and myvalue out of this JSON?

jq '.SecretString' -r gives me this:

{"username":"myusername","password":"mypassword"}

But how do I get myusername and mypassword out of that?

Ok, this seems to work, but should I be piping it back into jq like this? Feels like im doing it wrong:

jq '.SecretString' -r | jq '.username' -r
jq '.SecretString' -r | jq '.password' -r
1 Answers

.SecretString is not so secret, but it is the JSON string representation of a JSON object. To parse it, simply use fromjson, so you could, for example, obtain the username with:

.SecretString | fromjson | .username
Related