将动态JSON转换为CSV在Golang中的实现

huangapple go评论91阅读模式
英文:

Converting dynamic JSON to CSV in Golang

问题

我尝试将动态JSON转换为CSV,我调查了一些库和答案,但没有找到一个显著的解决方案。

这个例子这个例子可能会有所帮助,但我无法将JSON的结构添加到我的代码中,因为JSON是动态的。

在Python和JS中,我看到了以下这些例子:

Python:

  1. # Python program to convert
  2. # JSON file to CSV
  3. import json
  4. import csv
  5. # Opening JSON file and loading the data
  6. # into the variable data
  7. with open('data.json') as json_file:
  8. data = json.load(json_file)
  9. employee_data = data['emp_details']
  10. # now we will open a file for writing
  11. data_file = open('data_file.csv', 'w')
  12. # create the csv writer object
  13. csv_writer = csv.writer(data_file)
  14. # Counter variable used for writing
  15. # headers to the CSV file
  16. count = 0
  17. for emp in employee_data:
  18. if count == 0:
  19. # Writing headers of CSV file
  20. header = emp.keys()
  21. csv_writer.writerow(header)
  22. count += 1
  23. # Writing data of CSV file
  24. csv_writer.writerow(emp.values())
  25. data_file.close()

JS:

  1. const items = json3.items
  2. const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
  3. const header = Object.keys(items[0])
  4. const csv = [
  5. header.join(','), // header row first
  6. ...items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
  7. ].join('\r\n')
  8. console.log(csv)

这些代码可以帮助你轻松地将动态JSON转换为CSV。

示例输入和输出:

JSON输入:

  1. [
  2. {
  3. "name": "John",
  4. "age": "21"
  5. },
  6. {
  7. "name": "Noah",
  8. "age": "23"
  9. },
  10. {
  11. "name": "Justin",
  12. "age": "25"
  13. }
  14. ]

CSV输出:

  1. "name","age"
  2. "John","21"
  3. "Noah","23"
  4. "Justi","25"

那么,我该如何在Go中将动态JSON转换为CSV呢?

PS:我发现了一个Golang库(json2csv),可以帮助进行转换,但只能在命令提示符中使用。

还有一些在线工具,例如:

https://csvjson.com/json2csv

https://data.page/json/csv

英文:

I tried to convert dynamic JSON to CSV, I investigate libs and answers I can't find a remarkable thing.

This and this examples could be helpful but I can't add the JSON's struct to my code, JSON is dynamic.

In Python & JS, I saw these examples;

Python;

  1. # Python program to convert
  2. # JSON file to CSV
  3. import json
  4. import csv
  5. # Opening JSON file and loading the data
  6. # into the variable data
  7. with open('data.json') as json_file:
  8. data = json.load(json_file)
  9. employee_data = data['emp_details']
  10. # now we will open a file for writing
  11. data_file = open('data_file.csv', 'w')
  12. # create the csv writer object
  13. csv_writer = csv.writer(data_file)
  14. # Counter variable used for writing
  15. # headers to the CSV file
  16. count = 0
  17. for emp in employee_data:
  18. if count == 0:
  19. # Writing headers of CSV file
  20. header = emp.keys()
  21. csv_writer.writerow(header)
  22. count += 1
  23. # Writing data of CSV file
  24. csv_writer.writerow(emp.values())
  25. data_file.close()

JS;

  1. const items = json3.items
  2. const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
  3. const header = Object.keys(items[0])
  4. const csv = [
  5. header.join(','), // header row first
  6. ...items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
  7. ].join('\r\n')
  8. console.log(csv)

These codes help convert dynamic JSON to CSV easily.

Example input & output;

JSON(input);

  1. [
  2. {
  3. "name": "John",
  4. "age": "21"
  5. },
  6. {
  7. "name": "Noah",
  8. "age": "23"
  9. },
  10. {
  11. "name": "Justin",
  12. "age": "25"
  13. }
  14. ]

CSV(output);

  1. "name","age"
  2. "John","21"
  3. "Noah","23"
  4. "Justi","25"

So how can I convert dynamic JSON to CSV in Go?

PS: I discover a Golang lib(json2csv) that helps to convert but only works on command prompt.

I few online tools for example;

https://csvjson.com/json2csv

https://data.page/json/csv

答案1

得分: 2

经过调查,我使用yukithm/json2csv包来处理它。

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/yukithm/json2csv"
  6. "log"
  7. "os"
  8. )
  9. func main() {
  10. b := &bytes.Buffer{}
  11. wr := json2csv.NewCSVWriter(b)
  12. j, _ := os.ReadFile("your-input-path\\input.json")
  13. var x []map[string]interface{}
  14. // 解组json
  15. err := json.Unmarshal(j, &x)
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. // 将json转换为CSV
  20. csv, err := json2csv.JSON2CSV(x)
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. // CSV字节转换和写入...
  25. err = wr.WriteCSV(csv)
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. wr.Flush()
  30. got := b.String()
  31. //以下行打印CSV
  32. println(got)
  33. // 如果需要,创建文件并追加文本
  34. createFileAppendText("output.csv", got)
  35. }
  36. //
  37. func createFileAppendText(filename string, text string) {
  38. f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
  39. if err != nil {
  40. panic(err)
  41. }
  42. defer f.Close()
  43. if _, err = f.WriteString(text); err != nil {
  44. panic(err)
  45. }
  46. }

input.json;

  1. [
  2. {
  3. "Name": "Japan",
  4. "Capital": "Tokyo",
  5. "Continent": "Asia"
  6. },
  7. {
  8. "Name": "Germany",
  9. "Capital": "Berlin",
  10. "Continent": "Europe"
  11. },
  12. {
  13. "Name": "Turkey",
  14. "Capital": "Ankara",
  15. "Continent": "Europe"
  16. },
  17. {
  18. "Name": "Greece",
  19. "Capital": "Athens",
  20. "Continent": "Europe"
  21. },
  22. {
  23. "Name": "Israel",
  24. "Capital": "Jerusalem",
  25. "Continent": "Asia"
  26. }
  27. ]

output.csv

  1. /Capital,/Continent,/Name
  2. Tokyo,Asia,Japan
  3. Berlin,Europe,Germany
  4. Ankara,Europe,Turkey
  5. Athens,Europe,Greece
  6. Jerusalem,Asia,Israel
英文:

After investigation, I handle it with yukithm/json2csv package.

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/yukithm/json2csv"
  6. "log"
  7. "os"
  8. )
  9. func main() {
  10. b := &bytes.Buffer{}
  11. wr := json2csv.NewCSVWriter(b)
  12. j, _ := os.ReadFile("your-input-path\\input.json")
  13. var x []map[string]interface{}
  14. // unMarshall json
  15. err := json.Unmarshal(j, &x)
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. // convert json to CSV
  20. csv, err := json2csv.JSON2CSV(x)
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. // CSV bytes convert & writing...
  25. err = wr.WriteCSV(csv)
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. wr.Flush()
  30. got := b.String()
  31. //Following line prints CSV
  32. println(got)
  33. // create file and append if you want
  34. createFileAppendText("output.csv", got)
  35. }
  36. //
  37. func createFileAppendText(filename string, text string) {
  38. f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
  39. if err != nil {
  40. panic(err)
  41. }
  42. defer f.Close()
  43. if _, err = f.WriteString(text); err != nil {
  44. panic(err)
  45. }
  46. }

input.json;

  1. [
  2. {
  3. "Name": "Japan",
  4. "Capital": "Tokyo",
  5. "Continent": "Asia"
  6. },
  7. {
  8. "Name": "Germany",
  9. "Capital": "Berlin",
  10. "Continent": "Europe"
  11. },
  12. {
  13. "Name": "Turkey",
  14. "Capital": "Ankara",
  15. "Continent": "Europe"
  16. },
  17. {
  18. "Name": "Greece",
  19. "Capital": "Athens",
  20. "Continent": "Europe"
  21. },
  22. {
  23. "Name": "Israel",
  24. "Capital": "Jerusalem",
  25. "Continent": "Asia"
  26. }
  27. ]

output.csv

  1. /Capital,/Continent,/Name
  2. Tokyo,Asia,Japan
  3. Berlin,Europe,Germany
  4. Ankara,Europe,Turkey
  5. Athens,Europe,Greece
  6. Jerusalem,Asia,Israel

答案2

得分: 0

将以下内容粘贴到 https://github.com/yukithm/json2csv/blob/master/cmd/json2csv/main.go 并将该函数插入到 main 函数中。

  1. func stackoverflow() {
  2. jsonStr := `
  3. [
  4. {
  5. "name": "John",
  6. "age": "21"
  7. },
  8. {
  9. "name": "Noah",
  10. "age": "23"
  11. },
  12. {
  13. "name": "Justin",
  14. "age": "25"
  15. }
  16. ]`
  17. buff := bytes.NewBufferString(jsonStr)
  18. data, _ := readJSON(buff)
  19. results, _ := json2csv.JSON2CSV(data)
  20. headerStyle := headerStyleTable["jsonpointer"]
  21. err := printCSV(os.Stdout, results, headerStyle, false)
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. }

这对我有效。

  1. json2csv git:(master) go run main.go
  2. /age,/name
  3. 21,John
  4. 23,Noah
  5. 25,Justin
英文:

Paste this to https://github.com/yukithm/json2csv/blob/master/cmd/json2csv/main.go and insert the func to main.

  1. func stackoverflow() {
  2. jsonStr := `
  3. [
  4. {
  5. "name": "John",
  6. "age": "21"
  7. },
  8. {
  9. "name": "Noah",
  10. "age": "23"
  11. },
  12. {
  13. "name": "Justin",
  14. "age": "25"
  15. }
  16. ]`
  17. buff := bytes.NewBufferString(jsonStr)
  18. data, _ := readJSON(buff)
  19. results, _ := json2csv.JSON2CSV(data)
  20. headerStyle := headerStyleTable["jsonpointer"]
  21. err := printCSV(os.Stdout, results, headerStyle, false)
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. }

It works for me.

  1. json2csv git:(master) go run main.go
  2. /age,/name
  3. 21,John
  4. 23,Noah
  5. 25,Justin

huangapple
  • 本文由 发表于 2022年8月10日 16:17:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/73302954.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定