ignore json tag for field list in a struct field

87 views
Skip to first unread message

amarjeeta...@gmail.com

unread,
Jul 3, 2020, 10:11:38 AM7/3/20
to golang-nuts
If there is a json tag with multiple fields in a single line, is there any json parser which will consider the json field name?
Like in below, "encoding/json" ignores the fields to unmarshal if there are multiple fields in a single line with a json tag.
EX: below code will print nothing.

type a_1_struct_name struct {
   
Field1, Field2 string `json:"field_1"`
}

func main() {
   
a := a_1_struct_name{}
   
j := `{"field_1": "field_1_val"}`
   if err := json.Unmarshal([]byte(j), &a); err != nil {
     
log.Fatal(err)
   
}
   
fmt.Println(a)
}

But if we remove the json tag `json:"field_1"` and the change the value of json(j) accordingly, it prints {field_1_val }

I am writing a program to get the json equivalent tag of struct fields using reflect. 
My question is, is it safe to ignore the fieldList(comma separated struct fields in a single line) if there is a json tag with it?

Lutz Horn

unread,
Jul 3, 2020, 2:55:30 PM7/3/20
to golang-nuts
This code throws an error:

./prog.go:10:10: struct field Field2 repeats json tag "field_1" also at prog.go:10

see https://play.golang.org/p/sFZ-6GWUO0J

Why not use this:

type a_1_struct_name struct {
Field1 string `json:"field_1"`
Field2 string `json:"field_2"`
}

________________________________________
Von: golan...@googlegroups.com <golan...@googlegroups.com> im Auftrag von amarjeeta...@gmail.com <amarjeeta...@gmail.com>
Gesendet: Freitag, 3. Juli 2020 16:11
An: golang-nuts
Betreff: [go-nuts] ignore json tag for field list in a struct field
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com<mailto:golang-nuts...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1932cc53-b7b6-44d2-b855-1639e4c32ac0o%40googlegroups.com<https://groups.google.com/d/msgid/golang-nuts/1932cc53-b7b6-44d2-b855-1639e4c32ac0o%40googlegroups.com?utm_medium=email&utm_source=footer>.

amarjeeta...@gmail.com

unread,
Jul 3, 2020, 9:08:08 PM7/3/20
to golang-nuts

On Saturday, July 4, 2020 at 12:25:30 AM UTC+5:30, Lutz Horn wrote:
This code throws an error:

./prog.go:10:10: struct field Field2 repeats json tag "field_1" also at prog.go:10

see https://play.golang.org/p/sFZ-6GWUO0J

ohh 
Yeah, its failing on go 14, but it runs in my local on go 13. 
So probably I shouldn't be worried about dropping the fields from output list.
 
Why not use this:

type a_1_struct_name struct {
        Field1 string `json:"field_1"`
        Field2 string `json:"field_2"`
}

Yeah this would be the proper way to write json tags. But I am writing a parser kind of tool that will try to generate the filed tags of struct. So I never know how somebody else is going to write their program that I am going to parse. 

________________________________________
Von: golan...@googlegroups.com <golan...@googlegroups.com> im Auftrag von amarjeeta...@gmail.com <amarjeeta...@gmail.com>
Gesendet: Freitag, 3. Juli 2020 16:11
An: golang-nuts
Betreff: [go-nuts] ignore json tag for field list in a struct field

If there is a json tag with multiple fields in a single line, is there any json parser which will consider the json field name?
Like in below, "encoding/json" ignores the fields to unmarshal if there are multiple fields in a single line with a json tag.
EX: below code will print nothing.


type a_1_struct_name struct {
   Field1, Field2 string `json:"field_1"`
}

func main() {
   a := a_1_struct_name{}
   j := `{"field_1": "field_1_val"}`
   if err := json.Unmarshal([]byte(j), &a); err != nil {
      log.Fatal(err)
   }
   fmt.Println(a)
}

But if we remove the json tag `json:"field_1"` and the change the value of json(j) accordingly, it prints {field_1_val }

I am writing a program to get the json equivalent tag of struct fields using reflect.
My question is, is it safe to ignore the fieldList(comma separated struct fields in a single line) if there is a json tag with it?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com<mailto:golang-nuts+unsubscribe@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages