How to convert json to string in golang and echo framework? - json

I have a json that I receive by post
{"endpoint": "assistance"}
I receive this like this
json_map: = make (map[string]interface{})
Now I need to assign it to a variable as a string but I don't know how to do it.
endpoint: = c.String (json_map ["endpoint"])

A type safe way to do this would be creating a struct that represents your request object and unmarshalling it.
This gives you a panic on unexpected requests.
package main
import (
"encoding/json"
"fmt"
)
type response struct {
Endpoint string
}
func main() {
jsonBody := []byte(`{"endpoint": "assistance"}`)
data := response{}
if err := json.Unmarshal(jsonBody, &data); err != nil {
panic(err)
}
fmt.Println(data.Endpoint)
}
// assistance
This program as an example safely decodes the JSON into a struct and prints the value.

What you are trying to achieve is not to convert a JSON to a string but an empty interface interface{} to a string You can achieve this by doing a type assertion:
endpoint, ok := json_map["endpoint"].(string)
if !ok {
// handle the error if the underlying type was not a string
}
Also as #Lex mentionned, it would probably be much safer to have a Go struct defining your JSON data. This way all your fields will be typed and you will no longer need this kind of type assertion.

Related

How to convert *elbv2.DescribeLoadBalancersOutput to an JSON in goLang to perform data parsing

I have been trying to work with AWS Go SDK Version 2. I am getting an where I want to convert the response I got from DescribeLoadBalancer() function into a JSON, so that I can parse it and perform further action but when I try to follow how to parse the JSON in go describe here https://blog.alexellis.io/golang-json-api-client/. I get an error in the compiler that I can't convert *elbv2.DescribeLoadBalancersOutput into []byte. I tried different approaches i.e trying to convert *elbv2.DescribeLoadBalancersOutput into string etc but I always get the same error i.e cannot convert *elbv2.DescribeLoadBalancersOutput into the specified type. hence want to understand what would be the best to perform the required action. Here's my code.
package main
import (
"encoding/gob"
"fmt"
"reflect"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elbv2"
)
type LoadBalancerARN struct {
loadBalancerARN string
}
func main() {
session, error := session.NewSession(&aws.Config{
Region: aws.String("eu-central-1"),
})
if error != nil {
fmt.Printf("Cannot create a session, %v", error)
}
service := elbv2.New(session)
input := &elbv2.DescribeLoadBalancersInput{}
getLoadBalancersJson, error := service.DescribeLoadBalancers(input)
if error != nil {
if aerr, ok := error.(awserr.Error); ok {
switch aerr.Code() {
case elbv2.ErrCodeLoadBalancerNotFoundException:
fmt.Println(elbv2.ErrCodeLoadBalancerNotFoundException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
fmt.Println(error.Error())
}
return
}
encodinglbJson := gob.NewEncoder(getLoadBalancersJson)
//var arn LoadBalancerARN
fmt.Println(getLoadBalancersJson)
fmt.Println(reflect.TypeOf(getLoadBalancersJson))
}
So, I have figured the above out, best way to do the above is, AWS provide a LoadBalancer function which convert the *elbv2.DescribeLoadBalancersOutput into an object of type *[]elbv2.LoadBalancer and then you can convert that object into the byte array using json.Marshal function and from there you can perform the parsing as needed.
Here's code
type LoadBalancerJSONStructure struct {
LoadBalancerArn string
DNSName string
CanonicalHostedZoneId string
CreatedTime string
LoadBalancerName string
Scheme string
VpcId string
Type string
IpAddressType string
}
response, error := json.Marshal(getLoadBalancers.LoadBalancers)
if error != nil {
fmt.Println("Error, %v", error)
}
fmt.Println(reflect.TypeOf(getLoadBalancers.LoadBalancers))
var lbJson LoadBalancerJSONStructure
err := json.Unmarshal(response, &lbJson)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Struct is: ", lbJson)

Golang equivalent to Python json.dumps and json.loads

This is a very weird situation but I need to convert a stringified json to something valid that I can unmarshall with:
"{\"hello\": \"hi\"}"
I want to be able to unmarshall this into a struct like this:
type mystruct struct {
Hello string `json:"hello,string"`
}
I know normally the unmarshall takes bytes but Im trying to convert what I currently get into something structified.
Any suggestions?
The issue is that the encoding/json package accepts well-formed JSON, in this case the initial JSON that you have has escaped quotes, first you have to unescape them, one way to do this is by using the strconv.Unquote function, here's a sample snippet:
package main
import (
"encoding/json"
"fmt"
"strconv"
)
type mystruct struct {
Hello string `json:"hello,omitempty"`
}
func main() {
var rawJSON []byte = []byte(`"{\"hello\": \"hi\"}"`)
s, _ := strconv.Unquote(string(rawJSON))
var val mystruct
if err := json.Unmarshal([]byte(s), &val); err != nil {
// handle error
}
fmt.Println(s)
fmt.Println(err)
fmt.Println(val.Hello)
}

Unmarshalling array of bytes to an interface and type cast that interface into struct doesn't work?

I have been coding in golang for a while now. I have come across something I thought would work perfectly fine.
When I JSON Marshal a nested struct in golang I get the array of bytes, when I UnMarshal the same into an interface and convert the interface into the respective nested struct, it gives me a panic stating interface conversion: interface is map[string]interface but not the nested struct.
Please go through the link below.
https://play.golang.org/p/apdR4TKjee-
Can someone explain to me what is that I am missing?
When you unmarshall JSON into interface{}, it has no way to know what type you want it to use, so it defaults to map[string]interface{} as indicated in the documentation:
To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:
...
map[string]interface{}, for JSON objects
If you want to unmarshal to a specific type, pass an instance of that type to Unmarshal:
var result sample
err = json.Unmarshal(data,&result)
You can not unmarshal to nil interface, however, if interface has a pointer to undelying structure it will work see below your updated code:
package main
import (
"encoding/json"
"fmt"
"github.com/viant/toolbox"
"log"
)
type sample struct {
Ping string `json:"ping"`
Pong sample1 `json:"long"`
}
type sample1 struct {
Play string `json:"plong"`
}
func main() {
var ans sample
var result interface{} = &sample{}
ans.Ping = "asda"
var ans1 sample1
ans1.Play = "asasd"
ans.Pong = ans1
fmt.Println(ans)
converter := toolbox.NewColumnConverter(toolbox.DefaultDateLayout)
var aMap = make(map[string]interface{})
converter.AssignConverted(&aMap, ans)
data, err := json.Marshal(ans)
fmt.Println(err)
fmt.Println(string(data), err)
err = json.Unmarshal(data,&result)
fmt.Println(result)
if err!= nil{
log.Fatal(err)
}
fmt.Println(result.(*sample))
fmt.Println(result)
}

Is there a way to extract JSON from an http response without having to build structs?

All of the ways I'm seeing involve building structs and unmarshalling the data into the struct. But what if I'm getting JSON responses with hundreds of fields? I don't want to have to create 100 field structs just to be able to get to the data I want. Coming from a Java background there are easy ways to simply get the http response as a string and then pass the JSON string into a JSON object that allows for easy traversal. It's very painless. Is there anything like this in Go?
Java example in pseudo code:
String json = httpResponse.getBody();
JsonObject object = new JsonObject(json);
object.get("desiredKey");
Golang: fetch JSON from an HTTP response without using structs as helpers
This is a typical scenario we come across. This is achieved by json.Unmarshal.
Here is a simple json
{"textfield":"I'm a text.","num":1234,"list":[1,2,3]}
which is serialized to send across the network and unmarshaled at Golang end.
package main
import (
"fmt"
"encoding/json"
)
func main() {
// replace this by fetching actual response body
responseBody := `{"textfield":"I'm a text.","num":1234,"list":[1,2,3]}`
var data map[string]interface{}
err := json.Unmarshal([]byte(responseBody), &data)
if err != nil {
panic(err)
}
fmt.Println(data["list"])
fmt.Println(data["textfield"])
}
Hope this was helpful.
The json.Unmarshal method will unmarshal to a struct that does not contain all the fields in the original JSON object. In other words, you can cherry-pick your fields. Here is an example where FirstName and LastName are cherry-picked and MiddleName is ignored from the json string:
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
func main() {
jsonString := []byte("{\"first_name\": \"John\", \"last_name\": \"Doe\", \"middle_name\": \"Anderson\"}")
var person Person
if err := json.Unmarshal(jsonString, &person); err != nil {
panic(err)
}
fmt.Println(person)
}
The other answers here are misleading, as they don't show you what happens if you try to go deeper in the Map. This example works fine enough:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func main() {
r, e := http.Get("https://github.com/manifest.json")
if e != nil {
panic(e)
}
body := map[string]interface{}{}
json.NewDecoder(r.Body).Decode(&body)
/*
[map[
id:com.github.android
platform:play
url:https://play.google.com/store/apps/details?id=com.github.android
]]
*/
fmt.Println(body["related_applications"])
}
but if you try to go one level deeper, it fails:
/*
invalid operation: body["related_applications"][0] (type interface {} does not
support indexing)
*/
fmt.Println(body["related_applications"][0])
Instead, you would need to assert type at each level of depth:
/*
map[
id:com.github.android
platform:play
url:https://play.google.com/store/apps/details?id=com.github.android
]
*/
fmt.Println(body["related_applications"].([]interface{})[0])
You can as well unmarshal it into a map[string]interface{}
body, err := ioutil.ReadAll(resp.Body)
map := &map[string]interface{}{}
json.Unmarshal(body, map)
desiredValue := map["desiredKey"]
The received json must have an object as the most outer element. The map can also contain lists or nested maps, depending on the json.

How do I Unmarshal JSON?

I am trying to unmarshal JSON into a struct. However, the struct has a field with a tag. Using reflection, and I try to see if the tag has the string "json" in it. If it does, then the json to unmarshal should simply be unmarshaled into the field as a string.
Example:
const data = `{"I":3, "S":{"phone": {"sales": "2223334444"}}}`
type A struct {
I int64
S string `sql:"type:json"`
}
Problem is simple - unmarshal "S" in the json as a string into the struct A.
This is how far I have come. But I am stuck here.
http://play.golang.org/p/YzrhjuXxGN
This is the go way of doing it - no reflection requred. Create a new type RawString and create MarshalJSON and UnmarshalJSON methods for it. (playground)
// RawString is a raw encoded JSON object.
// It implements Marshaler and Unmarshaler and can
// be used to delay JSON decoding or precompute a JSON encoding.
type RawString string
// MarshalJSON returns *m as the JSON encoding of m.
func (m *RawString) MarshalJSON() ([]byte, error) {
return []byte(*m), nil
}
// UnmarshalJSON sets *m to a copy of data.
func (m *RawString) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("RawString: UnmarshalJSON on nil pointer")
}
*m += RawString(data)
return nil
}
const data = `{"i":3, "S":{"phone": {"sales": "2223334444"}}}`
type A struct {
I int64
S RawString `sql:"type:json"`
}
func main() {
a := A{}
err := json.Unmarshal([]byte(data), &a)
if err != nil {
log.Fatal("Unmarshal failed", err)
}
fmt.Println("Done", a)
}
I modified the implementation of RawMessage to create the above.
The problem here is that you are using one encoding format for two protocols and probably there is something wrong in your model.
That is a valid Json payload and should be handled as such. One trick that you can use is to create
another field to handle the "string" json and one to handle the "json struct".
See this modified example: I first unmarshal json and then marshal back to json to create the final string to upload to the database. One field is used for unmarshaling and the other to communicate with the DB.
package main
import(
"fmt"
"encoding/json"
)
const data = `{"i":3, "S":{"phone": {"sales": "2223334444"}}}`
type A struct {
I int64
Sjson struct {
Phone struct {
Sales string `json:"sales"`
} `json:"phone"`
} `json:"S", sql:"-"`
S string `sql:"type:json",json:"-"`
}
func main() {
msg := A{}
_ = json.Unmarshal([]byte(data), &msg)
data, _ := json.Marshal(msg.Sjson)
msg.S = string(data)
fmt.Println("Done", msg)
}
Looks like the problem is that s interface{} in your code was not addressable. For Value.SetString the Value has to be addressable and with Kind String. you can check the documentation for it - http://golang.org/pkg/reflect/#Value.SetString
How i understand it SetString would not change the value in a, since you are only working with interface s. in Laws of Reflection you can find "reflect.ValueOf is a copy of x, not x itself"(3rd Law).
To make your code work I made some type assertions, and I used reflect.ValueOf on a pointer to asserted struct.
To check if Value is settable or addressable you can use Value.CanSet ad Value.CanAddr
working code: http://play.golang.org/p/DTriENkzA8
No idea whether its correct way to do this