JSON – How to Save a JSON Value as a Variable Using jq

jqjson

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

Best Answer

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"?')
Related Question