Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make my JSON less verbose?

Tags:

I'm currently developing a web application and using JSON for ajax requests and responses. I have an area where I return a very large dataset to the client in the form of an array of over 10000 objects. Here's part of the example (its been simplified somewhat):

"schedules": [
        {
            "codePractice": 35,
            "codeScheduleObject": 576,
            "codeScheduleObjectType": "",
            "defaultCodeScheduleObject": 12,
            "name": "Dr. 1"
        },
        {
            "codePractice": 35,
            "codeScheduleObject": 169,
            "codeScheduleObjectType": "",
            "defaultCodeScheduleObject": 43,
            "name": "Dr. 2"
        },
        {
            "codePractice": 35,
            "codeScheduleObject": 959,
            "codeScheduleObjectType": "",
            "defaultCodeScheduleObject": 76,
            "name": "Dr. 3"
        }
    ]

As, you can imagine, with a very large number of objects in this array, the size of the JSON reponse can be quite large.

My question is, is there a JSON stringifier/parser that would convert the "schedules" array to look something like this as a JSON string:

"schedules": [
    ["codePractice", "codeScheduleObject", "codeLogin", "codeScheduleObjectType", "defaultCodeScheduleObject","name"],
    [35, 576, "", 12, "Dr. 1"],
    [35, 169, "", 43, "Dr. 2"],
    [35, 959, "", 76, "Dr. 3"],
]

ie, that there would be an array at the beginning of the "schedules" array that held the keys of the objects this array, and all of the other container arrays would hold the values.

I could, if I wanted, do the conversion on the server and parse it on the client, but I'm wondering if there's a standard library for parsing/stringifying large JSON?

I could also run it through a minifier, but I'd like to keep the keys I have currently as they give some context within the application.

I'm also hoping you might critique my approach here or suggest alternatives?