You're likely to need to actually encode it to JSON to be able to calculate that. The data model may not accurately indicate exactly what properties are exposed via JSON, what they're called, or how they're encoded.
There are a few other questions on SO asking roughly the same thing, see
is there an easy way to estimate size of a json object? and
Calculate size in bytes of JSON payload including it in the JSON payload in PHP
You don't say how you're using JSON, but it's fairly likely it's using Jackson, and perhaps Spring, so something like this ought to put you in the basic ballpark
ObjectMapper om = new ObjectMapper();
Object example = ... // the object you want to find the JSON size for
String jsonString = om.writeValueAsString(example); // get the JSON string
int characterCount = json.length(); // count the number of characters
byte[] bytes = om.writeValueAsBytes(example); // get the JSON as UTF-8 bytes
int byteCount = bytes.length; // count the number of bytes
The difference between characterCount
and byteCount
may not be obvious, especially if you're not used to dealing with Unicode code-points. HTTP Content-Length
indicates the number of octets (bytes), so byteCount
would be most accurate, see What's the "Content-Length" field in HTTP header?.