Jeffail/gabs

null value being added to container

Closed this issue · 2 comments

Hey there, love being able to use this in my work. :) I'm encountering some behavior I didn't expect, sample code is below.

package main

import (
	"fmt"

	"github.com/Jeffail/gabs"
)

func verifyItem(item interface{}, jsonObj *gabs.Container) bool {
	_, isString := item.(string)
	if isString {
		return true
	}
	jsonObj.ArrayAppend(item, "notString")
	return false
}

func main() {
	jsonParsed, _ := gabs.ParseJSON([]byte(`{"outer": [1,2,3,"four","five","six"]}`))
	// jsonParsed, _ := gabs.ParseJSON([]byte(`{"outer": ["four","five","six"]}`))
	children, _ := jsonParsed.Search("outer").Children()
	jsonObj := gabs.New()
	for _, child := range children {
		verifyItem(child.Data(), jsonObj)
	}
	fmt.Println(jsonObj.String())
}

I'd expect that the result would be {"notString":[1,2,3]}, but for some reason, null is being added in: {"notString":[null,1,2,3]}. In this example, I'm hoping to only create the notString key if any interfaces are identified as not being a string. That works (toggling jsonParsed gives {} when only passing strings), but whenever a non-string is detected, null gets added. Any ideas?

Looks like it's caused by this line

Hey @1I1III1liL1, thanks for raising this. I've added a check so that only non-nil pre-existing values are inserted to new arrays.

Commit: 7be49c3
Release: https://github.com/Jeffail/gabs/releases/tag/v1.1.1