thedevsaddam/gojsonq

Cannot find all elements in array

030 opened this issue · 2 comments

030 commented

sample

{
  "items" : [ {
    "downloadUrl" : "http://localhost:8081/repository/maven-releases/file2/file2/1.0.0/file2-1.0.0.jar",
    "path" : "file2/file2/1.0.0/file2-1.0.0.jar",
    "id" : "bWF2ZW4tcmVsZWFzZXM6ZDQ4MTE3NTQxZGNiODllYzQzMTI5MDdhYzdiOWUyMDM",
    "repository" : "maven-releases",
    "format" : "maven2",
    "checksum" : {
      "sha1" : "d9f877a857a7e9928eac04d09a59f25967624155",
      "sha256" : "0a8cac771ca188eacc57e2c96c31f5611925c5ecedccb16b8c236d6c0d325112",
      "sha512" : "e4694dce7937894bba2b5b4ad33dfd1893d8494fcd4eaac7ca3392c49ddba2ef0497b2a0307c81580611a785a0244a6003b4bae0d978c8d1544f4f691e6c7130",
      "md5" : "ad60407c083b4ecc372614b8fcd9f305"
    }
  }, {
    "downloadUrl" : "http://localhost:8081/repository/maven-releases/file47/file47/1.0.0/file47-1.0.0.pom.sha1",
    "path" : "file47/file47/1.0.0/file47-1.0.0.pom.sha1",
    "id" : "bWF2ZW4tcmVsZWFzZXM6ODUxMzU2NTJhOTc4YmU5YTQyNGYxMzQ3NDYwYmM0ODI",
    "repository" : "maven-releases",
    "format" : "maven2",
    "checksum" : {
      "sha1" : "0126c00d391aebeddcd55ef8d1bd99c26a0fdb57",
      "md5" : "d579a682f677fa6b22e054c8a69494d2"
    }
  } ],
  "continuationToken" : "35303a6235633862633138616131326331613030356565393061336664653966613733"
} 

Attempts

gojsonq.New().JSONString(json).From("items").Find("downloadUrl")

and

name := gojsonq.New().JSONString(json).Find("items.downloadUrl")

return

nil

while jq .items[].downloadUrl return all downloadUrl

The following jq statement returns all downloadUrls:

curl -X GET http://localhost:8081/service/rest/v1/search/assets?repository=maven-releases | jq .items[].downloadUrl

returns:

"http://localhost:8081/repository/maven-releases/file2/file2/1.0.0/file2-1.0.0.jar"
"http://localhost:8081/repository/maven-releases/file2/file2/1.0.0/file2-1.0.0.jar.md5"
"http://localhost:8081/repository/maven-releases/file2/file2/1.0.0/file2-1.0.0.jar.sha1"
"http://localhost:8081/repository/maven-releases/file2/file2/1.0.0/file2-1.0.0.pom"
"http://localhost:8081/repository/maven-releases/file2/file2/1.0.0/file2-1.0.0.pom.md5"
"http://localhost:8081/repository/maven-releases/file2/file2/1.0.0/file2-1.0.0.pom.sha1"
"http://localhost:8081/repository/maven-releases/file10/file10/1.0.0/file10-1.0.0.jar"
"http://localhost:8081/repository/maven-releases/file10/file10/1.0.0/file10-1.0.0.jar.md5"
"http://localhost:8081/repository/maven-releases/file10/file10/1.0.0/file10-1.0.0.jar.sha1"
"http://localhost:8081/repository/maven-releases/file10/file10/1.0.0/file10-1.0.0.pom"
"http://localhost:8081/repository/maven-releases/file10/file10/1.0.0/file10-1.0.0.pom.md5"
"http://localhost:8081/repository/maven-releases/file10/file10/1.0.0/file10-1.0.0.pom.sha1"
"http://localhost:8081/repository/maven-releases/file11/file11/1.0.0/file11-1.0.0.jar"
"http://localhost:8081/repository/maven-releases/file11/file11/1.0.0/file11-1.0.0.jar.md5"
"http://localhost:8081/repository/maven-releases/file11/file11/1.0.0/file11-1.0.0.jar.sha1"
"http://localhost:8081/repository/maven-releases/file11/file11/1.0.0/file11-1.0.0.pom"
"http://localhost:8081/repository/maven-releases/file11/file11/1.0.0/file11-1.0.0.pom.md5"
"http://localhost:8081/repository/maven-releases/file11/file11/1.0.0/file11-1.0.0.pom.sha1"
"http://localhost:8081/repository/maven-releases/file7/file7/1.0.0/file7-1.0.0.jar"
"http://localhost:8081/repository/maven-releases/file7/file7/1.0.0/file7-1.0.0.jar.md5"
"http://localhost:8081/repository/maven-releases/file7/file7/1.0.0/file7-1.0.0.jar.sha1"
"http://localhost:8081/repository/maven-releases/file7/file7/1.0.0/file7-1.0.0.pom"
"http://localhost:8081/repository/maven-releases/file7/file7/1.0.0/file7-1.0.0.pom.md5"
"http://localhost:8081/repository/maven-releases/file7/file7/1.0.0/file7-1.0.0.pom.sha1"
"http://localhost:8081/repository/maven-releases/file8/file8/1.0.0/file8-1.0.0.jar"
"http://localhost:8081/repository/maven-releases/file8/file8/1.0.0/file8-1.0.0.jar.md5"
"http://localhost:8081/repository/maven-releases/file8/file8/1.0.0/file8-1.0.0.jar.sha1"
"http://localhost:8081/repository/maven-releases/file8/file8/1.0.0/file8-1.0.0.pom"
"http://localhost:8081/repository/maven-releases/file8/file8/1.0.0/file8-1.0.0.pom.md5"
"http://localhost:8081/repository/maven-releases/file8/file8/1.0.0/file8-1.0.0.pom.sha1"
"http://localhost:8081/repository/maven-releases/file4/file4/1.0.0/file4-1.0.0.jar"
"http://localhost:8081/repository/maven-releases/file4/file4/1.0.0/file4-1.0.0.jar.md5"

You can use method Pluck/1

package main

import (
	"fmt"
	"reflect"

	"github.com/thedevsaddam/gojsonq"
)

const json = `{
  "items" : [ {
    "downloadUrl" : "http://localhost:8081/repository/maven-releases/file2/file2/1.0.0/file2-1.0.0.jar",
    "path" : "file2/file2/1.0.0/file2-1.0.0.jar",
    "id" : "bWF2ZW4tcmVsZWFzZXM6ZDQ4MTE3NTQxZGNiODllYzQzMTI5MDdhYzdiOWUyMDM",
    "repository" : "maven-releases",
    "format" : "maven2",
    "checksum" : {
      "sha1" : "d9f877a857a7e9928eac04d09a59f25967624155",
      "sha256" : "0a8cac771ca188eacc57e2c96c31f5611925c5ecedccb16b8c236d6c0d325112",
      "sha512" : "e4694dce7937894bba2b5b4ad33dfd1893d8494fcd4eaac7ca3392c49ddba2ef0497b2a0307c81580611a785a0244a6003b4bae0d978c8d1544f4f691e6c7130",
      "md5" : "ad60407c083b4ecc372614b8fcd9f305"
    }
  }, {
    "downloadUrl" : "http://localhost:8081/repository/maven-releases/file47/file47/1.0.0/file47-1.0.0.pom.sha1",
    "path" : "file47/file47/1.0.0/file47-1.0.0.pom.sha1",
    "id" : "bWF2ZW4tcmVsZWFzZXM6ODUxMzU2NTJhOTc4YmU5YTQyNGYxMzQ3NDYwYmM0ODI",
    "repository" : "maven-releases",
    "format" : "maven2",
    "checksum" : {
      "sha1" : "0126c00d391aebeddcd55ef8d1bd99c26a0fdb57",
      "md5" : "d579a682f677fa6b22e054c8a69494d2"
    }
  } ],
  "continuationToken" : "35303a6235633862633138616131326331613030356565393061336664653966613733"
} `

func Print(t interface{}) {
	switch reflect.TypeOf(t).Kind() {
	case reflect.Slice:
		s := reflect.ValueOf(t)

		for i := 0; i < s.Len(); i++ {
			fmt.Println(s.Index(i))
		}
	}
}

func main() {
	jq := gojsonq.New().JSONString(json).From("items")
	Print(jq.Pluck("downloadUrl"))
}

@Kvaz1r you are correct! @030 you can use pluck,

package main

import (
	"fmt"

	"github.com/thedevsaddam/gojsonq"
)

const data = `{
  "items" : [ {
    "downloadUrl" : "http://localhost:8081/repository/maven-releases/file2/file2/1.0.0/file2-1.0.0.jar",
    "path" : "file2/file2/1.0.0/file2-1.0.0.jar",
    "id" : "bWF2ZW4tcmVsZWFzZXM6ZDQ4MTE3NTQxZGNiODllYzQzMTI5MDdhYzdiOWUyMDM",
    "repository" : "maven-releases",
    "format" : "maven2",
    "checksum" : {
      "sha1" : "d9f877a857a7e9928eac04d09a59f25967624155",
      "sha256" : "0a8cac771ca188eacc57e2c96c31f5611925c5ecedccb16b8c236d6c0d325112",
      "sha512" : "e4694dce7937894bba2b5b4ad33dfd1893d8494fcd4eaac7ca3392c49ddba2ef0497b2a0307c81580611a785a0244a6003b4bae0d978c8d1544f4f691e6c7130",
      "md5" : "ad60407c083b4ecc372614b8fcd9f305"
    }
  }, {
    "downloadUrl" : "http://localhost:8081/repository/maven-releases/file47/file47/1.0.0/file47-1.0.0.pom.sha1",
    "path" : "file47/file47/1.0.0/file47-1.0.0.pom.sha1",
    "id" : "bWF2ZW4tcmVsZWFzZXM6ODUxMzU2NTJhOTc4YmU5YTQyNGYxMzQ3NDYwYmM0ODI",
    "repository" : "maven-releases",
    "format" : "maven2",
    "checksum" : {
      "sha1" : "0126c00d391aebeddcd55ef8d1bd99c26a0fdb57",
      "md5" : "d579a682f677fa6b22e054c8a69494d2"
    }
  } ],
  "continuationToken" : "35303a6235633862633138616131326331613030356565393061336664653966613733"
}
`

func main() {
	jq := gojsonq.New().JSONString(data)
	res := jq.From("items").Pluck("downloadUrl")
	fmt.Printf("Pluck: %v\n", res)
}

See more about Pluck