How to break javascript object into smaller parts

I am trying to JSONify a Javascript object only to get an "Invalid string length" error. I decided to break the object into smaller parts, use JSON.stringify in small parts, and add each segment to the file.

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

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

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

 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 to a Javascript object and use JSON.stringify, I get:

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

The problem is that string concatenation with additional manipulation },{ will not save the original data:

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

When I want obj["key1"] have [x, y, z] , the JSON above will parse obj["key1"] = [z] .

If I do not use JSON.stringify on smaller objects, it will defeat my original JSONifying target of a large javascript object. But if I do this, I cannot combine JSONified small objects with duplicate keys.

Is there a better way to handle the JSON.stringify "Invalid String Length" error? If not, is there a way to combine JSONified objects without overriding duplicate keys?

Thanks for reading the long question. Any help would be appreciated.

+6
source share
2 answers

The solution below uses direct row mannulation.

No performance comparisons were made.

 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; } 
+1
source

This is a normal comportement.

{"key1": [1, 2], "key1": [3], "key2": [4]}

You define the attribute key1 2 times. But each key must be unique. Thus, the second declaration cancels the first.

It seems you should change your concatenation method to concatenate each JSON string into an array like this:

 [ {"key1": [1, 2]}, {"key1": [3], "key2": [4]} ] 

What are you Node.JS version?

I am trying to JSONify a Javascript object only to get the "Invalid String Length".

This is an old question about the V8 engine, see issue # 14170 . You can try using a larger version of Node.JS.

+1
source

All Articles