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

unable to process array of struct #107

Open
ArulJeyananth opened this issue Aug 23, 2021 · 5 comments
Open

unable to process array of struct #107

ArulJeyananth opened this issue Aug 23, 2021 · 5 comments

Comments

@ArulJeyananth
Copy link

[
{"foo":"test1"},
{"foo":"test2"}
}

Suppose i have a JSON structure as above. I tried to get the value as
"0.foo" as jsonParsed.Path("0.foo").String()
and
"1.foo" as as jsonParsed.Path("1.foo").String()
It is not giving any value for this. Gives value as {}

@ArulJeyananth
Copy link
Author

Actually I want to process the following struct

[
{"addressLine1":"5500 Grand Lake Dr","city":"San Antonio","state":"TX","zipCode":"78244","formattedAddress":"5500 Grand Lake Dr, San Antonio, TX 78244","assessorID":"05076-103-0500","bedrooms":3,"county":"Bexar","legalDescription":"CB 5076A BLK 3 LOT 50","squareFootage":1878,"subdivision":"CONV A/S CODE","yearBuilt":1973,"bathrooms":2,"lotSize":8843,"propertyType":"Single Family","lastSaleDate":"2017-10-19T00:00:00.000Z","features":{"architectureType":"Contemporary","cooling":true,"coolingType":"Central","exteriorType":"Wood","floorCount":1,"foundationType":"Slab","garage":true,"garageType":"Garage","heating":true,"heatingType":"Forced Air","pool":true,"roofType":"Asphalt","roomCount":5,"unitCount":1},"taxAssessment":{"2018":{"value":126510,"land":18760,"improvements":107750},"2019":{"value":135430,"land":23450,"improvements":111980},"2020":{"value":142610,"land":23450,"improvements":119160}},"propertyTaxes":{"2019":{"total":2997}},"owner":{"names":["MICHEAL ONEALSMITH"],"mailingAddress":{"id":"5731-Pool-Canyon-Cv,-Austin,-TX-78734","addressLine1":"5731 Pool CanyonCv","city":"Austin","state":"TX","zipCode":"78734"}},"id":"5500-Grand-Lake-Dr,-San-Antonio,-TX-78244","longitude":-98.351442,"latitude":29.475962}
]

@mihaitodor
Copy link
Contributor

Hey @ArulJeyananth, thanks for submitting this issue! Your initial JSON sample seems to be invalid JSON, since you're using } instead of ] to close the JSON array. Otherwise, the following code seems to work as expected:

package main

import (
	"github.com/Jeffail/gabs/v2"
)

func main() {
	data := []byte(`[
		{"foo":"test1"},
		{"foo":"test2"}
	]`)
	g, _ := gabs.ParseJSON(data)
	println(g.Path("0.foo").String())
	println(g.Path("1.foo").String())

	data = []byte(`[
		{
			"addressLine1": "5500 Grand Lake Dr",
			"city": "San Antonio",
			"state": "TX",
			"zipCode": "78244",
			"formattedAddress": "5500 Grand Lake Dr, San Antonio, TX 78244",
			"assessorID": "05076-103-0500",
			"bedrooms": 3,
			"county": "Bexar",
			"legalDescription": "CB 5076A BLK 3 LOT 50",
			"squareFootage": 1878,
			"subdivision": "CONV A/S CODE",
			"yearBuilt": 1973,
			"bathrooms": 2,
			"lotSize": 8843,
			"propertyType": "Single Family",
			"lastSaleDate": "2017-10-19T00:00:00.000Z",
			"features": {
				"architectureType": "Contemporary",
				"cooling": true,
				"coolingType": "Central",
				"exteriorType": "Wood",
				"floorCount": 1,
				"foundationType": "Slab",
				"garage": true,
				"garageType": "Garage",
				"heating": true,
				"heatingType": "Forced Air",
				"pool": true,
				"roofType": "Asphalt",
				"roomCount": 5,
				"unitCount": 1
			},
			"taxAssessment": {
				"2018": {
					"value": 126510,
					"land": 18760,
					"improvements": 107750
				},
				"2019": {
					"value": 135430,
					"land": 23450,
					"improvements": 111980
				},
				"2020": {
					"value": 142610,
					"land": 23450,
					"improvements": 119160
				}
			},
			"propertyTaxes": {
				"2019": {
					"total": 2997
				}
			},
			"owner": {
				"names": [
					"MICHEAL ONEALSMITH"
				],
				"mailingAddress": {
					"id": "5731-Pool-Canyon-Cv,-Austin,-TX-78734",
					"addressLine1": "5731 Pool CanyonCv",
					"city": "Austin",
					"state": "TX",
					"zipCode": "78734"
				}
			},
			"id": "5500-Grand-Lake-Dr,-San-Antonio,-TX-78244",
			"longitude": -98.351442,
			"latitude": 29.475962
		}
	]`)
	g, _ = gabs.ParseJSON(data)
	println(g.Path("0.id").String())
}

It prints:

"test1"
"test2"
"5500-Grand-Lake-Dr,-San-Antonio,-TX-78244"

@henninggaus
Copy link

Hi there,

Thank you for the answer and the great package. I wanted to ask briefly whether there might be an issue with the function call.
I tried to run the sample code from the ReadMe file. Unfortunately the result is an empty array.
Would you like to watch that again, please?

jsonParsed, err := gabs.ParseJSON([]byte({"array":[{"value":1},{"value":2},{"value":3}]})) if err != nil { panic(err) } fmt.Println(jsonParsed.Path("array.1.value").String())

Result is {}

@mihaitodor
Copy link
Contributor

@henninggaus It looks like you imported github.com/Jeffail/gabs instead of github.com/Jeffail/gabs/v2. Please change the import and try again.

@aki-s
Copy link

aki-s commented Jun 28, 2022

Hi @henninggaus .

I encountered the same issue, and it is resolved by the suggestion of #107 (comment) .
Please close this issue.

 go get github.com/Jeffail/gabs/v2

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

4 participants