ScottM ScottM - 3 months ago 7
Javascript Question

JSON object - which structure makes more sense?

I'm just learning JSON & Javascript objects and trying to figure out the best way to structure an object for a particular project.

I think I've mapped out the object pretty well, but I've thought of two different ways of doing it and I'm not sure which is 'better' and what the implications are of doing it one way rather than the other.

{
"session": {
"sessionId": "",
"sessionTitle": "",
"sessionDescription": "",
"clientName": "",
"sessionTopics": [{
"topicNames": [
"name of first topic",
"name of second topic",
"name of third topic",
"name of fourth topic",
"name of fifth topic"
],
"topicChoices": [
["a", "c", "a", "b", "e", "a", "d", "e", "c", "b"],
["c", "b", "a", "c", "d", "a", "b", "c", "a", "b"],
["a", "b", "a", "b", "e", "a", "d", "e", "c", "d"],
["a", "c", "a", "b", "e", "a", "d", "e", "c", "b"],
["c", "d", "b", "c", "e", "b", "d", "e", "c", "a"]
],
"topicLmode": [
7, 9, 6, 9, 5
]
}],
"imageDescriptions": [
"client's description of image A",
"client's description of image B",
"client's description of image C",
"client's description of image D",
"client's description of image E"
]
}
}


-- or --

{
"session": {
"sessionId": "",
"sessionTitle": "",
"sessionDescription": "",
"clientName": "",
"sessionTopics": [{
"firstTopic": [{
"topicName": "name of first topic",
"topicChoices": [
"b", "c", "a", "d", "e", "a", "e", "b", "c", "b"
],
"lmodeRating" : 7
}]
}, {
"secondTopic": [{
"topicName": "second topic name",
"topicChoices": [
"a", "c", "a", "b", "e", "a", "d", "e", "c", "b"
],
"lmodeRating": 9
}]
}, {
"thirdTopic": [{
"topicName": "third topic name",
"topicChoices": [
"a", "c", "a", "b", "e", "a", "d", "e", "c", "b"
],
"lmodeRating": 6
}]
}, {
"fourthTopic": [{
"topicName": "fourth topic name",
"topicChoices": [
"a", "c", "a", "b", "e", "a", "d", "e", "c", "b"
],
"lmodeRating": 9
}]
}, {
"fifthTopic": [{
"topicName": "fifth topic name",
"topicChoices": [
"a", "c", "a", "b", "e", "a", "d", "e", "c", "b"
],
"lmodeRating": 5
}]
}],
"imageDescriptions": [
{
"imageA": "description of image A"
}, {
"imageB": "description of image B"
}, {
"imageC": "description of image C"
}, {
"imageD": "description of image D"
}, {
"imageE": "description of image E"
}
],
}
}


I don't know whether it makes a difference to which would be better, but I won't know ahead of time how many "topics" there will be in each session (I do know it will be likely always be 4-6 topics).

Also possibly relevant -- I'll need to be able to get data by topic for some calculations and for output, as well as calculations across topics (e.g., comparing 'topicChoices' across two topics - is 4th choice the same in topic 1 as in topic 4?).

My gut tells me that perhaps that makes the first approach better (easier to work with), but I'm not sure.

I'm open to suggestions!

Thanks
Scott

Answer

I suggest a 3rd version, which allows a better iteration of sessionTopics

var data = {
    "session": {
        "sessionId": "",
        "sessionTitle": "",
        "sessionDescription": "",
        "clientName": "",
        "sessionTopics": [
            {
                "topicName": "name of first topic",
                "topicChoices": [
                    "b", "c", "a", "d", "e", "a", "e", "b", "c", "b"
                ],
                "lmodeRating": 7
            }, {
                "topicName": "second topic name",
                "topicChoices": [
                    "a", "c", "a", "b", "e", "a", "d", "e", "c", "b"
                ],
                "lmodeRating": 9
            }
        ],
        "imageDescriptions": [
            {
                "imageA": "description of image A"
            }, {
                "imageB": "description of image B"
            }, {
                "imageC": "description of image C"
            }, {
                "imageD": "description of image D"
            }, {
                "imageE": "description of image E"
            }
        ],
    }
};