Jeffail/gabs

unable to process array of struct

Opened this issue · 5 comments

[
{"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 {}

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}
]

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"

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 {}

@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 commented

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