Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there an easy way to estimate size of a json object?

Tags:

json

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?

like image 888
user6604655 Avatar asked Aug 19 '16 21:08

user6604655


3 Answers

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
like image 126
Nick M. Avatar answered Oct 16 '22 13:10

Nick M.


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.

like image 39
Adi Stav Avatar answered Oct 16 '22 15:10

Adi Stav


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

like image 2
Shankar ARUL Avatar answered Oct 16 '22 15:10

Shankar ARUL