JSONParser chokes


I’m having a problem with JSONParser. I read a file from flash which has been previously encoded with JSONencoder. The structure is a simple array of tables which seems to be properly encoded in the flash file.

[{“ssid”:“Guest”,“pw”:"",“preferred”:true},{“ssid”:“ASUS_2G”,“pw”:“141366302*jdk”,“preferred”:false},{“ssid”:“ArchSolar~Buxton”,“pw”:“archsolar”,“preferred”:false},{“ssid”:“LRFF Wi-Fi 2”,“pw”:"",“preferred”:false},{“ssid”:“Olivia’s Garden”,“pw”:“mainelyhydroponics”,“preferred”:false},{“ssid”:“Olivia’s Garden_EXT”,“pw”:“mainelyhydroponics”,“preferred”:false},{“ssid”:“Pineland_Internal”,“pw”:"?",“preferred”:false},{“ssid”:“PF-Creamery”,“pw”:"?",“preferred”:false},{“ssid”:“Pineland-Market”,“pw”:"?",“preferred”:false},{“ssid”:“Mills2017”,“pw”:"?",“preferred”:false}]

But feeding this same string (as J_str) into JSONParser

nv_wifi <- sffs.open(“wifi_list.nv”,“r”)
J_str = nv_wifi.read()
test_array = JSONParser.parse(J_str)

kicks out the error:
2018-03-06 10:49:37 -05:00|[Device]|ERROR: the index ‘slice’ does not exist
2018-03-06 10:49:37 -05:00|[Device]|ERROR: in parse …imp#jsonparser.class.nut#1.0.0:285

Any suggestions where to look? It appears from the code example on the documentation page that the ’ " ’ need to be escaped. But if that were the case, shouldn’t JSONEncode do that? I don’t believe an escaped quote is standard JSON.

Maybe I’m on the wrong track?


nv_wifi.read() returns a blob, not a string. JSONParser expects a string, but doesn’t test for it (but probably should). If you try nv_wfifi.read().tostring(), you should see a better result.

Even after you do this, you should also wrap the call to JSONParser.parse in try/catch. The parser will most likely throw an exception if it has any trouble decoding the string.


Thanks so much! That did the trick.

I hadn’t considered that the file read function would return a blob, although that makes sense. I was looking in all the wrong places!