Skip to content

Commit

Permalink
Arrays, yo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffail committed Aug 21, 2014
1 parent d5456b2 commit 05e82f5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ third

Children() will return all children of an array in order. This also works on objects, however, the children will be returned in a random order.

###Searching through arrays

If your JSON structure contains arrays you can still search the fields of the objects within the array, this returns a JSON array containing the results for each element.

```go
...

jsonParsed, _ := gabs.ParseJSON([]byte(`{"array":[ {"value":1}, {"value":2}, {"value":3} ]}`))
fmt.Println(jsonParsed.Path("array.value").String())

...
```

Will print:

```
[1,2,3]
```

###Generating JSON

```go
Expand Down Expand Up @@ -101,8 +120,6 @@ Will print:
```go
...

var err error

jsonObj, _ := gabs.Consume(map[string]interface{}{})

jsonObj.Array("array")
Expand Down
17 changes: 16 additions & 1 deletion gabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func (g *Container) Path(path string) *Container {

/*
Search - Attempt to find and return an object within the JSON structure by specifying the hierarchy
of field names to locate the target.
of field names to locate the target. If the search encounters an array and has not reached the end
target then it will iterate each object of the array for the target and return all of the results in
a JSON array.
*/
func (g *Container) Search(hierarchy ...string) *Container {
var object interface{}
Expand All @@ -69,6 +71,19 @@ func (g *Container) Search(hierarchy ...string) *Container {
for target := 0; target < len(hierarchy); target++ {
if mmap, ok := object.(map[string]interface{}); ok {
object = mmap[hierarchy[target]]
} else if marray, ok := object.([]interface{}); ok {
tmpArray := []interface{}{}
for _, val := range marray {
tmpGabs := &Container{val}
res := tmpGabs.Search(hierarchy[target:]...).Data()
if res != nil {
tmpArray = append(tmpArray, res)
}
}
if len(tmpArray) == 0 {
return &Container{nil}
}
return &Container{tmpArray}
} else {
return &Container{nil}
}
Expand Down
62 changes: 62 additions & 0 deletions gabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,68 @@ func TestBasic(t *testing.T) {
}
}

func TestFindArray(t *testing.T) {
sample := []byte(`{"test":{"array":[{"value":1}, {"value":2}, {"value":3}]}}`)

val, err := ParseJSON(sample)
if err != nil {
t.Errorf("Failed to parse: %v", err)
return
}

target := val.Path("test.array.value")
expected := "[1,2,3]"
result := target.String()

if expected != result {
t.Errorf("Expected %v, received %v", expected, result)
}
}

func TestFindArray2(t *testing.T) {
sample := []byte(`{
"test":{
"array":[
{
"values":[
{"more":1},
{"more":2},
{"more":3}
]
},
{
"values":[
{"more":4},
{"more":5},
{"more":6}
]
},
{
"values":[
{"more":7},
{"more":8},
{"more":9}
]
}
]
}
}`)

val, err := ParseJSON(sample)
if err != nil {
t.Errorf("Failed to parse: %v", err)
return
}

target := val.Path("test.array.values.more")
expected := "[[1,2,3],[4,5,6],[7,8,9]]"
result := target.String()

if expected != result {
t.Errorf("Expected %v, received %v", expected, result)
}
}

func TestExamples(t *testing.T) {
jsonParsed, _ := ParseJSON([]byte(`{
"outter":{
Expand Down

0 comments on commit 05e82f5

Please sign in to comment.