in reply to Formatting JSON the right way

Construct a complete hashref structure containing the entire JSON packet that you want to transmit, then encode_json to prepare the string to send. When receiving data from the host, do the opposite with decode_json. In this way, the JSON encoder/decoder becomes a black-box and the text-string is a mystery that only the black-box needs to know how to handle.