Im inputing JSON file from shell and converting to specific format i did that part on bottom but im trying to include like metadat like time ,uuid and couple of other thing to it then printout the new json file.My issue i cant put the new info to data part of the data
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
// "github.com/google/uuid"
)
func help() {
fmt.Println("help")
}
type Event struct{
Specversion string `json:"specversion"`
ID string `json:"id"` /// uuid
Source string `json:"source"`
// Type bool `json:"type"`
Time time.Now `json:"time"`
Data string `json:"data"` /// this part supposed to come from the file
}
func main(){
if len(os.Args) < 1 { //checking for provided argument if the first arg satisfy then use read file
fmt.Println("Usage : " os.Args[0] " file name")
help()
os.Exit(1)
}
file, err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Println("Cannot read the file or file does not exists")
os.Exit(1)
}
out, err := json.Marshal(map[string]string{"data": string(file)})
if err != nil {
panic(err)
}
//fmt.Println(string(out))
e := Event{
Specversion: "1.0",
//id: uuid.New(),
Source: "CSS",
//type: true,
Time: time.Now(),
Data: data.(string),
}
fmt.Println(string(out))
fmt.Println(string(e))
//fmt.Println(string(id))
}
The outcome should be this
{
"specversion": "1.0",
"id": "ce20b92d-b45b-hhd7-a544-8fe4d8f7ff06",
"source": "CCS",
"type": "EVENT",
"time": "2022-08-09T23:59:08.468903 00:00",
"data": "{\"mac_address\": null, \".................
original file from input
"mac_address": "",
"serial_number": "j",
"device_type": "STORAGE",
"device_model": "VIRTUAL",
"part_number": "VIRTUAL",
"extra_attributes": [
{
"attribute_type": "ENTITLEMENT_ID",
"attribute_value": "H6MI9JYNJCOSBL1GTjfgjfjY6P"
CodePudding user response:
The application marshals a map with the data, but not the metadata. The application configures a value with the metadata and data, but does nothing with it. Pull those pieces together. Something like this:
file, err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Println("Cannot read the file or file does not exists")
os.Exit(1)
}
e := Event{
Specversion: "1.0",
Source: "CSS",
Time: time.Now(),
Data: string(file),
}
out, _ := json.MarshalIndent(e, "", " ")
fmt.Println(string(out))
Run the program on the playground.
CodePudding user response:
You can try the approach below for this:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
// "github.com/google/uuid"
)
func help() {
fmt.Println("help")
}
type Event struct {
Specversion string `json:"specversion"`
ID string `json:"id"` /// uuid
Source string `json:"source"`
// Type bool `json:"type"`
Time time.Now `json:"time"`
Data map[string]interface{} `json:"data"` /// this part supposed to come from the file
}
func main() {
if len(os.Args) < 1 { //checking for provided argument if the first arg satisfy then use read file
fmt.Println("Usage : " os.Args[0] " file name")
help()
os.Exit(1)
}
file, err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Println("Cannot read the file or file does not exists")
os.Exit(1)
}
res := map[string]interface{}{}
err = json.Unmarshal(file, &res)
if err != nil {
panic(err)
}
//fmt.Println(string(out))
e := Event{
Specversion: "1.0",
//id: uuid.New(),
Source: "CSS",
//type: true,
Time: time.Now(),
Data: res,
}
fmt.Println(string(out))
fmt.Println(string(e))
//fmt.Println(string(id))
}
Defining your Event struct data type as map sting interface helps. Hope this works.