Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple-value jsonParsed.Search("employees", "address").ChildrenMap() in single-value context #102

Open
jinjie-unity opened this issue May 31, 2021 · 1 comment

Comments

@jinjie-unity
Copy link

Such an error was found when executing the program.

./hello.go:47:79: multiple-value jsonParsed.Search("employees", "address").ChildrenMap() in single-value context

And the result is empty. fmt.Println("ID of first employee:\t", jsonParsed.Path("employees.employee.0.id").String())

Here is my code

package main

import (
	"fmt"

	"github.com/Jeffail/gabs"
)

func main() {
	data := []byte(`{
		"employees":{
		   "protected":false,
		   "address":{
			  "street":"22 Saint-Lazare",
			  "postalCode":"75003",
			  "city":"Paris",
			  "countryCode":"FRA",
			  "country":"France"
		   },
		   "employee":[
			  {
				 "id":1,
				 "first_name":"Jeanette",
				 "last_name":"Penddreth"
			  },
			  {
				 "id":2,
				 "firstName":"Giavani",
				 "lastName":"Frediani"
			  }
		   ]
		}
	 }`)

	jsonParsed, err := gabs.ParseJSON(data)
	if err != nil {
		panic(err)
	}

	// Search JSON
	fmt.Println("Get value of Protected:\t", jsonParsed.Path("employees.protected").Data())
	fmt.Println("Get value of Country:\t", jsonParsed.Search("employees", "address", "country").Data())
	fmt.Println("ID of first employee:\t", jsonParsed.Search("employees", "employee", "id").String())
	fmt.Println("Check Country Exists:\t", jsonParsed.Exists("employees", "address", "countryCode"))

	// Iterating address objects
	for key, child := range jsonParsed.Search("employees", "address").ChildrenMap() {
		fmt.Printf("Key=>%v, Value=>%v\n", key, child.Data().(string))
	}

	// // Iterating employee array
	// for _, child := range jsonParsed.Search("employees", "employee").Children() {
	// 	fmt.Println(child.Data())
	// }

	// // Use index in your search
	// for _, child := range jsonParsed.Search("employees", "employee", "0").Children() {
	// 	fmt.Println(child.Data())
	// }
}
@Sychorius
Copy link

I found the same issue and fixed by using github.com/Jeffail/gabs/v2 instead of github.com/Jeffail/gabs
You may also need to check your go.mod

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants