From 11cb24d8421de3e1bb5c5efb066a03037150568d Mon Sep 17 00:00:00 2001 From: tidwall Date: Wed, 1 Apr 2020 05:03:04 -0700 Subject: [PATCH] Fix trailing curly bracket close #36 --- sjson.go | 8 +++++++- sjson_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/sjson.go b/sjson.go index df83a96..3c1ae11 100644 --- a/sjson.go +++ b/sjson.go @@ -341,7 +341,13 @@ func appendRawPaths(buf []byte, jstr string, paths []pathResult, raw string, default: return nil, &errorType{"json must be an object or array"} case '{': - buf = append(buf, jsres.Raw[:len(jsres.Raw)-1]...) + end := len(jsres.Raw) - 1 + for ; end > 0; end-- { + if jsres.Raw[end] == '}' { + break + } + } + buf = append(buf, jsres.Raw[:end]...) if comma { buf = append(buf, ',') } diff --git a/sjson_test.go b/sjson_test.go index a680f97..318e565 100644 --- a/sjson_test.go +++ b/sjson_test.go @@ -310,3 +310,27 @@ func TestDeleteDotKeyIssue19(t *testing.T) { t.Fatalf("expected '%v', got '%v'", `{"data":{"key1":"value1"}}`, json) } } + +func TestIssue36(t *testing.T) { + var json = ` + { + "size": 1000 + } +` + var raw = ` + { + "sample": "hello" + } +` + _ = raw + if true { + json, _ = SetRaw(json, "aggs", raw) + } + if !gjson.Valid(json) { + t.Fatal("invalid json") + } + res := gjson.Get(json, "aggs.sample").String() + if res != "hello" { + t.Fatal("unexpected result") + } +}