I want to calculate json array Size in KB/Mb. I have a condition where I need to calculate json Array size before uploading. It should not be more than 128 KB.
To obtain the size of a JSON-encoded array or object, use the json_size function, and specify the column containing the JSON string and the JSONPath expression to the array or object.
Use measureLength() to compute the number of characters that will be printed by printTo() . Use measurePrettyLength() to compute the number of characters that will be printed by prettyPrintTo() .
One of the more frequently asked questions about the native JSON data type, is what size can a JSON document be. The short answer is that the maximum size is 1GB.
The maximum length of a JSON type using a binary storage format is 16776192 bytes.
Convert your jsonArray
to String
and use string.getBytes().length
. It will give number of bytes used by the string to store the value.
Using those bytes you can calculate the size in any unit.
String.getBytes().length is the number of bytes needed to represent your string in the platform's default encoding. For example, if the default encoding was UTF-16 (rare), it would be exactly 2x the value returned by String.length(). More commonly, your platform encoding will be a multi-byte encoding like UTF-8.
JSON is basically an String
and encodings determine how much memory is required to store a String
. Please read this first.
Now with that knowledge you can simply proceed as follow:
JSON.toString().getBytes(YOUR_PREFERRED_ENCODING).length;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With