Last active
July 20, 2017 23:00
-
-
Save smt923/879b482e1aef6c5daee0fe293e20324e to your computer and use it in GitHub Desktop.
json to csv converter for a friend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment