How to split javascript object into smaller parts
<p>I am trying to JSONify a Javascript object only to get "Invalid String Length" error. I decided to break the object up into smaller parts, use JSON.stringify on the smaller parts, and append each segment to the file.</p>

<p>I first converted the javascript object into an array and split them into smaller parts.</p>

<p><code>{'key1': [x, y, z], 'key2' : [p, q, l], ...... }</code> - a sample of original object in JSON notation. Each character <code>x, y, z, p, q, l</code> is an abbreviation of a base 64 string that is long enough to cause the string length overflow problem.</p>

<p><code>[ ['key1', x], ['key1', y], ['key1', z], ['key2', p], .......</code> ] - array converted</p>

<pre><code>var arrays = []
while (arrayConvertedObject.length &gt; 0)
    arrays.push(arrayConvertedObject.slice(0, 2))
}
</code></pre>

<p>Then I was going to create a javascript object for each of the smaller arrays in <code>arrays</code> to use JSON.stringify individually. </p>

<pre><code>[["key1", x], ["key1", y]] - array 1
[["key1", z], ["key2", p]] - array 2
</code></pre>

<p>When I convert each smaller array into a Javascript object and use JSON.stringify, I get :</p>

<pre><code>{"key1": [x, y]} - string 1
{"key1": [z], "key2": [p]} - string 2
</code></pre>

<p>The problem is that the string concatenation with extra manipulation of <code>},{</code> will not retain the original data :</p>

<pre><code>{"key1": [x, y], "key1": [z], "key2": [p]}
</code></pre>

<p>When I want <code>obj["key1"]</code> to have <code>[x, y, z]</code>, the JSON above will be parsed into <code>obj["key1"] = [z]</code>.</p>

<p>If I do not use JSON.stringify on the smaller objects, it will defeat my original goal of JSONifying a large javascript object. But if I do so, I cannot concatenate JSONified small objects that have duplicate keys.</p>

<p>Is there any better way to deal with JSON.stringify "Invalid String Length" error? If not, is there a way to concatenate JSONified objects without overriding duplicate keys?</p>

<p>Thank you for reading a lengthy question. Any help will be appreciated.</p>