title: How do I convert a JSONObject to a byte array and then convert this byte array to get back the original JSONObject?
body: <p>I am using the <a href="http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/util/json/JSONObject.html" rel="noreferrer">AWS <code>JSONObject</code> class</a>. Let's say I define a <code>JSONObject</code> object like so:</p>
  
  <pre><code>JSONObject obj = new JSONObject();
  obj.put("Field1": 35);
  
  JSONObject nestedObj = new JSONObject();
  nestedObj.put("Name1":"value1");
  nestedObj.put("Name2":42);
  
  obj.put("Field2": nestedObj);
  </code></pre>
  
  <p>So the JSONObject looks like:</p>
  
  <pre><code>{"Field1": 35,
   "Field2": {"Name1": "value1",
              "Name2": 42}
  }
  </code></pre>
  
  <p>I want to take this <code>JSONObject</code> and convert it to a byte array somehow:</p>
  
  <pre><code>byte[] objAsBytes = convertToBytes(obj);
  </code></pre>
  
  <p>where <code>convertToBytes</code> is some function that does this correctly. Then I would like to take this byte array and convert it back to the original <code>JSONObject</code> so it still preserves its original structure. </p>
  
  <p>Does anyone know how to do this? I would like to do this because I am using Amazon Kinesis and more specifically the <code>PutRecord</code> API and a <code>PutRecordRequest</code> requires the data to be a <code>ByteBuffer</code>, so I need to convert the <code>JSONObject</code> to a byte array, and then wrap the byte array as a <code>ByteBuffer</code>. Then, when I retrieve the record I need to convert the <code>ByteBuffer</code> to a byte array and then get the original JSONObject.</p>