convert string to json in golang and vice versa?

MayK :

In my app, I receive a json from the client. This json can be anything since the user defines the keys and the values. In the backend I store it as string in the datastore.

Now i'm trying to override the MarshalJson / UnmarshalJson functions so that what I send / receive from the client is not a string but a json.

I can't figure out how to convert a string to json in go.

my structure

type ContextData string
type Iot struct {
Id              IotId       `json:"id,string" datastore:"-" goon:"id"`
Name            string   `json:"name"`
Context         ContextData  `json:"context" datastore:",noindex"` }

example of received data

{ 'id' : '',
  'name' '',
  'context': {
           'key1': value1,
           'key2': value2 }}

how i want to store this Context field in the datastore as a noindex string '{'key1':value1, 'key2':value2}' example of data i want to send

{ 'id' : '',
  'name' '',
  'context': {
           'key1': value1,
           'key2': value2 }}
tomasz :

If I understand your problem correctly, you want to use json.RawMessage as Context.

RawMessage is a raw encoded JSON object. It implements Marshaler and Unmarshaler and can be used to delay JSON decoding or precompute a JSON encoding.

RawMessage is just []byte, so you can keep it in data store and then attach it for an outgoing messages as "precomputed JSON".

type Iot struct {
    Id      int             `json:"id"`
    Name    string          `json:"name"`
    Context json.RawMessage `json:"context"` // RawMessage here! (not a string)
}

func main() {
    in := []byte(`{"id":1,"name":"test","context":{"key1":"value1","key2":2}}`)

    var iot Iot
    err := json.Unmarshal(in, &iot)
    if err != nil {
        panic(err)
    }

    // Context is []byte, so you can keep it as string in DB
    fmt.Println("ctx:", string(iot.Context))

    // Marshal back to json (as original)
    out, _ := json.Marshal(&iot)
    fmt.Println(string(out))
}

https://play.golang.org/p/69n0B2PNRv

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java String to Json & vice versa

How to convert String[] to String and vice versa in Android

Convert List<int> to string and vice versa?

How to convert a netty ByteBuf to a String and vice versa

How to convert numpy array to string and vice versa

how to convert joda datetime to String and vice versa

How to convert byte array to string and vice versa?

Python convert set to string and vice versa

Convert an IP string to a number and vice versa

Convert string time to unix time and vice versa

How to convert a string to hex and vice versa in c?

Convert a Guid string into BigInteger and vice versa

Convert []uint32 to []byte and vice versa in golang

byte to string and vice versa

XML To JSON and Vice Versa

Convert ObjectId to String and vice versa inside MongoDB aggregate map

How to convert string variable to int and vice versa in CLASSIC ASP

Convert dataset to xml string with nested repeated xml vice versa

Java: Can I convert List of Object to List of String[] and vice versa?

Cannot convert String to byte array and vice versa in Java

How to convert WCHAR* to string in C++ and vice versa?

How many ways to convert bitmap to string and vice-versa?

Convert hour minute string time to utc and vice versa

Convert a binary string to Hexadecimal and vice-versa in Elixir

How do you convert a byte array to a hexadecimal string, and vice versa?

Convert the string containing array and dict into the list and vice versa in c#

How to convert double byte character/string to single byte and vice versa?

Convert ListView to Line separated String Array and vice versa

How to convert string into numpy array for tensorflow and vice versa