Go: Nested Struct Marshall & UnMarshall

{
"sampleInput": {
"input": {
"encryptedMessage": "F KFRTZX JCUQTWJW TSHJ XFNI, YMFY YMJ JCYWFTWINSFWD NX NS BMFY BJ IT, STY BMT BJ FWJ. LT JCUQTWJ!",
"key": 5
}
}
}
type input struct {
EncryptedMessage string `json:"encryptedMessage"`
Key int `json:"key"`
}
type sampleInput struct {
Input input `json:"input"`
}
type sample struct {
SampleInput sampleInput `json:"sampleInput"`
}
file, _ := ioutil.ReadFile("input-nested.json")var data sample
err := json.Unmarshal([]byte(file), &data)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(data)
{{{F KFRTZX JCUQTWJW TSHJ XFNI, YMFY YMJ JCYWFTWINSFWD NX NS BMFY BJ IT, STY BMT BJ FWJ. LT JCUQTWJ! 5}}}
func caserCipherDecrypt(str string, key int) string {
len := len(str)
msg := ""
str = strings.ToUpper(str)
for i := 0; i < len; i++ {
k := int(str[i])
if k < 65 {
msg = msg + string(k)
} else {
j := (int(str[i]) - key)
if j < 65 {
j = j + 26
}
msg = msg + string(j)
}
}
return msg
}
msg := caserCipherDecrypt(data.SampleInput.Input.EncryptedMessage, data.SampleInput.Input.Key)
type output struct {
Message string `json:"message"`
}
type sampleOutput struct {
Output output `json:"output"`
}
type response struct {
SampleOutput sampleOutput `json:"sampleOnput"`
var resp = response{
SampleOutput: sampleOutput{
Output: output{
Message: msg,
},
},
}
outputJSON, _ := json.Marshal(resp)
{"sampleOnput":{"output":{"message":"A FAMOUS EXPLORER ONCE SAID, THAT THE EXTRAORDINARY IS IN WHAT WE DO, NOT WHO WE ARE. GO EXPLORE!"}}}

#redhatter #opensource #developer #kubernetes #keycloak #golang #openshift #quarkus #spring https://mentorcruise.com/mentor/abhishekkoserwal/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Classic Mistakes New Software Developers Make

Four Ways to Leave a Bad Code Review

GSoC Community Bonding & 1st Coding week with CHAOSS

APY.Finance Weekly Update (6/13–6/19)

Google Summer of Code Final Report!

How to Become a Great Software Engineer

two figures standing on a peak with their arms raised as the sun rises

How to root Walton Primo gm mini

Root android phone

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek koserwal

Abhishek koserwal

#redhatter #opensource #developer #kubernetes #keycloak #golang #openshift #quarkus #spring https://mentorcruise.com/mentor/abhishekkoserwal/

More from Medium

Compare and Match Faces in Go

Simple IoT Messages Delivery With GoLang — 2

Securing gRPC connection with SSL/TLS Certificate using Go

Concurrency in Go — #23