Converting xml to json in Golang - json

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"`
}

Related

Map JSON array value to struct specific variable

Let's say I have this JSON structure:
{
"name":"repo",
"tags":["1.0","2.0","3.0"]
}
And I would like to map it to this Go struct:
type Repository struct {
Name string `json:"name"`
Tags []struct {
Tag string `json:"??"`
Sha256 string
}
}
How can I link the "tags" array JSON value to a struct field?
EDIT: The idea will be to access the tags array value like this
repository.Tags[0].Tag.
Implement json.Unmarshaler on a Tag type:
package main
import (
"encoding/json"
"log"
)
type Repository struct {
Name string
Tags []Tag
}
type Tag struct {
Tag string
Sha256 string
}
func (t *Tag) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
t.Tag = s
return nil
}
func main() {
b := []byte(`{ "name":"repo", "tags":["1.0","2.0","3.0"] }`)
var r Repository
err := json.Unmarshal(b, &r)
if err != nil {
log.Fatal(err)
}
log.Printf("%+v\n", r)
}
Try it on the playground: https://play.golang.org/p/ExwWhis0w0V
Marshaling back to JSON is left as an exercise for the reader.

Golang elegantly JSON decode different structures

I have different structures that share a field and I need to decode a JSON file into its corresponding structure in Go.
Example:
type Dog struct {
AnimalType string //will always be "dog"
BarkLoudnessLevel int
}
type Cat struct {
AnimalType string //will always be "cat"
SleepsAtNight bool
}
If I am receiving one of these structures as a JSON string, what would be the most elegant way of parsing it into its proper structure?
So, there are a couple ways of doing this, but the easiest is probably deserializing the payload twice and having conditional branches based off of the "AnimalType" attribute in your payload. Here's a simple example using an intermediate deserialization model:
package main
import (
"fmt"
"encoding/json"
)
type Dog struct {
AnimalType string //will always be "dog"
BarkLoudnessLevel int
}
type Cat struct {
AnimalType string //will always be "cat"
SleepsAtNight bool
}
var (
payloadOne = `{"AnimalType":"dog","BarkLoudnessLevel":1}`
payloadTwo = `{"AnimalType":"cat","SleepsAtNight":false}`
)
func main() {
parseAnimal(payloadOne)
parseAnimal(payloadTwo)
}
func parseAnimal(payload string) {
animal := struct{
AnimalType string
}{}
if err := json.Unmarshal([]byte(payload), &animal); err != nil {
panic(err)
}
switch animal.AnimalType {
case "dog":
dog := Dog{}
if err := json.Unmarshal([]byte(payload), &dog); err != nil {
panic(err)
}
fmt.Printf("Got a dog: %v\n", dog)
case "cat":
cat := Cat{}
if err := json.Unmarshal([]byte(payload), &cat); err != nil {
panic(err)
}
fmt.Printf("Got a cat: %v\n", cat)
default:
fmt.Println("Unknown animal")
}
}
See it in action here.
IMO a better way of approaching this is moving the "metadata" for the payload into a parent structure, though this requires modifying the expected json payload. So, for example, if you were working with payloads that looked like:
{"AnimalType":"dog", "Animal":{"BarkLoudnessLevel": 1}}
Then you could use something like json.RawMessage to partially parse the structure and then conditionally parse the rest as needed (rather than parsing everything twice)--also results in a nicer separation of structure attributes. Here's an example of how you'd do that:
package main
import (
"encoding/json"
"fmt"
)
type Animal struct {
AnimalType string
Animal json.RawMessage
}
type Dog struct {
BarkLoudnessLevel int
}
type Cat struct {
SleepsAtNight bool
}
var (
payloadOne = `{"AnimalType":"dog", "Animal":{"BarkLoudnessLevel": 1}}`
payloadTwo = `{"AnimalType":"cat", "Animal":{"SleepsAtNight": false}}`
)
func main() {
parseAnimal(payloadOne)
parseAnimal(payloadTwo)
}
func parseAnimal(payload string) {
animal := &Animal{}
if err := json.Unmarshal([]byte(payload), &animal); err != nil {
panic(err)
}
switch animal.AnimalType {
case "dog":
dog := Dog{}
if err := json.Unmarshal(animal.Animal, &dog); err != nil {
panic(err)
}
fmt.Printf("Got a dog: %v\n", dog)
case "cat":
cat := Cat{}
if err := json.Unmarshal(animal.Animal, &cat); err != nil {
panic(err)
}
fmt.Printf("Got a cat: %v\n", cat)
default:
fmt.Println("Unknown animal")
}
}
And in action here.

Parse JSON HTTP response using golang

I am trying to get the value of say "ip" from my following curl output:
{
"type":"example",
"data":{
"name":"abc",
"labels":{
"key":"value"
}
},
"subsets":[
{
"addresses":[
{
"ip":"192.168.103.178"
}
],
"ports":[
{
"port":80
}
]
}
]
}
I have found many examples in the internet to parse json output of curl requests and I have written the following code, but that doesn't seem to return me the value of say "ip"
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
type svc struct {
Ip string `json:"ip"`
}
func main() {
url := "http://myurl.com"
testClient := http.Client{
Timeout: time.Second * 2, // Maximum of 2 secs
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatal(err)
}
res, getErr := testClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
svc1 := svc{}
jsonErr := json.Unmarshal(body, &svc1)
if jsonErr != nil {
log.Fatal(jsonErr)
}
fmt.Println(svc1.Ip)
}
I would appreciate if anyone could provide me hints on what I need to add to my code to get the value of say "ip".
You can create structs which reflect your json structure and then decode your json.
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
)
type Example struct {
Type string `json:"type,omitempty"`
Subsets []Subset `json:"subsets,omitempty"`
}
type Subset struct {
Addresses []Address `json:"addresses,omitempty"`
}
type Address struct {
IP string `json:"IP,omitempty"`
}
func main() {
m := []byte(`{"type":"example","data": {"name": "abc","labels": {"key": "value"}},"subsets": [{"addresses": [{"ip": "192.168.103.178"}],"ports": [{"port": 80}]}]}`)
r := bytes.NewReader(m)
decoder := json.NewDecoder(r)
val := &Example{}
err := decoder.Decode(val)
if err != nil {
log.Fatal(err)
}
// If you want to read a response body
// decoder := json.NewDecoder(res.Body)
// err := decoder.Decode(val)
// Subsets is a slice so you must loop over it
for _, s := range val.Subsets {
// within Subsets, address is also a slice
// then you can access each IP from type Address
for _, a := range s.Addresses {
fmt.Println(a.IP)
}
}
}
The output would be:
192.168.103.178
By decoding this to a struct, you can loop over any slice and not limit yourself to one IP
Example here:
https://play.golang.org/p/sWA9qBWljA
One approach is to unmarshal the JSON to a map, e.g. (assumes jsData contains JSON string)
obj := map[string]interface{}{}
if err := json.Unmarshal([]byte(jsData), &obj); err != nil {
log.Fatal(err)
}
Next, implement a function for searching the value associated with a key from the map recursively, e.g.
func find(obj interface{}, key string) (interface{}, bool) {
//if the argument is not a map, ignore it
mobj, ok := obj.(map[string]interface{})
if !ok {
return nil, false
}
for k, v := range mobj {
//key match, return value
if k == key {
return v, true
}
//if the value is a map, search recursively
if m, ok := v.(map[string]interface{}); ok {
if res, ok := find(m, key); ok {
return res, true
}
}
//if the value is an array, search recursively
//from each element
if va, ok := v.([]interface{}); ok {
for _, a := range va {
if res, ok := find(a, key); ok {
return res,true
}
}
}
}
//element not found
return nil,false
}
Note, that the above function return an interface{}. You need to convert it to appropriate type, e.g. using type switch:
if ip, ok := find(obj, "ip"); ok {
switch v := ip.(type) {
case string:
fmt.Printf("IP is a string -> %s\n", v)
case fmt.Stringer:
fmt.Printf("IP implements stringer interface -> %s\n", v.String())
case int:
default:
fmt.Printf("IP = %v, ok = %v\n", ip, ok)
}
}
A working example can be found at https://play.golang.org/p/O5NUi4J0iR
Typically in these situations you will see people describe all of these sub struct types. If you don't actually need to reuse the definition of any sub structs (like as a type for a function argument), then you don't need to define them. You can just use one definition for the whole response. In addition, in some cases you don't need to define a type at all, you can just do it at the time of declaration:
package main
import "encoding/json"
const s = `
{
"subsets": [
{
"addresses": [
{"ip": "192.168.103.178"}
]
}
]
}
`
func main() {
var svc struct {
Subsets []struct {
Addresses []struct { Ip string }
}
}
json.Unmarshal([]byte(s), &svc)
ip := svc.Subsets[0].Addresses[0].Ip
println(ip == "192.168.103.178")
}
You can write your own decoder or use existing third-party decoders.
For instance, github.com/buger/jsonparser could solve your problem by iterating throw array (two times).
package main
import (
"github.com/buger/jsonparser"
"fmt"
)
var data =[]byte(`{
"type":"example",
"data":{
"name":"abc",
"labels":{
"key":"value"
}
},
"subsets":[
{
"addresses":[
{
"ip":"192.168.103.178"
}
],
"ports":[
{
"port":80
}
]
}
]
}`)
func main() {
jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
jsonparser.ArrayEach(value, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
v, _, _, err := jsonparser.Get(value, "ip")
if err != nil {
return
}
fmt.Println("ip: ", string(v[:]))
}, "addresses")
}, "subsets")
}
Output: ip: 192.168.103.178

Parsing a JSON file in Go

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()
}

Getting json dynamic key name as string?

For example:
{"id":
{"12345678901234":
{"Account":"asdf",
"Password":"qwerty"
"LastSeen":"1397621470",
}
}
}
A program I've been trying to make needs to get the id as a string and then later use it to check the time in LastSeen.
I've tried using simplejson and jsonq,but still cant figure out how to do that.
You can use RawMessage and make it much simpiler (play with it) :
package main
import (
"encoding/json"
"fmt"
)
var data []byte = []byte(`{"id": {"12345678901234": {"Account":"asdf", "Password":"qwerty", "LastSeen":"1397621470"}}}`)
type Message struct {
Id string
Info struct {
Account string
Password string
LastSeen string
}
}
func main() {
var (
tmpmsg struct {
Data map[string]json.RawMessage `json:"id"`
}
msg Message
)
if err := json.Unmarshal(data, &tmpmsg); err != nil {
panic(err) //you probably wanna use or something instead
}
for id, raw := range tmpmsg.Data {
msg.Id = id
if err := json.Unmarshal(raw, &msg.Info); err != nil {
panic(err)
}
}
fmt.Printf("%+v\n", msg)
}
Looking at the Golang blog post on JSON here it can be done using the encoding/json package. I created a small program to do this as follows:
package main
import (
"encoding/json"
"fmt"
)
var data []byte = []byte(`{"id": {"12345678901234": {"Account":"asdf", "Password":"qwerty", "LastSeen":"1397621470"}}}`)
type Message struct {
id string
LastSeen int64
}
var m Message
func main() {
var i interface {}
err := json.Unmarshal(data, &i)
if err != nil {
println("Error decoding data")
fmt.Printf("%s", err.Error())
return
}
m := i.(map[string]interface{})
for k, v := range m {
println(k)
im := v.(map[string]interface{})
for ik, iv := range im {
println("\t", ik)
jm := iv.(map[string]interface{})
for jk, jv := range jm {
println("\t\t", jk, ": ", jv.(string))
}
}
}
}
I apologise if this is poor in terms of Go best practices and such, I am new to the language. And I know that some elements of this aren't entirely necessary like the Message type definition but this works, at least on your data.