Skip to content
Advertisement

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

Advertisement

Answer

“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”.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement