Skip to content

Instantly share code, notes, and snippets.

@rodrigorodriguescosta
Created September 27, 2021 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigorodriguescosta/94a1f3b7f821832a26c0b76ed0cdc371 to your computer and use it in GitHub Desktop.
Save rodrigorodriguescosta/94a1f3b7f821832a26c0b76ed0cdc371 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"io/ioutil"
)
type AutoGenerated struct {
ID string `json:"_id"`
Index int `json:"index"`
GUID string `json:"guid"`
IsActive bool `json:"isActive"`
Balance string `json:"balance"`
Picture string `json:"picture"`
Age int `json:"age"`
EyeColor string `json:"eyeColor"`
Name string `json:"name"`
Gender string `json:"gender"`
Company string `json:"company"`
Email string `json:"email"`
Phone string `json:"phone"`
Address string `json:"address"`
About string `json:"about"`
Registered string `json:"registered"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Pessoas []struct {
ID int `json:"id"`
Name string `json:"name"`
Salario string `json:"salario"`
Contatos []struct {
ID int `json:"id"`
Nome string `json:"nome"`
} `json:"contatos"`
} `json:"pessoas"`
Tags []string `json:"tags"`
Friends []struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"friends"`
Greeting string `json:"greeting"`
FavoriteFruit string `json:"favoriteFruit"`
}
func main() {
plan, _ := ioutil.ReadFile("C:\\Users\\Rodrigo\\Downloads\\generated2.json")
var data []AutoGenerated
err := json.Unmarshal(plan, &data)
if err != nil {
}
dsn := "host=localhost user=postgres password=test dbname=teste port=5432 sslmode=disable"
// create:=`create table empresa
//(
// id serial
// constraint empresa_pk
// primary key,
// doc jsonb
//);
//
//`
//db.Exec(create)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
fmt.Sprint(err)
}
fmt.Print("Inicio")
for _, value := range data {
jsonValue,err := json.Marshal(value)
if err != nil {
}
sql := fmt.Sprintf("INSERT INTO empresa(doc)values('%s')", fmt.Sprint(string(jsonValue)))
db.Exec(sql)
}
fmt.Print("Fim")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment