如何用jq将Raivo OTP转Aegis格式并构建嵌套info对象
使用jq实现OTP数据格式转换
需求说明
将Raivo格式的OTP条目转换为Aegis兼容的格式,核心完成字段重命名与嵌套对象构造。
输入示例(Raivo格式)
{ "kind": "TOTP", "account": "google@gmail.com", "secret": "GoogleSecret", "iconType": "raivo_repository", "issuer": "Google.com", "timer": "30", "digits": "6", "counter": "0", "algorithm": "SHA1", "iconValue": "google.com/google-gmail.png" }
期望输出(Aegis格式)
{ "type": "totp", "uuid": "b33ca6f5-7661-4289-afcc-ee9847c3e59a", "name": "google@gmail.com", "issuer": "Google.com", "note": "", "icon": null, "info": { "secret": "GoogleSecret", "algo": "SHA1", "digits": 6, "period": 30 } }
jq转换方案
单条目转换命令
如果输入是单个OTP对象,直接使用以下jq表达式:
{ type: (.kind | ascii_downcase), uuid: (uuid | tostring), name: .account, issuer: .issuer, note: "", icon: null, info: { secret: .secret, algo: .algorithm, digits: (.digits | tonumber), period: (.timer | tonumber) } }
多条目数组转换命令
如果输入是多个OTP对象组成的JSON数组,用map()包裹表达式批量处理:
map({ type: (.kind | ascii_downcase), uuid: (uuid | tostring), name: .account, issuer: .issuer, note: "", icon: null, info: { secret: .secret, algo: .algorithm, digits: (.digits | tonumber), period: (.timer | tonumber) } })
兼容TOTP与HOTP的扩展版本
如果需要同时处理TOTP和HOTP类型,可动态适配info字段:
map({ type: (.kind | ascii_downcase), uuid: (uuid | tostring), name: .account, issuer: .issuer, note: "", icon: null, info: { secret: .secret, algo: .algorithm, digits: (.digits | tonumber), (if .kind == "TOTP" then "period" else "counter" end): (if .kind == "TOTP" then .timer else .counter end | tonumber) } })
关键逻辑说明
- 字段映射:通过键值对直接完成重命名,比如
.account映射为name,.kind转小写后作为type - 嵌套对象构造:在
info字段内直接生成新对象,将原字段按需映射,同时把字符串类型的数字(如digits、timer)转为数值类型 - UUID生成:使用jq内置的
uuid函数生成随机UUID,满足Aegis格式要求 - 冗余字段处理:原数据中
iconType、iconValue、counter(TOTP场景)等无关字段直接忽略
使用方法
将输入数据保存为input.json,运行以下命令输出转换结果:
jq -f transform.jq input.json > output.json
(其中transform.jq是保存上述jq表达式的文件,也可直接将表达式写在命令行中)
内容的提问来源于stack exchange,提问作者Spliced Anomaly
相关产品推荐
相关产品推荐

