Click here to Skip to main content
13,145,774 members (60,800 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 5 Feb 2014

CJSON - Compressed JSON

, 5 Feb 2014
Rate this:
Please Sign up or sign in to vote.
This is a technique that I have thought of which can help us save bandwidth while communicating.


Data means actual understanding being represented in any sort. It may be facts, words, etc. Internet is basically sharing of data. In today’s technologically inclined world sharing of data via internet is increasing day by day and need to share more compressed and reliable form of data is the need so that we can share data at much faster rate and also use low bandwidth. Hence getting a motivation from JSON I have thought of a concept that I like to call CJSON (Compressed JSON). It basically is a more compressed form of JSON. The main idea behind this concept is to share data in a more compressed form and hence reduce bandwidth usage and also enable faster communication between the server and the client with the reliability that JSON provides. Today’s technological progress has ensured better processing power and it is improving with respect to time but the data that is shared between server and client needs to be presented in more compressed form.


Compressed Javascript Object Notation is a more compressed form of JSON. JSON is a key value pair representation to share data. In real world scenario, when there is exchange of data, majority of times an array of objects are exchanged. Hence when array of object is exchanged, the data that is sent over the line using JSON has keys that are repeated number of times depending on the length of the array.


Variable = [{"name": "karan", "surname":"savla"}, {"name": "karan1",
    "surname" : "savla1"}]; 

In the above example, we can see that the keys like name, surname are repeated. Hence we are wasting the bandwidth by sending this unwanted data. Hence what we can do instead is...


Variable = {"name,surname,..": 
["karan,savla,..", " karan1,savla1,…",..]}; 

The above example is a much compressed representation of the JSON array where the keys are not repeated and key and value are separated by the same JSON symbol of ":’ and we are not using curly brackets ({) again and again to separate the elements in the array, hence saving the bandwidth. Also if we observe the CJSON more closely, the number of inverted commas (") used are also reduced dramatically and hence we are saving the bandwidth. CJSON will not only work better for array scenarios but also if we are exchanging simple data like just one object value as below:


Variable = {"name":"karan","surname" : "savla"}; 


Variable = {"name,surname" : "karan,savla"}; 

In the above case, we can see that CJSON also works for simple data, i.e., it saves bandwidth. Now for more complex scenarios where we send nested objects like below:


Variable = [
{"name" : "a", "place" : "b",
"game" : {"playing" : "yes", "cricket"
: "red"}}}}
, {"name" : "c", "place" : "d",
"game" : {"playing" : "yes", "cricket"
: "green"}}}}]   


Variable = {"name,place,game{playing,cricket{bat[{type,weight}],bowl}}

In the above examples, we can see that even the complex objects can be represented using CJSON. The beginning of curly bracket ({) signifies the beginning of a new object. Hence, when we would de-serialize the above CJSON variable, we can just read the keys and find the type of objects in the present inside the main object and then similarly we can read the values and map them to a specific object.

Also to further compress the object that is sent over the wire and to further simply the process of de-serialization, we can construct following CJSON strings:

Variable = {@ObjectName
: "a,b,{{[{'wood','12'},{'aluminium','10'}],red}}"};

Once we receive the JSON string, we need to deserialize it back into object at the receiving end so that we can work on it. For this purpose, currently I am only aware of the two techniques mentioned below:

  1. First approach is that the deserializing code will need to check every object in the solution and match its properties to the object to be deserialized and then deserialize it. (This is what I can think of, there may be some better way but then too scanning of more than one object will be involved).
  2. Second approach is that while deserializing, we explicitly need to specify the object to be deserialized into the deserializing method.

Hence, we can further modify our string and send in the object name itself like:

Variable = {@"Employee" : ["...","..."]};

Also, if the object name at server and client is different, then we can also use the below syntax to communicate:

Variable = {@["SenderName","ReceiverName"] : ["….","…"]}; 

So now mapping becomes much faster and easier, also more bandwidth is saved. Here @ at the beginning will let our code know that following thing before the ":" is the name of the object and not properties. Also all the values will be by default arranged as per the object properties name in alphabetical order while performing serialization.

Also further compression can be achieved when the data is serialized. When we send a data through the wire we send the data as it is, in real world we send arrays of data through the wire. Today’s technological leaps have made sure that processing power has increased. Hence when we send data we can further compress it by.



Variable = [
{"name" : "karan", "place" : "mumbai",
"game" : {"playing" : "yes", "cricket"
: "red"}}}}<br />
, {"name" : "karan1", "place" : "@12",
"game" : {"playing" : "yes", "cricket"
: {"bat":[{"type":"@1221","weight":"14"},{"type":"@1221","weight":"14"},"bowl"
: "green"}}}}]


Variable = {"name,place,game{playing,cricket{bat[{type,weight}],bowl}}

What the above example states is that whenever we have a field that is repeated, then we can replace that field value with ‘@’ as a prefix and then the first location of the field like location for mumbai is it is in the 1st array and 2nd position in it, hence we get @12. This compression technique will figure out if compressing the field value is required or not and will replace the value with the compressed value only when compression is achieved like in above example "14" is not replace as value is in a compressed form then compression technique will compress it. Then when de-serializing at the client end, we can use the decompression technique to again reform the object as all the values are sent within the CJSON string.

CJSON Grammar

A CJSON text is a sequence of tokens. CJSON text is a serialized object or array.

CJSON-text = object / array

These are the six structural characters:

begin-array = [ left square bracket

begin-object    = { left curly bracket

end-array       =] right square bracket

end-object      = } right curly bracket

name-separator  = : colon

value-separator = , comma

Note: If we have a comma(,) in the value, then we need to convert it to (‘,’) while sending so that we can parse it at the client side

Apart from these, all keys and set of values are grouped using inverted comma and internally separated using comma example:

{"name,surname" : ["karan,savla","abc,xyz"]};

In the above example, name and surname are the keys that are grouped together and separated by comma which is the same case for the values. And the main array values are also separated by using comma.

Points of Interest

This is just a way that I have thought of to compress the JSON data that we are sending to save the bandwidth which is very crucial as the new mobile devices have come up. Please let me know if I have made a mistake somewhere or if this technique may fail in some case or so.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Karan Savla
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

SuggestionPoor JSON style Pin
Member 1289859911-Dec-16 6:15
memberMember 1289859911-Dec-16 6:15 
QuestionInteresting but how does it stack up? Pin
John B Oliver3-Mar-14 10:15
memberJohn B Oliver3-Mar-14 10:15 
AnswerRe: Interesting but how does it stack up? Pin
Karan Savla4-Mar-14 2:36
memberKaran Savla4-Mar-14 2:36 
GeneralGood Idea, but much consider about performent Pin
Member 104051646-Feb-14 15:12
memberMember 104051646-Feb-14 15:12 
GeneralRe: Good Idea, but much consider about performent Pin
Karan Savla6-Feb-14 18:29
memberKaran Savla6-Feb-14 18:29 
Questionmmmm so how would all the standard serializers out there Pin
Sacha Barber5-Feb-14 22:42
mvpSacha Barber5-Feb-14 22:42 
GeneralMy vote of 4 Pin
Ali Javani5-Feb-14 12:16
memberAli Javani5-Feb-14 12:16 
QuestionGood idea, but no metrics Pin
Member 78219435-Feb-14 9:44
memberMember 78219435-Feb-14 9:44 
AnswerRe: Good idea, but no metrics Pin
Karan Savla5-Feb-14 19:09
memberKaran Savla5-Feb-14 19:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 5 Feb 2014
Article Copyright 2014 by Karan Savla
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid