将 json 值保存为变量

将 json 值保存为变量

我从命令中得到以下输出。

{
  "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"
  }
}

有人可以帮我提取价值访问 uri根据上面的例子,输出应该是

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

答案1

我会一起去jq。在您的示例中,它将是(输入当然来自您的命令):

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

答案2

使用sed

$ ... | sed -En ':a;/"data":/,/}/{n;/access-uri/s/[^:]*[^"]*"([^"]*).*/\1/p;ba}'
/p/u4yRbnS_Yv29ivICXNWz-76BAgBqfln0SthBVYLZ3AdPOs9BKTQEH48MZEJNvXaT/n/bmjx6wj24zrv/b/season5/o/abcd.zip

相关内容