0

I am getting the below output from a command.

{
  "data": {
    "access-type": "ObjectRead",
    "access-uri": "/p/u4yRbnS_Yv29ivICXNWz-76BAgBqfln0SthBVYLZ3AdPOs9BKTQEH48MZEJNvXaT/n/bmjx6wj24zrv/b/season5/o/abcd.zip",
    "bucket-listing-action": null,
    "id": "tePBaSkrsUEBY+rKK0HiwraPn76TLD86BOsqm7dr3cqjNXp6026BouTf9kQoKzZk:abcd.zip",
    "name": "abcd.zip",
    "object-name": "abcd.zip",
    "time-created": "2022-10-27T02:20:17.430000+00:00",
    "time-expires": "2023-02-01T00:00:00+00:00"
  }
}

Can someone help me to extract the value of access-uri According the above example the output should be

/p/u4yRbnS_Yv29ivICXNWz-76BAgBqfln0SthBVYLZ3AdPOs9BKTQEH48MZEJNvXaT/n/bmjx6wj24zrv/b/season5/o/abcd.zip

2 Answers 2

8

I'd go with jq. In your example it would be (the Input comes from your command of course):

myvar=$(commandX | jq -r '.data."access-uri"?')
0
-1

Using sed

$ ... | sed -En ':a;/"data":/,/}/{n;/access-uri/s/[^:]*[^"]*"([^"]*).*/\1/p;ba}'
/p/u4yRbnS_Yv29ivICXNWz-76BAgBqfln0SthBVYLZ3AdPOs9BKTQEH48MZEJNvXaT/n/bmjx6wj24zrv/b/season5/o/abcd.zip
2
  • This would also match a key not-access-uri Commented Oct 28, 2022 at 11:31
  • It fails if the data is in any shape other than what is presented. For example, running the JSON through jq -c provides a perfectly legal one-line representation. Commented Oct 28, 2022 at 11:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .