将对象添加到平面输入的输出中

huangapple go评论70阅读模式
英文:

Adding an object to output w/ flat input

问题

我试图将一个应用程序中的数据转换成另一个应用程序接受的格式。实际上,我正试图做与这篇博文完全相同的事情,但是使用 jq 而不是他们的程序。https://www.tygertec.com/aegis-raivo-otp-translator/

示例输入数据:

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

我希望输出看起来像这样:

{
"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
}
}
大部分都只是重新命名字段,比如从 "kind" 到 "type"(我想弄清楚如何将其大写,但这不是我的问题),但我在构建 "info" 对象方面遇到了很大的困难。

我找到了一些类似的问题,其中使用了 map/reduce,并尝试将它们改为我所需的形式,但我不想为每个键都创建一个对象,而且我对如何构建一个既包括了我只需要进行简单“重命名”(例如:type: .kind)的键又包括了新的(嵌套的)"info" 对象感到困惑。

感谢您的帮助。

英文:

I'm trying to get data from one app into a format that another app will accept. I'm actualy trying to do exactly what this blog post is about but with jq instead of their program. https://www.tygertec.com/aegis-raivo-otp-translator/

Sample input data:

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

I'd like the output to look like this:

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

Most of this is just relabelling the fields, like from "kind" to "type" (I would like to figure out how to capitalize, but that is not my question), but I'm having a hell of a time figuring out how to make the "info" object.

I found some similar questions where map/reduce are used and tried reworking those into what I need, but I don't want to make an object for every key, and I'm generally confused about how to construct an object combining a) the keys I just need to do a simple "rename" (ex: type: .kind) on and a) the new (nested) "info" object.

Thank you for any help.

答案1

得分: 2

If you just want to translate certain keys to certain other keys (with slight modifications sometimes), just create a new object, and assign the corresponding values (I didn't get where uuid is supposed to come from):

{
  "type": .kind | ascii_downcase,
  "uuid": "???",
  "name": .account,
  "issuer": null,
  "note": "",
  "icon": null,
  "info": {
    "secret": null,
    "algo": .algorithm,
    "digits": .digits | tonumber,
    "period": .timer | tonumber
  }
}

Demo

If you want to pursue the "middle man" approach from the linked article, also show the intermediate structure you want to convert to/from.

英文:

If you just want to translate certain keys to certain other keys (with slight modifications sometimes), just create a new object, and assign the corresponding values (I didn't get where uuid is supposed to come from):

{
  type: .kind | ascii_downcase,
  uuid: "???",
  name: .account,
  issuer,
  note: "",
  icon: null,
  info: {
    secret,
    algo: .algorithm,
    digits: .digits | tonumber,
    period: .timer | tonumber
  }
}

Demo

If you wanto pursue the "middle man" approach from the linked article, also show the intermediate srtructure you want to convert to/from.

huangapple
  • 本文由 发表于 2023年6月15日 13:30:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定