Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any well-known method for DRYing JSON

Tags:

Consider this JSON response:

[{     Name: 'Saeed',     Age: 31 }, {     Name: 'Maysam',     Age: 32 }, {     Name: 'Mehdi',     Age: 27 }] 

This works fine for small amount of data, but when you want to serve larger amounts of data (say many thousand records for example), it seems logical to prevent those repetitions of property names in the response JSON somehow.

I Googled the concept (DRYing JSON) and to my surprise, I didn't find any relevant result. One way of course is to compress JSON using a simple home-made algorithm and decompress it on the client-side before consuming it:

[['Name', 'Age'],  ['Saeed', 31],  ['Maysam', 32],  ['Mehdi', 27]] 

However, a best practice would be better than each developer trying to reinvent the wheel. Have you guys seen a well-known widely-accepted solution for this?

like image 495
Saeed Neamati Avatar asked Dec 30 '12 07:12

Saeed Neamati


People also ask

What is JSON and how to use it?

JSON stands for JavaScript Object Notation. It’s an easy-to-parse and lightweight data-interchange format. In spite of its name, JSON is completely language-agnostic, so it can be used with any programming language, not just JavaScript.

What is JSON and json JSON stringify?

JSON is a data format that has its own independent standard and libraries for most programming languages. JSON supports plain objects, arrays, strings, numbers, booleans, and null. JavaScript provides methods JSON.stringify to serialize into JSON and JSON.parse to read from JSON. Both methods support transformer functions for smart reading/writing.

Is it possible to use JSON for data exchange?

Initially it was made for JavaScript, but many other languages have libraries to handle it as well. So it’s easy to use JSON for data exchange when the client uses JavaScript and the server is written on Ruby/PHP/Java/Whatever. JSON.stringify to convert objects into JSON. JSON.parse to convert JSON back into an object.

Can you convert JSON to JavaScript?

Because of this similarity, a JavaScript program can easily convert JSON data into native JavaScript objects. The JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only. Code for reading and generating JSON data can be written in any programming language.


2 Answers

First off, JSON is not meant to be the most compact way of representing data. It's meant to be parseable directly into a javascript data structure designed for immediate consumption without further parsing. If you want to optimize for size, then you probably don't want self describing JSON and you need to allow your code to make a bunch of assumptions about how to handle the data and put it to use and do some manual parsing on the receiving end. It's those assumptions and extra coding work that can save you space.

If the property names and format of the server response are already known to the code, you could just return the data as an array of alternating values:

['Saeed', 31, 'Maysam', 32, 'Mehdi', 27] 

or if it's safe to assume that names don't include commas, you could even just return a comma delimited string that you could split into it's pieces and stick into your own data structures:

"Saeed, 31, Maysam, 32, Mehdi, 27" 

or if you still want it to be valid JSON, you can put that string in an array like this which is only slightly better than my first version where the items themselves are array elements:

["Saeed, 31, Maysam, 32, Mehdi, 27"] 

These assumptions and compactness put more of the responsibility for parsing the data on your own javascript, but it is that removal of the self describing nature of the full JSON you started with that leads to its more compact nature.

like image 75
jfriend00 Avatar answered Sep 23 '22 06:09

jfriend00


One solution is known as hpack algorithm

https://github.com/WebReflection/json.hpack/wiki

like image 34
Yubin Bai Avatar answered Sep 25 '22 06:09

Yubin Bai