JSONItem.Lookup causing KeyNotFoundException

I have a problem in one of our projects where a class with JSONItem as super class throws a KeyNotFoundException on Me.Lookup(key, Nil).

I copied the same class into a demo project and removed everything that is not relevant, but I don’t get the error recreated.

What could be the reason for this?

https://drive.google.com/file/d/1fFZ2rbo8agVs4Yxtp7eE-yzdHq27aXYT/view?usp=drive_link

I was able to replicate the issue

https://tracker.xojo.com/xojoinc/xojo/-/issues/73787

Var json As New JSONItem

Var result As Integer = json.Lookup("test", 0) // WORKS

json.RemoveAll

Var result2 As Integer = json.Lookup("test", 0) // THROWS EXCEPTION
3 Likes

@StefanA
Have you tried that same thing with a Dictionary? JSONItem was changed to use a Dictionary and GenerateJSON a few years ago for the speed so I would expect it to have the same issue.

Dictionary does not have this issue.
This code works as expected:

Var d As New Dictionary

Var i As Integer = d.Lookup("test", 0)

d.RemoveAll

Var i2 As Integer = d.Lookup("test", 0)

You should put that in your case

That said, you could just reinitialize the JSONItem with new to get the same effect.

I remember having this same issue when i used removeAll (jsonitem) and then got the exception…