I'm working on a security service that will return a list of permissions and I'm trying to estimate the size of the json response object. Here's a piece of sample data:
ID=123 VariableName=CanAccessSomeContent
I'm looking for an easy way to estimate what size of the json response object will be with 1500 rows. Is there an online estimation tool or some other technique I can use to easily get a rough size estimate?
Using Python you can estimate the size by creating the dictionary or just make one...
import json
import os
import sys
dict = {}
for a in range(0, 1500):
dict[a] = {'VariableName': 'CanAccessSomeContent'}
output = json.dumps(dict, indent = 4)
print ("Estimated size: " + str(sys.getsizeof(output) / 1024) + "KB")
with open( "test.json", 'wb') as outfile:
outfile.write(output)
print ("Actual size: " + str(os.path.getsize('test.json') / 1024) + "KB")
Output:
Estimated size: 100KB
Actual size: 99KB
I solved it, when I needed to, by adding a File-like object that just counted the characters and json.dump()ing into it:
# File-like object, throws away everything you write to it but keeps track of the size.
class MeterFile:
def __init__(self, size=0):
self.size = size
def write(self, string):
self.size += len(string)
# Calculates the JSON-encoded size of an object without storing it.
def json_size(obj, *args, **kwargs):
mf = MeterFile()
json.dump(obj, mf, *args, **kwargs)
return mf.size
The advantage is that encoding is not stored in memory, which could be large especially in cases you care about the size to begin with.
Function to estimate file size (Mash of JSON-Size & UTF-8 Length node repos)
function json_filesize (value) {
// returns object size in bytes
return (~-encodeURI(JSON.stringify(value)).split(/%..|./).length)/1048576
}
json_filesize({foo: 'bar'}) >> 13
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