This first if check should be done before anything else. Especially line 110 seems to be useless if the key does not exist. Since the other cases won't be reached if the exception is thrown, you don't need to put them into else. Finally, this whole code can go into a method that takes the json and returns the id to simplify (and shorten) the method.