gojson icon indicating copy to clipboard operation
gojson copied to clipboard

-subStruct does not print nested fields in array items

Open satta opened this issue 6 years ago • 1 comments

I'm trying to use gojson to generate structs for an array of items, which may or may not contain (disjunct) keys with different sub-objects. Example (let's call this test.json):

[{"foo": {"bar": 2, "baz": 4, "xx": {"foo": "bar"}}}, 
 {"foo": {"bar": 2, "yy": {"bar": "baz"}}}]

In my original input these are one object per line, but as gojson can not handle this I have converted the input to an array instead. While this format can be well processed using the default settings:

$ cat test.json | gojson
package main

type Foo []struct {
	Foo struct {
		Bar int64 `json:"bar"`
		Baz int64 `json:"baz"`
		Xx  struct {
			Foo string `json:"foo"`
		} `json:"xx"`
		Yy struct {
			Bar string `json:"bar"`
		} `json:"yy"`
	} `json:"foo"`
}

there's a problem when trying to use -subStruct:

$ cat test.json | gojson -subStruct
package main

type Foo []struct {
	Foo Foo_sub3 `json:"foo"`
}

The substructs just are not printed. I would have expected an output similar to what I would get from a non-array input:

$ cat test2.json
{"foo": {"bar": 2, "baz": 4, "xx": {"foo": "bar"}}}
$ cat test2.json | gojson -subStruct
package main

type Foo struct {
	Foo Foo_sub2 `json:"foo"`
}

type Foo_sub2 struct {
	Bar int64    `json:"bar"`
	Baz int64    `json:"baz"`
	Xx  Foo_sub1 `json:"xx"`
}

type Foo_sub1 struct {
	Foo string `json:"foo"`
}

where the referenced substructs are also printed in the actual output.

satta avatar Jul 02 '18 15:07 satta

Just ran into this and can confirm that this is still happening.

carbocation avatar Mar 17 '20 15:03 carbocation