2

I have the following JSON:

{"test": {"property 1": 345, "property 2": 976, "property 3": "asd"}}

I need to compress it to be very short, like that (in URL)

/#params=abs54sgdasd1we!ewd

I have a list of defined properties on that JSON, so that's why I'm asking what is the best dictionary encoder for JS. Later I should be able to decode from that string back to JSON.

Alex Ivasyuv
  • 8,585
  • 17
  • 72
  • 90

1 Answers1

0

You could just HTMLencode the whole JSON string. Remember that you need the key in "key": "value" pairs in the Object/"Associative array"/"dictionary", since ordering is not mandatory for that. Otherwise, 345|976|"asd" could end up as 976|"asd"|345 without the keys and have unexpected results. Otherwise, covert it to an Array/List so the order is maintained and you only rely on the order of values.

If "very short" is the main requirement and it must be in the URL, then store the JSON in a DB and generate an id or hash (like crc, md5) based on it as a lookup key which you can add to the URL parameters.

Edit: The property1,2,3 names can be reduced to a, b, c if you don't want to switch to an Array.

Edit2: If the application it will interact with is cookie aware, you could store the data in a cookie and the hash in the URL.

And, if the values of property 1-3 are limited, you could map them to predefined values of fixed length.

Community
  • 1
  • 1
aneroid
  • 12,983
  • 3
  • 36
  • 66