The program reads the file from input json,then appends extra structure within the program. I have everything working but when i printout in my console it has extra n\ r and spaces in the json. In the playground it doesnt but when i run it from shell it does. The problem how can i removed to match the outcome file.
File thats getting inputted:
{
"mac_address": "",
"serial_number": "4UW1234",
"device_type": "STORuhaiul",
"device_model": "N9ZsdfsdA",
"part_number": "N9sdfsdfsdf5A",
"extra_attributes": [
{
"attribute_type": "ENTITLEMENT_ID",
"attribute_value": "ABC123JKJUBCDZ"
}
],
"platform_customer_id": "f850243a4c1911eca07f6db9a",
"application_customer_id": "88e0ff88c07811ed76c7988eb"
}
Outcome file should be like this exactly quoted(result)
{
"specversion": "1.0",
"id": "ce20b92d-b45b7-a544-8fe4d8f7ff06",
"source": "CCS",
"type": "",
"time": "2022-08-09T23:59:08.468903+00:00",
"data": "{\"mac_address\": null, \"serial_number\": \"ADL-DHCI1\", \"device_type\": \"DHCI_STORAGE\", \"device_model\": \"NIMBLE DHCI STORAGE\", \"part_number\": \"60\", \"extra_attributes\": [
{\"attribute_type\": \"ENTITLEMENT_ID\", \"attribute_value\": \"YIZUYYNEYV0LVAU\"}
], \"tag_entities\": [], \"platform_customer_id\": \"a07db1081d5b41eeed59645ee95\", \"application_customer_id\": \"3118643e201cb2c92e8b7aa93178\", \"folder_name\": null}"
}
my code
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/google/uuid"
)
type Event struct {
Specversion string `json:"specversion"`
ID uuid.UUID `json:"id"` /// uuid
Source string `json:"source"`
Type string `json:"type"`
Time time.Time `json:"time"`
Data string `json:"data"` /// this part supposed to come from the file
}
type Data struct {
MacAddress string `json:"mac_address"`
SerialNumber string `json:"serial_number"`
DeviceType string `json:"device_type"`
DeviceModel string `json:"device_model"`
PartNumber string `json:"part_number"`
ExtraAttributes []DeviceAttribute `json:"extra_attributes"`
PlatformCustomerID string `json:"platform_customer_id"`
ApplicationCustomerID string `json:"application_customer_id"`
}
type DeviceAttribute struct {
AttributeType string `json:"attribute_type"`
AttributeValue string `json:"attribute_value"`
}
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)
}
Data, 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",
ID: uuid.New(),
Source: "CSS",
Type: "", // user input -p or -u,
Time: time.Now(),
Data: string(Data),
}
out, _ := json.Marshal(e)
fmt.Println(string(out))
}
func help() {
fmt.Println("help")
}
When im running the program im getting this output from vs code
{"specversion":"1.0","id":"c053519-71ef-4a80-a47b-392ac2434f99","source":"CSS","type":"","time":"2022-09-09T14:01:40.2599627-05:00","data":"{\r\n \"mac_address\": \"\",\r\n \"serial_number\": \"4UW1234\",\r\n \"device_type\": \"STOuhaul\",\r\n \"device_model\": \"N95A\",\r\n \"part_number\": \"N9Z55A\",\r\n \"extra_attributes\": [\r\n {\r\n \"attribute_type\": \"ENTITLEMENT_ID\",\r\n \"attribute_value\": \"ABC123JV9UBCDZ\"\r\n }\r\n ],\r\n \"platform_cust.....
output from shell
{"specversion":"1.0","id":"5f5b1841-d99c-4eef-8867-7c09a23d5bb5","source":"CSS","type":"","time":"2022-09-09T19:34:43.802856511Z","data":" \"mac_address\": \"\",\n \"serial_number\": \"j\",\n \"device_type\": \"STORAGE\",\n \"device_model\": \"VIRTUAL\",\n \"part_number\": \"VIRTUAL\",\n \"extra_attributes\": [\n {\n \"attribute_type\": \"ENTITLEMENT_ID\",\n \"attribute_value\": \"H6MI9JYNJCOSBL1GTjfgjfjY6P\"\n }\n ],\n \"platform_customer_id\": \"b050fa3a49hfghfghfbbf0a2a9c6414efb\",\n \"application_customer_id\": \"03eda3027f4c11ec8cee12749a9c9018\"\n\n"}