gpt4 book ai didi

json - 在 shell 脚本中解码 JSON 和 base64 编码值

转载 作者:行者123 更新时间:2023-12-02 10:57:51 30 4
gpt4 key购买 nike

我有一个 JSON,我需要通过特定 key 提取一个 Base64 编码的值并对其进行解码。

JSON 具有以下结构:

[
{
"LockIndex": 0,
"Key": "Arul/key1",
"Flags": 0,
"Value": "MzAKCg==",
"CreateIndex": 369,
"ModifyIndex": 554
}
]

在上面的 JSON 中,我只需要提取 "Value":"MzAKCg==" 并解码 Base64 编码的 "MzAKCg==" 值。我想使用 shell 脚本来执行此操作。

请帮忙。

最佳答案

jq最近增加了对base64编码和解码的支持

https://stedolan.github.io/jq/manual/#Formatstringsandescaping

@base64:

The input is converted to base64 as specified by RFC 4648.

@base64d:

The inverse of @base64, input is decoded as specified by RFC 4648. Note: If the decoded string is not UTF-8, the results are undefined.

对于您的数据,命令是

jq -r 'map(.Value | @base64d)' < file.json
<小时/>

https://github.com/stedolan/jq/issues/47

尚未发布,但您可以安装最新的开发版本来使用它。

brew reinstall --HEAD jq

一旦 jq 的下一个版本发布,您就可以切换回最新的稳定版本。

关于json - 在 shell 脚本中解码 JSON 和 base64 编码值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40910296/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com