Skip to content

Instantly share code, notes, and snippets.

@vdparikh
Created March 28, 2025 20:48
Show Gist options
  • Save vdparikh/8c78a1f63252ff47b0d05c7459bb9bc5 to your computer and use it in GitHub Desktop.
Save vdparikh/8c78a1f63252ff47b0d05c7459bb9bc5 to your computer and use it in GitHub Desktop.
generic api to parse json to csv
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"net/http"
"os"
)
func main() {
// API configurations
apiURL := "https://api.example.com/data" // Replace with your API URL
authToken := "your_bearer_token_or_api_key"
// Additional headers
headers := map[string]string{
"Authorization": "Bearer " + authToken,
"Accept": "application/json",
"User-Agent": "Go-CSV-Exporter",
}
// Create HTTP client and request
client := &http.Client{}
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// Add headers
for key, value := range headers {
req.Header.Set(key, value)
}
// Send request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return
}
defer resp.Body.Close()
// Check if response is OK
if resp.StatusCode != http.StatusOK {
fmt.Println("Error: Response status not OK:", resp.Status)
return
}
// Decode JSON response
var data map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
fmt.Println("Error decoding JSON:", err)
return
}
// Extract the array block (assuming it's under "items")
items, ok := data["items"].([]interface{})
if !ok || len(items) == 0 {
fmt.Println("Error: 'items' array is missing or empty in JSON response")
return
}
// Extract headers dynamically from the first item
firstItem, ok := items[0].(map[string]interface{})
if !ok {
fmt.Println("Error: Unable to parse first item as JSON object")
return
}
// Prepare CSV headers
var headersList []string
for key := range firstItem {
headersList = append(headersList, key)
}
// Create CSV file
csvFile, err := os.Create("output.csv")
if err != nil {
fmt.Println("Error creating CSV file:", err)
return
}
defer csvFile.Close()
// Initialize CSV writer
writer := csv.NewWriter(csvFile)
defer writer.Flush()
// Write headers to CSV
writer.Write(headersList)
// Write data rows dynamically
for _, item := range items {
itemMap, ok := item.(map[string]interface{})
if !ok {
continue
}
var row []string
for _, key := range headersList {
row = append(row, fmt.Sprintf("%v", itemMap[key])) // Convert values to string
}
writer.Write(row)
}
fmt.Println("CSV file created successfully as 'output.csv'")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment