From 1433748eeb9aa4e33b70885c13daf7131b867c1c Mon Sep 17 00:00:00 2001 From: Bart Walczak Date: Wed, 27 Feb 2019 03:14:38 -0800 Subject: [PATCH 1/3] properly parse json objects if they are present in the csv file --- main.go | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 7e96fcc..d6368fd 100644 --- a/main.go +++ b/main.go @@ -51,15 +51,41 @@ func ReadCSV(path *string) ([]byte, string) { for i, d := range content { buffer.WriteString("{") for j, y := range d { + // \" -> ' + y = strings.Replace(y, `\"`, "'", -1) + // """""" -> '' + y = strings.Replace(y, `""""""`, "''", -1) + // """" -> '' + y = strings.Replace(y, `""""`, "''", -1) + // """ -> " + y = strings.Replace(y, `"""`, `"`, -1) + // "" -> empty + y = strings.Replace(y, `""`, "", -1) + // '' -> "" + y = strings.Replace(y, "''", `""`, -1) + // trim external quotes + y = strings.Trim(y, `"`) buffer.WriteString(`"` + headersArr[j] + `":`) - _, fErr := strconv.ParseFloat(y, 32) - _, bErr := strconv.ParseBool(y) - if fErr == nil { - buffer.WriteString(y) - } else if bErr == nil { - buffer.WriteString(strings.ToLower(y)) + if len(y) == 0 { + buffer.WriteString(`""`) } else { - buffer.WriteString((`"` + y + `"`)) + _, fErr := strconv.ParseFloat(y, 32) + _, bErr := strconv.ParseBool(y) + if fErr == nil { + buffer.WriteString(y) + } else if bErr == nil { + buffer.WriteString(strings.ToLower(y)) + } else { + // make room for json objects and arrays + switch string(y[0]) { + case "{": + fallthrough + case "[": + buffer.WriteString(y) + default: + buffer.WriteString(`"` + y + `"`) + } + } } //end of property if j < len(d)-1 { @@ -75,8 +101,14 @@ func ReadCSV(path *string) ([]byte, string) { } buffer.WriteString(`]`) - rawMessage := json.RawMessage(buffer.String()) - x, _ := json.MarshalIndent(rawMessage, "", " ") + // final correction for empty keys + str := buffer.String() + str = strings.Replace(str, ":}", `:""}`, -1) + rawMessage := json.RawMessage(str) + x, err := json.MarshalIndent(rawMessage, "", " ") + if err != nil { + log.Fatal(err) + } newFileName := filepath.Base(*path) newFileName = newFileName[0:len(newFileName)-len(filepath.Ext(newFileName))] + ".json" r := filepath.Dir(*path) From 636a360a59f6297701627ec54ee8723d0ac6e859 Mon Sep 17 00:00:00 2001 From: Bart Walczak Date: Tue, 12 Mar 2019 04:19:04 -0700 Subject: [PATCH 2/3] one more check for an empty property --- main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main.go b/main.go index d6368fd..df9b856 100644 --- a/main.go +++ b/main.go @@ -104,6 +104,7 @@ func ReadCSV(path *string) ([]byte, string) { // final correction for empty keys str := buffer.String() str = strings.Replace(str, ":}", `:""}`, -1) + str = strings.Replace(str, ":,", `:"",`, -1) rawMessage := json.RawMessage(str) x, err := json.MarshalIndent(rawMessage, "", " ") if err != nil { From 49eae95c9e7ba561817482f8faabf3a2a2ac2f53 Mon Sep 17 00:00:00 2001 From: Bart Walczak Date: Wed, 20 Mar 2019 03:44:08 -0500 Subject: [PATCH 3/3] fixing one comma check --- main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index df9b856..95334d7 100644 --- a/main.go +++ b/main.go @@ -104,8 +104,9 @@ func ReadCSV(path *string) ([]byte, string) { // final correction for empty keys str := buffer.String() str = strings.Replace(str, ":}", `:""}`, -1) - str = strings.Replace(str, ":,", `:"",`, -1) + str = strings.Replace(str, `:,"`, `:"","`, -1) rawMessage := json.RawMessage(str) + fmt.Println(string(rawMessage)) x, err := json.MarshalIndent(rawMessage, "", " ") if err != nil { log.Fatal(err)