diff --git a/pkg/yqlib/candidiate_node_json.go b/pkg/yqlib/candidiate_node_json.go index 728e5afb..fe7d344e 100644 --- a/pkg/yqlib/candidiate_node_json.go +++ b/pkg/yqlib/candidiate_node_json.go @@ -162,8 +162,13 @@ func (o *CandidateNode) MarshalJSON() ([]byte, error) { buf.WriteByte('}') return buf.Bytes(), nil case SequenceNode: - log.Debugf("MarshalJSON SequenceNode") - err := enc.Encode(o.Content) + log.Debugf("MarshalJSON SequenceNode, %v, len: %v", o.Content, len(o.Content)) + var err error + if len(o.Content) == 0 { + buf.WriteString("[]") + } else { + err = enc.Encode(o.Content) + } return buf.Bytes(), err default: err := enc.Encode(nil) diff --git a/pkg/yqlib/json_test.go b/pkg/yqlib/json_test.go index ab0c7225..b36d1e19 100644 --- a/pkg/yqlib/json_test.go +++ b/pkg/yqlib/json_test.go @@ -80,6 +80,27 @@ const roundTripMultiLineJson = `{ ` var jsonScenarios = []formatScenario{ + { + description: "array empty", + skipDoc: true, + input: "[]", + scenarioType: "roundtrip-ndjson", + expected: "[]\n", + }, + { + description: "array has scalar", + skipDoc: true, + input: "[3]", + scenarioType: "roundtrip-ndjson", + expected: "[3]\n", + }, + { + description: "array has object", + skipDoc: true, + input: `[{"x": 3}]`, + scenarioType: "roundtrip-ndjson", + expected: "[{\"x\":3}]\n", + }, { description: "array null", skipDoc: true,