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.
This is a normal comportement.
{"key1": [1, 2], "key1": [3], "key2": [4]}
You define 2 times the key1
attribute. But each key must be unique. So the second declaration override the first.
It think you must change your concatenation method to concatenate each JSON string into an array like this :
[
{"key1": [1, 2]},
{"key1": [3], "key2": [4]}
]
What is you Node.JS version ?
I am trying to JSONify a Javascript object only to get "Invalid String Length" error.
This is an old issue of V8 engine, see issue #14170. You may be considered to try on a greater version of Node.JS.