-3

In the below mentioned code i need to only get the value of access token and write it to another file.

{
  "access_token": "dddddddddddddd",
  "expires_in": 3600,
  "scope": "https://www.googleapis.com/auth/drive.metadata 
  "token_type": "Bearer"
}
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Shilpa
  • 1
  • 2
  • 1
    "below-mentioned code" that's not bash code. – krisz Aug 12 '19 at 03:12
  • 1
    And that's not an array. – krisz Aug 12 '19 at 03:22
  • Do you want to *parse a string* with the above-mentioned contents? – krisz Aug 12 '19 at 03:38
  • i need the value of only access token and write that content to another file using shellscipt – Shilpa Aug 12 '19 at 03:43
  • Hi Shilpa, welcome to stackoverflow. Its important to be detailed in your questions here so that people can help you easily and you need to show what you tried and where you stumbled. – Mihir Luthra Aug 12 '19 at 04:00
  • You look to have json posted in your question. Do not manipulate json with bash. Instead use a tool that can validate json, like [jq](https://stedolan.github.io/jq/) – David C. Rankin Aug 12 '19 at 04:16
  • Perfect example, you are missing the `",` following `"metadata`. With valid json, (stored in `file`) you only need `jq .access_token file` to retrieve `"dddddddddddddd"`. – David C. Rankin Aug 12 '19 at 04:32

1 Answers1

1
s='{"access_token": "dddddddddddddd", "expires_in": 3600, "scope": "https://www.googleapis.com/auth/drive.metadata "token_type": "Bearer"}'
echo ${s:18:14} # prints dddddddddddddd

Or use regular expressions.

krisz
  • 2,686
  • 2
  • 11
  • 18