#Mutating JSON Struct

3 messages · Page 1 of 1 (latest)

humble ruin
#

I have this struct:

type info struct {
    InfoType  string `json:"Info_type"`
    InfoValue string `json:"Info_value"`
}

type Response struct {
    infos []info `json:"info"`
}

// Grabs data from SQL then scans into struct 
var dp Response
var infoJSON []byte
json.Unmarshal(infoJSON, &dp.infos)

// continues

This output looks like this:

[
{
    "Info_type": "example1",
    "Info_value": "Example11"
},
{
    "Info_type": "example2",
    "Info_value": "Example22"
},
{
    "Info_type": "example3",
    "Info_value": "Example33"
},
{
    "Info_type": "example4",
    "Info_value": "Example44"
}
]

However, i'd like to mutate this and serve it as:

[
{"Example1": "Example11"},
{"Example2": "Example22"},
{"Example3": "Example33"}
]

How can i go about this?

runic estuary
#

I would not recommend it as what you're doing isn't valid JSON since your keys are unpredictable and are sequential