vr1111
vr1111

Reputation: 23

Linux grep command to find the value of key from json

I have a JSON output that contains a list of objects stored in a variable.(I may not be phrasing that right)

Output of a curl command: will post in comment as I am unable to post here

I want to grep the value at this position "ad6743fae9c54748b8644564c691ba58" shown in the output, which changes everytime i run the curl command. I want that to pass as a input to other curl command.

Please help

Upvotes: 0

Views: 718

Answers (3)

Tanaike
Tanaike

Reputation: 201398

"jq" https://stedolan.github.io/jq/download/ is necessary for this. If for "ad6743fae9c54748b8644564c691ba58", itself is changed every time, how about following script?

of="data.json" && curl ..... -o $of > $of && key=$(cat $of | jq -r '.destination[0]|keys' | jq -r '.[]') && jq -r ".destination[0].$key" $of && unset key of
  • Data got by curl is output as name of "$of".

  • Position of "ad6743fae9c54748b8644564c691ba58" is retrieved as "$key".

  • Value of "ad6743fae9c54748b8644564c691ba58" is output using "$key".

For example, in this script, when the position of "ad6743fae9c54748b8644564c691ba58" is not changed, "ad6743fae9c54748b8644564c691ba58" can be changed to "abcdefg1234567".

Upvotes: 1

Knight-Zhou
Knight-Zhou

Reputation: 49

like this?:

#!/usr/bin/env python
#coding:utf-8
import sys
import json
aa = '''{
    "destination": [
        {
            "ad6743fae9c54748b8644564c691ba58": {
                "throttle_bytes_per_second": "0",
                "delete_this": false,
                "path": "s3testbucket",
                "server_side_encryption": false,
                "provider": "s3",
                "access_key": "XXXXXXXXXXX",
                "access_secret": "XXXXXXXXXXXXXXXXXXXXX"
            }
        }
    ],
    "request_id": "d6e089bb-9729-423b-8319-d441e0a72202"
}'''

# aa=sys.argv[1]

bb = json.loads(aa)
print bb["request_id"]

Upvotes: 0

Robin Thoni
Robin Thoni

Reputation: 1721

Use jq: curl 'http://.../some.json' | jq .destination[0].ad6743fae9c54748b8644564c691ba58

Upvotes: 0

Related Questions