JSON.parse(“1”) works. JSON.parse(“k1”) throws an exception

(Everyday Code – instead of keeping our knowledge in a README.md let’s share it with the internet)

TL; DR; – JSON.parse(“1”) works. JSON.parse(“k1”) throws an exception

This is a valid JSON


This is NOT a valid JSON


The code

$ irb
2.6.5 :001 > require 'json'
 => true 
2.6.5 :002 > JSON.parse("1")
 => 1 
2.6.5 :003 > JSON.parse("k1")
Traceback (most recent call last):
        6: from /home/kireto/.rvm/rubies/ruby-2.6.5/bin/irb:23:in `<main>'
        5: from /home/kireto/.rvm/rubies/ruby-2.6.5/bin/irb:23:in `load'
        4: from /home/kireto/.rvm/rubies/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        3: from (irb):3
        2: from /home/kireto/.rvm/gems/ruby-2.6.5/gems/json-2.5.1/lib/json/common.rb:216:in `parse'
        1: from /home/kireto/.rvm/gems/ruby-2.6.5/gems/json-2.5.1/lib/json/common.rb:216:in `parse'
JSON::ParserError (809: unexpected token at 'k1')
2.6.5 :004 > JSON.parse("\"1\"")

It is always mistakes as simple as that, that are causing the bugs.


I am sharing the details mostly for our team.

When users use the Instructions Steps framework on the BuildIn3D and FLLCasts platforms we have a communication between the client and the server. The client sends information as JSON, the server parses it. For a few days some of the communication was missed. Not everything was recorded on the server. This was not anything critical, but about 360,000 events were not recorded and it shows how we did not understand JSON.parse entirely.

The commit

The commit to cause this was

+        data = JSON.parse(events_array[0]["data"])
+        referrer = data["referrer"] if data.is_a? Hash

We get a string from events_array[0][“data”] and we parse this string. But this string is not always a JSON. It is sometimes just pure string. All the specs pass and we are committing the code.

In the specs suite all the data that we are testing is “1”,”,2″- generally numbers. Why? Because this makes sense looking at the data of the spec and in the spec we’ve had only numbers as the string, but not a general string like “k1”.

It bites. It bites like a shark.

FabBRIX WWF, Shark in 3D building instructions