I am having trouble parsing a JSON file in Go.
I am not getting any errors, but I am not getting an output.
I have tried a few different methods, but I can't seem to get any to work.
Any help would be greatly appreciated. Thanks in advance.
package simplefiles
import (
"encoding/json"
"fmt"
"os"
)
//PluginInfo - struct for plugins.json
var PluginInfo struct {
LatestVersion string `json:"latest_version"`
LastUpdated string `json:"last_updated"`
Popular bool `json:"popular"`
Info []string `json:"Info"`
}
//ParsePlugins - parses plugins.json
func ParsePlugins() {
pluginsFile, err := os.Open("test.json")
if err != nil {
fmt.Println("opening config file", err.Error())
}
jsonParser := json.NewDecoder(pluginsFile)
if err = jsonParser.Decode(&PluginInfo); err != nil {
fmt.Println("parsing config file", err.Error())
} else {
fmt.Printf(PluginInfo.LastUpdated)
}
return
}
JSON Sample:
{
"my-test-site":{
"latest_version":"6.4.5",
"last_updated":"2016-05-22T00:23:00.000Z",
"popular":true,
"infomation":[
{
"id":6043,
"title":"Test info 1",
"created_at":"2014-08-01T10:58:35.000Z",
"updated_at":"2015-05-15T13:47:24.000Z",
"published_date":null,
"references":{
"url":[
"http://samplesite1.com",
"http://samplesite2.com"
]
},
"site_type":"info",
"fixed_v":"1.10"
}
]
},
"another-test-site":{
"latest_version":"2.1.0",
"last_updated":"2016-06-12T08:36:00.000Z",
"popular":false,
"infomation":[
{
"id":6044,
"title":"Test site 2 info",
"created_at":"2014-08-01T10:58:35.000Z",
"updated_at":"2015-05-15T13:47:24.000Z",
"published_date":null,
"references":{
"otherinfo":[
"blah blah blah"
]
},
"site_type":"search",
"fixed_v":"1.2.0"
}
]
}
}
Your problem is that your JSON data is a map of string to the struct type you defined, not the struct type directly. If you modify your code slightly as below it works, but you need to index into the map to get each struct value:
package main
import (
"encoding/json"
"fmt"
"os"
)
//PluginInfo - struct for plugins.json
var PluginInfo map[string]struct { // NOTICE map of string to struct
LatestVersion string `json:"latest_version"`
LastUpdated string `json:"last_updated"`
Popular bool `json:"popular"`
Info []string `json:"Info"`
}
//ParsePlugins - parses plugins.json
func ParsePlugins() {
pluginsFile, err := os.Open("test.json")
if err != nil {
fmt.Println("opening config file", err.Error())
}
jsonParser := json.NewDecoder(pluginsFile)
if err = jsonParser.Decode(&PluginInfo); err != nil {
fmt.Println("parsing config file", err.Error())
} else {
for key, val := range PluginInfo {
fmt.Printf("Key %q, last updated %s\n", key, val.LastUpdated)
}
}
return
}
func main() {
ParsePlugins()
}
Related
I'm using github.com/basgys/goxml2json for xml to json conversion. Below is the example code:
package main
import (
"fmt"
"strings"
xj "github.com/basgys/goxml2json"
)
func main() {
xml := strings.NewReader(`<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2">
<bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/>
<foo>bar</foo>
</osm>`)
json, err := xj.Convert(xml)
if err != nil {
panic("ERROR converting xml to json")
}
fmt.Println(json.String())
}
The output of the above code is:
{
"osm": {
"-version": 0.6,
"-generator": "CGImap 0.0.2",
"bounds": {
"-minlat": "54.0889580",
"-minlon": "12.2487570",
"-maxlat": "54.0913900",
"-maxlon": "12.2524800"
},
"foo": "bar"
}
}
However, I am expecting the output like below as given by https://codebeautify.org/xmltojson/y2221f265:
{
"osm": {
"bounds": "",
"foo": "bar"
}
}
How to remove the keys starting with - from the JSON output? I do not know the structure of the data beforehand.
I think this should do it. It is a modified version of the goxml2json.Convert func. Used the WithAttrPrefix to specify a custom prefix(in case you ever want to use a - at the start of your body).
Please note that this only works for the latest commit on the master branch, the v1.1.0 tag doesn't support plugins, so you have to go get it like so: go get github.com/basgys/goxml2json#master
RemoveAttr just recursively deletes all children with the given prefix. You could also do other modifications at this point.
package main
import (
"bytes"
"fmt"
"strings"
xj "github.com/basgys/goxml2json"
)
const prefix = "veryuniqueattrprefix-"
func main() {
xml := strings.NewReader(`<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2">
<bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/>
<foo>bar</foo>
</osm>`)
// Decode XML document
root := &xj.Node{}
err := xj.NewDecoder(
xml,
xj.WithAttrPrefix(prefix),
).Decode(root)
if err != nil {
panic(err)
}
RemoveAttr(root)
// Then encode it in JSON
buf := new(bytes.Buffer)
e := xj.NewEncoder(buf)
err = e.Encode(root)
if err != nil {
panic(err)
}
fmt.Println(buf.String())
}
func RemoveAttr(n *xj.Node) {
for k, v := range n.Children {
if strings.HasPrefix(k, prefix) {
delete(n.Children, k)
} else {
for _, n := range v {
RemoveAttr(n)
}
}
}
}
outputs:
{"osm": {"bounds": "", "foo": "bar"}}
Try this
package main
import (
"encoding/json"
"fmt"
"strings"
xj "github.com/basgys/goxml2json"
)
// ToObject - convert string to any given struct
func ToObject(value string, object interface{}) error {
err := json.Unmarshal([]byte(value), &object)
if err != nil {
return err
}
return nil
}
func main() {
xml := strings.NewReader(`<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2">
<bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/>
<foo>bar</foo>
</osm>`)
converter, err := xj.Convert(xml)
if err != nil {
panic("ERROR converting xml to json")
}
osmStruct := &ExampleStruct{}
ToObject(converter.String(), &osmStruct)
b, _ := json.Marshal(osmStruct)
fmt.Println(string(b))
}
// ExampleStruct -
type ExampleStruct struct {
Osm Osm `json:"osm"`
}
// Osm -
type Osm struct {
Version string `json:",omitempty"`
Generator string `json:",omitempty"`
Bounds Bounds `json:"bounds"`
Foo string `json:"foo"`
}
// Bounds -
type Bounds struct {
Minlat string `json:",omitempty"`
Minlon string `json:",omitempty"`
Maxlat string `json:",omitempty"`
Maxlon string `json:",omitempty"`
}
I am trying to save this astros.json file into csv file.
Everything is working I don't have any error but my code is not going into my for loop.
So the astros.csv file is created successfully but there are no datas inside.
package main
import (
"encoding/json"
"encoding/csv"
"fmt"
"io/ioutil"
"os"
"net/http"
"strconv"
)
type People struct {
Name string
Craft string
}
type General struct {
//People []People
Number int
Message string
}
func main() {
// Reading data from JSON File
response, err := http.Get("http://api.open-notify.org/astros.json")
if err != nil {
fmt.Printf("The Http request failed with error %s\n", err)
}
data,_ := ioutil.ReadAll(response.Body)
//fmt.Println(string(data))
// Unmarshal JSON data
var general []General
json.Unmarshal([]byte(data), &general)
//fmt.Printf("First person: %s, Message: %s", general.People[0].Name, general.Message)
// Create a csv file
csvdatafile, err := os.Create("./astros.csv")
if err != nil {
fmt.Println(err)
}
defer csvdatafile.Close()
// Write Unmarshaled json data to CSV file
w := csv.NewWriter(csvdatafile)
for _, obj := range general {
fmt.Println("Are you going into the for ?")
var record []string
//record = append(record, obj.People)
record = append(record, strconv.Itoa(obj.Number), obj.Message)
record = append(record, obj.Message)
//record = append(record, []string{People})
w.Write(record)
fmt.Println("Are you coming here")
record = nil
}
w.Flush()
fmt.Println("Appending succed")
}
It is important to always check for errors; if you change your code to:
err = json.Unmarshal([]byte(data), &general)
if err != nil {
fmt.Printf("Error in unmarshall %s\n", err)
return
}
Then you would see the problem; json.Unmarshal is returning cannot unmarshal object into Go value of type []main.General. This means that general is nil so your loop is never entered.
The reason for this is apparent when you compare the JSON:
{
"people": [{
"name": "Christina Koch",
"craft": "ISS"
}, {
"name": "Alexander Skvortsov",
"craft": "ISS"
}, {
"name": "Luca Parmitano",
"craft": "ISS"
}, {
"name": "Andrew Morgan",
"craft": "ISS"
}, {
"name": "Oleg Skripochka",
"craft": "ISS"
}, {
"name": "Jessica Meir",
"craft": "ISS"
}
],
"number": 6,
"message": "success"
}
With the variable you are unmarshalling into; []General where General is:
type General struct {
//People []People
Number int
Message string
}
It looks like you had the right idea at some point with the []People but then commented it out.
If you uncomment []People, unmarshal into a General (rather than []General) then make your loop for _, obj := range general.People { it should work as expected. This assumes that you want to iterate through the people; general.Message and general.Number are also available (but you dont need a loop to access these).
See this example (have removed file operation as the playground does not support that).
you could try something like this:
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
)
type People struct {
Name string
Craft string
}
type Response struct {
People []People
Number int
Message string
}
func main() {
// Reading data from JSON File
r, err := http.Get("http://api.open-notify.org/astros.json")
if err != nil {
fmt.Printf("The Http request failed with error %s\n", err)
}
var response Response
err = json.NewDecoder(r.Body).Decode(&response)
if err != nil {
fmt.Printf("decoding error%s\n", err)
return
}
// Create a csv file
csvdatafile, err := os.Create("./astros.csv")
if err != nil {
fmt.Println(err)
}
defer csvdatafile.Close()
// Write Unmarshaled json data to CSV file
w := csv.NewWriter(csvdatafile)
for _, obj := range response.People {
fmt.Println("Are you going into the for ?")
var record []string
//record = append(record, obj.People)
record = append(record, strconv.Itoa(response.Number), response.Message)
record = append(record, obj.Name)
// record = append(record, []string{obj})
w.Write(record)
fmt.Println("Are you coming here")
record = nil
}
w.Flush()
fmt.Println("Appending succed")
}
I can't figure out how to unmarshal the json data provided by an api and consume the data to print in a specified format.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type postOffice []struct {
Name string
Taluk string
Region string
Country string
}
func main() {
data, err := http.Get("http://postalpincode.in/api/pincode/221010")
if err != nil {
fmt.Printf("The http request has a error : %s", err)
} else {
read, _ := ioutil.ReadAll(data.Body)
var po postOffice
err = json.Unmarshal(read, &po)
if err != nil {
fmt.Printf("%s", err)
}
fmt.Print(po)
}
}
The code was working well till the "read" was evaluated but is throwing the following error on using json.Unmarshal "json: cannot unmarshal object into Go value of type main.post[]"
You need create a second struct to receive the whole JSON.
type JSONResponse struct {
Message string `json:"Message"`
Status string `json:"Success"`
PostOffice postOffice `json:"PostOffice"`
}
This is because the PostOffice is an array inside of the response.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
//this is the new struct
type JSONResponse struct {
Message string `json:"Message"`
Status string `json:"Success"`
PostOffice postOffice `json:"PostOffice"`
}
type postOffice []struct {
Name string
Taluk string
Region string
Country string
}
func main() {
data, err := http.Get("http://postalpincode.in/api/pincode/221010")
if err != nil {
fmt.Printf("The http request has a error : %s", err)
} else {
read, _ := ioutil.ReadAll(data.Body)
//change the type of the struct
var po JSONResponse
err = json.Unmarshal(read, &po)
if err != nil {
fmt.Printf("%s", err)
}
fmt.Print(po)
}
}
I hope someone could help me with this issue because I have been scratching my head for a while.
I have a project where I am trying to load json into a struct in go. I have followed exactly several tutorials online, but keep getting no data back and no error.
My json file is called page_data.json and looks like:
[
{
"page_title": "Page1",
"page_description": "Introduction",
"link": "example_link",
"authors":
[
"Author1",
"Author2",
"Author3",
]
},
// second object, same as the first
]
But when I try the following in go:
package main
import (
"fmt"
"encoding/json"
"os"
"io/ioutil"
)
type PageData struct {
Title string `json: "page_title"`
Description string `json: "page_description"`
Link string `json: "link"`
Authors []string `json: "authors"`
}
func main() {
var numPages int = LoadPageData("page_data.json")
fmt.Printf("Num Pages: %d", numPages)
}
func LoadPageData(path string) int {
jsonFile, err := os.Open(path)
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var pageList []PageData
json.Unmarshal(byteValue, &pageList)
return len(pageList)
}
the output I get is:
Num Pages: 0
Fix the JSON commas and the Go struct field tags. For example,
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type PageData struct {
Title string `json:"page_title"`
Description string `json:"page_description"`
Link string `json:"link"`
Authors []string `json:"authors"`
}
func main() {
var numPages int = LoadPageData("page_data.json")
fmt.Printf("Num Pages: %d\n", numPages)
}
func LoadPageData(path string) int {
jsonFile, err := os.Open(path)
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
fmt.Println(err)
}
var pageList []PageData
err = json.Unmarshal(byteValue, &pageList)
if err != nil {
fmt.Println(err)
}
fmt.Println(pageList)
return len(pageList)
}
Output:
[{Page1 Introduction example_link [Author1 Author2 Author3]}]
page_data.json:
[
{
"page_title": "Page1",
"page_description": "Introduction",
"link": "example_link",
"authors":
[
"Author1",
"Author2",
"Author3"
]
}
]
I just starting learn this Go Lang programming, and now i'm stuck with the [] things, I tried to create a blog using the Go Lang and i'm using a template, there's no problem with the template, it just I want to append a data that I got from json file.
If I just take the data and send it through the file it's already done, but the problem is when I tried to append the slug to the data (because the json file i got no slug url in it.
That's why I want to get the title of the post then make a slug with it, then
package main
import (
"encoding/json"
"fmt"
"github.com/gosimple/slug"
"html/template"
"io/ioutil"
"net/http"
"os"
)
type Blog struct {
Title string
Author string
Header string
}
type Posts struct {
Posts []Post `json:"posts"`
}
type Post struct {
Title string `json:"title"`
Author string `json:"author"`
Content string `json:"content"`
PublishDate string `json:"publishdate"`
Image string `json:"image"`
}
type BlogViewModel struct {
Blog Blog
Posts []Post
}
func loadFile(fileName string) (string, error) {
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return "", err
}
return string(bytes), nil
}
func loadPosts() []Post {
jsonFile, err := os.Open("source/posts.json")
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully open the json file")
defer jsonFile.Close()
bytes, _ := ioutil.ReadAll(jsonFile)
var post []Post
json.Unmarshal(bytes, &post)
return post
}
func handler(w http.ResponseWriter, r *http.Request) {
blog := Blog{Title: "asd", Author: "qwe", Header: "zxc"}
posts := loadPosts()
viewModel := BlogViewModel{Blog: blog, Posts: posts}
t, _ := template.ParseFiles("template/blog.html")
t.Execute(w, viewModel)
}
The error is show in the main function
func main() {
posts := loadPosts()
for i := 0; i < len(posts.Post); i++ { // it gives me this error posts.Post undefined (type []Post has no field or method Post)
fmt.Println("Title: " + posts.Post[i].Title)
}
// http.HandleFunc("/", handler)
// fs := http.FileServer(http.Dir("template"))
// http.Handle("/assets/css/", fs)
// http.Handle("/assets/js/", fs)
// http.Handle("/assets/fonts/", fs)
// http.Handle("/assets/images/", fs)
// http.Handle("/assets/media/", fs)
// fmt.Println(http.ListenAndServe(":9000", nil))
}
I already try to solved it a couple of hours but I hit the wall, I think it's possible but I just don't find the way, I don't know what is the good keyword to solve the problem.
And I don't if I already explain good enough or not. Please help me, thank you
This is the JSON file format
{
"posts": [
{
"title": "sapien ut nunc",
"author": "Jeni",
"content": "jgwilt0#mapquest.com",
"publishdate": "26.04.2017",
"image": "http://dummyimage.com/188x199.png/cc0000/ffffff"
},
{
"title": "mus vivamus vestibulum sagittis",
"author": "Analise",
"content": "adonnellan1#biblegateway.com",
"publishdate": "13.03.2017",
"image": "http://dummyimage.com/182x113.bmp/ff4444/ffffff"
}
]
}
You're loadPost method returns []Post. Your definition of Post does not contain the attribute Post. Your Posts struct does.
Here is a modified working example. I didn't define your other structures for brevity.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
var rawJson = `{
"posts": [
{
"title": "sapien ut nunc",
"author": "Jeni",
"content": "jgwilt0#mapquest.com",
"publishdate": "26.04.2017",
"image": "http://dummyimage.com/188x199.png/cc0000/ffffff"
},
{
"title": "mus vivamus vestibulum sagittis",
"author": "Analise",
"content": "adonnellan1#biblegateway.com",
"publishdate": "13.03.2017",
"image": "http://dummyimage.com/182x113.bmp/ff4444/ffffff"
}
]
}`
type Data struct {
Posts []struct {
Title string `json:"title"`
Author string `json:"author"`
Content string `json:"content"`
Publishdate string `json:"publishdate"`
Image string `json:"image"`
} `json:"posts"`
}
func loadFile(fileName string) (string, error) {
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return "", err
}
return string(bytes), nil
}
func loadData() (Data, error) {
var d Data
// this is commented out so that i can load raw bytes as an example
/*
f, err := os.Open("source/posts.json")
if err != nil {
return d, err
}
defer f.Close()
bytes, _ := ioutil.ReadAll(f)
*/
// replace rawJson with bytes in prod
json.Unmarshal([]byte(rawJson), &d)
return d, nil
}
func main() {
data, err := loadData()
if err != nil {
log.Fatal(err)
}
for i := 0; i < len(data.Posts); i++ {
fmt.Println("Title: " + data.Posts[i].Title)
}
/*
// you can also range over the data.Posts if you are not modifying the data then using the index is not necessary.
for _, post := range data.Posts {
fmt.Println("Title: " + post.Title)
}
*/
}
modified just for files
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type Data struct {
Posts []struct {
Title string `json:"title"`
Author string `json:"author"`
Content string `json:"content"`
Publishdate string `json:"publishdate"`
Image string `json:"image"`
} `json:"posts"`
}
func loadData() (Data, error) {
var d Data
b, err := ioutil.ReadFile("source/posts.json")
if err != nil {
return d, err
}
err = json.Unmarshal(b, &d)
if err != nil {
return d, err
}
return d, nil
}
func main() {
data, err := loadData()
if err != nil {
log.Fatal(err)
}
for _, post := range data.Posts {
fmt.Println("Title: " + post.Title)
}
}