douzhan1963 2019-06-18 12:31
浏览 229
已采纳

在Golang中将TXT文件转换为Json [关闭]

I'm struggling trying to convert a text file to JSON. I can't use python for limitations in the implementation so it needs to be in Go.

I have this file structure

VAULT_ADDR=https://address.com
SECRET_USERNAME={{tata/user}}
SECRET_PASSWORD={{tata/pass}}
SECRET_SLACK_TOKEN={{tata/token}}

SECRET_CONTENTFUL_SPACE_ID={{tata/space}}
SECRET_CONTENTFUL_ACCESS_TOKEN={{tata/stuff}}

SECRET_NEW_RELIC_TOKEN={{tata/tata}}

I'd need to covert it for a JSON structure, I tried a lot of things but don't know if I should use a structure, interface or how to do this.

would like to convert for this:

[  
{  
    "name":"VAULT_ADDR",
    "value":"https://address.com"
},
{  
    "name":"SECRET_USERNAME",
    "value":"{{tata/user}}"
},
{  
    "name":"SECRET_PASSWORD",
    "value":"{{tata/pass}}"
},
{  
    "name":"SECRET_SLACK_TOKEN",
    "value":"{{tata/token}}"
},
{  
    "name":"SECRET_CONTENTFUL_SPACE_ID",
    "value":"{{tata/space}}"
},
{  
    "name":"SECRET_CONTENTFUL_ACCESS_TOKEN",
    "value":"{{tata/stuff}}"
},
{  
    "name":"SECRET_NEW_RELIC_TOKEN",
    "value":"{{tata/tata}}"
}
]

this is my code so far...

package main

import (
  "flag"
  "fmt"
  "io/ioutil"
  "log"
  "os"
  "strings"
)

 var (
   file string
)

 func main() {

   flag.StringVar(&file, "f", "", "file path")
   flag.Parse()

   data, err := os.Open(file)
   if err != nil {
      log.Fatal(err)
   }
   defer data.Close()

   file, err := ioutil.ReadAll(data)

   converter := string(file)

   s := strings.Split(converter, "=")

   for _, line := range s {
      parser := "name" + line

      fmt.Println(parser)
   }

 }

My code output:

name= VAULT_ADDR value
name= https://address.com
SECRET_USERNAME value
name= {{tata/user}}
SECRET_PASSWORD value
name= {{tata/pass}}
SECRET_SLACK_TOKEN value
name= {{tata/token}}

SECRET_CONTENTFUL_SPACE_ID value
name= {{tata/space}}
SECRET_CONTENTFUL_ACCESS_TOKEN value
name= {{tata/stuff}}

SECRET_NEW_RELIC_TOKEN value
name= {{tata/tata}} value

thank you

  • 写回答

1条回答 默认 最新

  • duanbinren8906 2019-06-18 13:12
    关注

    You can read the data from a file as you do in your question and perform some extra validation and trimming on the strings and replace the template data with real. I am using fixed data to demonstrate how to do it.

    Here is the example:

    package main
    
    import (
        "encoding/json"
        "fmt"
        "strings"
    )
    
    type Element struct {
        Name  string `json:"name"`
        Value string `json:"value"`
    }
    
    func main() {
        var data = `VAULT_ADDR = "https://address.com"
    SECRET_USERNAME = "{{tata/user}}"
    SECRET_PASSWORD = "{{tata/pass}}"
    SECRET_SLACK_TOKEN = "{{tata/token}}"
    SECRET_CONTENTFUL_SPACE_ID = "{{tata/space}}"
    SECRET_CONTENTFUL_ACCESS_TOKEN = "{{tata/stuff}}"
    SECRET_NEW_RELIC_TOKEN = "{{tata/tata}}"`
    
        var dataSlice = make([]Element, 0)
        lines := strings.Split(data, "
    ")
        for _, line := range lines {
            keyVal := strings.Split(line, "=")
            dataSlice = append(dataSlice, Element{Name: keyVal[0], Value: keyVal[1]})
            // dataSlice[keyVal[0]] = keyVal[1]
            // in real code make sure the dataSlice has length==2
        }
        bts, err := json.Marshal(dataSlice)
        if err != nil {
            panic(err)
        }
        fmt.Printf("%s", bts)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 使用matlab将观测点聚合成多条目标轨迹
  • ¥15 Workbench中材料库无法更新,如何解决?
  • ¥20 如何推断此服务器配置
  • ¥15 关于github的项目怎么在pycharm上面运行
  • ¥15 内存地址视频流转RTMP
  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析