Search code examples
javascriptarraysjsonstringstringify

How to split javascript object into smaller parts


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.

I first converted the javascript object into an array and split them into smaller parts.

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

[ ['key1', x], ['key1', y], ['key1', z], ['key2', p], ....... ] - array converted

var arrays = []
while (arrayConvertedObject.length > 0)
    arrays.push(arrayConvertedObject.slice(0, 2))
}

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

[["key1", x], ["key1", y]] - array 1
[["key1", z], ["key2", p]] - array 2

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

{"key1": [x, y]} - string 1
{"key1": [z], "key2": [p]} - string 2

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

{"key1": [x, y], "key1": [z], "key2": [p]}

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

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.

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?

Thank you for reading a lengthy question. Any help will be appreciated.


Solution

  • The solution below uses direct string maniuplation.

    Haven't done any performance comparisons.

    var x="X", y="Y", z="Z", p="P";
    
    // your starting arrays 
    var subArrs = [ ['key1', x], ['key1', y], ['key1', z], ['key2', p]];
    
    // the result should be in this string
    var jsonString = "{}";
    
    // take each subArray
    subArrs.map(function(subArr) {
      var key = subArr[0];
      var val = subArr[1];
      // try to look for the key in the string
      var keyMatcher = '"'+key+'":\\[';
      var endOfObjectMatcher = '\\}$';
      var regexStr = keyMatcher + '|' + endOfObjectMatcher;
      jsonString = jsonString.replace(new RegExp(regexStr), function(match){
          if (match=='}') { 
            // the end of the object has been matched, 
            // so simply append the new key-value pair
            return ',"'+key+'":['+JSON.stringify(val)+"]}";
          } else { 
            // an existing key has been found, 
            // so prepend the value to the beginning of the array
            // (match contains something like: '"somekey":['
            return match + JSON.stringify(val) + ",";
          }
        });
    });
    
    // replace the falsely added first comma in the object
    jsonString = jsonString.replace('{,','{');
    
    // print it here
    document.write(">>" + jsonString + "<br/>");
    body {
       font-family: monospace;  
    }