0

I have the following examples.

1

package main

import (
        "encoding/json"
        "fmt"
)

type JSType struct {
        Value string `json:value`
        Empty string `json:empty`
}

func main() {
        str := `{"value": "base64", "empty": "", "params": {}}`
        var decoded JSType
        metaBin := []byte(str)
        json.Unmarshal(metaBin, &decoded)

        fmt.Println(decoded.Empty)
}

It returns an empty string which is expected.

2

package main

import (
        "encoding/json"
        "fmt"
)

type JSType struct {
        Value string `json:value`
        Empty string `json:empty`
}

func main() {
        str := `{"value": "base64", "params": {}}`
        var decoded JSType
        metaBin := []byte(str)
        json.Unmarshal(metaBin, &decoded)

        fmt.Println(decoded.Empty)
}

in the 2nd example I removed the 'empty' key from the json and it still returns an empty string. So my question is how can I determine if the valie is "" or not defined (key is missing).

Community
  • 1
  • 1
Zhivko Angelov
  • 139
  • 2
  • 7
  • 3
    Those aren't valid tags. – JimB Sep 13 '17 at 14:48
  • The the question this one is a "duplicate" of is not really a duplicate, *but* both questions are similar enough that the same answer applies to both. Also, not only is your tag syntax wrong (it should be `json: ",tagname"`), but the tags you used are not valid. – Milo Christiansen Sep 13 '17 at 15:58
  • @MiloChristiansen: "tagname" is ambiguous because there is no "tag name"; the tag _key_ is `json`, and the tag _value_ is the quoted string. The json package uses the first comma separated value as the field name, and the only valid options after that are `omitempty` and `string`, so you're example would also be invalid. – JimB Sep 13 '17 at 18:18
  • Obvious place holder is obvious. At least if you *took the time to RTFM*. – Milo Christiansen Sep 13 '17 at 18:20

0 Answers0