for this script utl_file is no longer being used. The problem is that some SQL is over 2499 characters in length, and it may be stored as one line in the data dictionary SQLPlus cannot execute lines ...
I believe htp.p cannot handle such large values without them being split. However, the fix I recommend is simply to let APEX_JSON output the to the stream like this ...