8

I have a JSON file :

{
    "request_id": "9a081c0c-9401-7eca-f55d-50e3b7c0301c",
    "lease_id": "",
    "renewable": false,
    "lease_duration": 2764800,
    "data": {
        "password": "test123",
        "username": "testuser1"
    },
    "wrap_info": null,
    "warnings": null,
    "auth": null
}

I am trying to read the values of username and password. Now I was able to integrate bash and python to get what I wanted.

# curl --silent -k https://1.2.3.4:8200/v1/secret/service/clustered -H "X-Vault-Token: c2e3b6ec17df" | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['password'])"
test123

# curl --silent -k https://1.2.3.4:8200/v1/secret/service/clustered -H "X-Vault-Token: c2e3b6ec17df" | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['username'])"
testuser1

But since I only want to use bash, I have done the following too:

# curl --silent -k https://1.2.3.4:8200/v1/secret/service/clustered -H "X-Vault-Token: c2e3b6ec17df" | sed -n -e 's/^.*password":"//p' | cut -d'"' -f1
test123

# curl --silent -k https://1.2.3.4:8200/v1/secret/service/clustered -H "X-Vault-Token: c2e3b6ec17df" | sed -n -e 's/^.*username":"//p' | cut -d'"' -f1
testuser

I am just concerned whether I have made use of sed and cut commands correctly in this case. Or is there a better way to extract the required fields?

Inian
  • 80,270
  • 14
  • 142
  • 161
  • 2
    Why would you want to use `sed`, use `jq` a son syntax aware parser. – Inian May 17 '17 at 18:36
  • Sed and `cut` are not "only Bash", they're external dependencies. If you really want a parser that's written in shell, you can have a look at [JSON.sh](https://github.com/dominictarr/JSON.sh). – Benjamin W. May 17 '17 at 20:40

3 Answers3

17

I would recommend using jq:

$ jq '.data.password' data.json
"test123"

Or both fields:

$ jq '.data.password, .data.username' data.json
"test123"
"testuser1"
chepner
  • 497,756
  • 71
  • 530
  • 681
grundic
  • 4,641
  • 3
  • 31
  • 47
6

I would recommend jq a lightweight JSON aware parser for manipulating JSON content. Pipe your curl command input to a filter in jq as

curl-command | jq --raw-output '.data.password, .data.username'

Instructions to download-and-install jq available.

chepner
  • 497,756
  • 71
  • 530
  • 681
Inian
  • 80,270
  • 14
  • 142
  • 161
0

You can use bashJson

Its a wrapper for the Python's JSON module and can handle complex JSON data.

See my answer here for example usage : https://stackoverflow.com/a/51821898/2734348

Anand Singh
  • 1,091
  • 13
  • 21