douzhan1963
douzhan1963
2019-06-18 12:31
浏览 140

在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
    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)
    }
    
    点赞 评论

相关推荐