Empty arrays from JSON.parse() contain a hidden element

Jay Oster jay at pubnub.com
Wed Mar 30 23:34:43 UTC 2016


Just a bug report, here. I'll let the jjs session speak for itself.

$ jjs -version
nashorn 1.8.0_77
jjs> var a = JSON.parse("[]")
jjs> a.length
0
jjs> JSON.stringify(a)
[]
jjs> a.push("foo")
1
jjs> a.length
2
jjs> JSON.stringify(a)
[0,"foo"]


For the moment, I have a workaround that replaces JSON.parse() with a
function that recursively replaces empty arrays with array literals. Since
they don't exhibit the same bad behavior:

jjs> var b = []
jjs> b.length
0
jjs> JSON.stringify(b)
[]
jjs> b.push("foo")
1
jjs> b.length
1
jjs> JSON.stringify(b)
["foo"]


More information about the nashorn-dev mailing list