Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/csv"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "os"
- )
- type Tweets []struct {
- Fullname string `json:"fullname"`
- ID string `json:"id"`
- Text string `json:"text"`
- Timestamp string `json:"timestamp"`
- User string `json:"user"`
- }
- func main() {
- if len(os.Args) < 3 {
- fmt.Printf("Usage: %s infile.json outfile.csv (order is important, name doesn't matter)", os.Args[0])
- os.Exit(1)
- }
- infile, err := ioutil.ReadFile(os.Args[1])
- if err != nil {
- panic(err)
- }
- var JSONTweets Tweets
- err = json.Unmarshal([]byte(infile), &JSONTweets)
- if err != nil {
- panic(err)
- }
- out, err := os.Create(os.Args[2])
- if err != nil {
- panic(err)
- }
- defer out.Close()
- w := csv.NewWriter(out)
- w.Write([]string{"timestamp", "text"})
- for _, obj := range JSONTweets {
- var record []string
- record = append(record, obj.Timestamp)
- record = append(record, obj.Text)
- w.Write(record)
- }
- w.Flush()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement