diff --git a/gjson.go b/gjson.go index 6c5ab40..0fd00cf 100644 --- a/gjson.go +++ b/gjson.go @@ -1002,12 +1002,10 @@ func parseObject(c *parseContext, i int, path string) (int, bool) { if ok { res := res.Get(rp.alogkey) if res.Exists() { - if k > 0 && len(res.String()) > 0 { + if k > 0 && len(res.String()) > 0 && len(jsons) > 0 { jsons = append(jsons, ',') } - if res.Type == JSON { - jsons = append(jsons, []byte(res.Raw)...) - } else if res.Type == String { + if res.Type == JSON || res.Type == String { jsons = append(jsons, []byte(res.Raw)...) } else if res.Type == Number { jsons = append(jsons, []byte(res.String())...) @@ -1021,9 +1019,6 @@ func parseObject(c *parseContext, i int, path string) (int, bool) { c.value.Raw = stringArray(jsons) return i + 1, true } - //if rp.alogok { - // break - //} c.value.Raw = "" c.value.Type = Number c.value.Num = float64(h - 1) @@ -1084,9 +1079,6 @@ func parseObject(c *parseContext, i int, path string) (int, bool) { } else { i, val = parseSquash(c.json, i) if hit { - //if rp.alogok { - // break - //} if len(c.value.Raw) > 1 { c.value.Raw = c.value.Raw + "," + val } else { diff --git a/gjson_test.go b/gjson_test.go index 6333be7..6faff02 100644 --- a/gjson_test.go +++ b/gjson_test.go @@ -813,9 +813,9 @@ func TestIssue20(t *testing.T) { } func TestIssue21(t *testing.T) { - json := `{ "Level1Field1":3, - "Level1Field4":4, - "Level1Field2":{ "Level2Field1":[ "value1", "value2" ], + json := `{ "Level1Field1":3, + "Level1Field4":4, + "Level1Field2":{ "Level2Field1":[ "value1", "value2" ], "Level2Field2":{ "Level3Field1":[ { "key1":"value1" } ] } } }` paths := []string{"Level1Field1", "Level1Field2.Level2Field1", "Level1Field2.Level2Field2.Level3Field1", "Level1Field4"} expected := []string{"3", `[ "value1", "value2" ]`, `[ { "key1":"value1" } ]`, "4"} @@ -914,7 +914,7 @@ var complicatedJSON = ` "nestedTagged": { "Green": "Green", "Map": { - "this": "that", + "this": "that", "and": "the other thing" }, "Ints": { @@ -1315,6 +1315,90 @@ func TestObjectGrouping(t *testing.T) { } } +func TestMultiObjectsExtraction(t *testing.T) { + var res Result + + carsJson := ` +{ + "paths": { + "/cars": { + "get": { + "description": "Get cars" + }, + "post": { + "description": "Create a car" + } + }, + "/owners": { + "get": { + "description": "Get owners" + }, + "post": { + "desc": "Create a owner" + } + }, + "/models": { + "get": { + "desc": "Get a model", + "param": [ + { + "name": "per_page", + "default": 10 + }, + { + "name": "page", + "default": 1 + }, + { + "name": "application", + "type": "string" + } + ] + }, + "post": { + "description": "Create a model" + } + } + } +} +` + res = Get(carsJson, "paths.*.get.description") + if res.String() != `["Get cars","Get owners"]` { + t.Fatalf("expected '%v', got '%v'", `["Get cars","Get owners"]`, res.String()) + } + + res = Get(carsJson, "paths./mod*.post.description") + if res.String() != `["Create a model"]` { + t.Fatalf("expected '%v', got '%v'", `["tom","janet"]`, res.String()) + } + + res = Get(carsJson, "paths./md*.post.description") + if res.String() != `` { + t.Fatalf("expected '%v', got '%v'", ``, res.String()) + } + + res = Get(carsJson, "paths.*.*.desc") + if res.String() != `["Create a owner"],["Get a model"]` { + t.Fatalf("expected '%v', got '%v'", `["Create a owner"],["Get a model"]`, res.String()) + } + + res = Get(carsJson, "paths.*.get.param.#[name=page].default") + if res.String() != `[1]` { + t.Fatalf("expected '%v', got '%v'", `[1]`, res.String()) + } + + res = Get(carsJson, "paths.*.get.param.#[name%\"pag*\"].default") + if res.String() != `[1]` { + t.Fatalf("expected '%v', got '%v'", `[1]`, res.String()) + } + + res = Get(carsJson, "paths.*.get.param.#[name=pages].default") + if res.String() != `` { + t.Fatalf("expected '%v', got '%v'", ``, res.String()) + } + +} + func TestJSONLines(t *testing.T) { json := ` true