1

I have done a fair amount of work with JSON in the past, but I've never needed to write my own JSON object. Now that I'm tasked with doing so, I'm having some trouble creating it in such a way that will cause me the least amount of trouble down the road. Basically, I want to do it right the first time.

My JSON array will eventually grow larger, but for these purposes I'll keep it small.

Here is what I have currently done:

{
    "ClassGroup": [
        {
            "id": "123",
            "classname": "Class 1",
            "isActive": "true",
            "isExpanded": "false",
            "students": [11, 22, 33, 44, 55],
            "isSelected": [11, 22, 33, 44, 55]
        },
        {
            "id": "456",
            "classname": "Class 2",
            "isActive": "false",
            "isExpanded": "false",
            "students": [66, 77, 88, 99],
            "isSelected": [66, 88, 99]
        }
    ],
    "Student": [
        {
            "id": "11",
            "first": "Student",
            "last": "One",
            "classes": [123],
            "isSelected": [123]
        },
        {
            "id": "22",
            "first": "Student",
            "last": "Two",
            "classes": [123],
            "isSelected": [123]
        }
    ]
}

A lot of the students and classes are missing from the array for now, so ignore any missing IDs that you see.

When doing it this way, and running the jQuery.getJSON function, I end up with an array like this:

data['ClassGroup'][0]['id'] will equal "123" data['ClassGroup'][1]['id'] will equal "456" data['Student'][0]['id'] will equal "11"

The problem with this is that when given an ID of a class, I can't easily locate the class. For example, if I want to find the name of class "456", I would have to iterate through the array until I find the X such that data['ClassGroup'][X]['id'] will equal "456", rather than just doing data['ClassGroup']['456']['classname'].

What I would like to do is re-write this JSON such that the result of $.getJSON will be consistent with the following:

data['ClassGroup']['123']['classname'] = "Class 1" data['ClassGroup']['456']['classname'] = "Class 2" data['Student']['11']['last'] = "One"

I think you get the picture.

Am I going about this the right way?

2
  • What language is your server-side code? Are you able to change the way you get this JSON? I have a JSON.NET ArrToObjConverter that you can assume some array's object property as an object property, and your JSON would return something like: "ClassGroup": { "123": { "classname": "Class 1", "isActive": "true", "isExpanded": "false", "students": [11, 22, 33, 44, 55], "isSelected": [11, 22, 33, 44, 55] }, "456": { "classname": "Class 2", "isActive": "false", (...) } } Commented Apr 3, 2014 at 16:44
  • We are partnering up with a company for this project. We are doing the front-end work, while they will be doing the bank-end (with ASP and MsSQL, I believe). We can make this JSON array however we like, and the other company will write a back-end script that will generate a file in this format filled with data. Commented Apr 3, 2014 at 17:06

2 Answers 2

3

Rewrite your JSON so it look like this :

{
    "ClassGroup": { /* <- Changed from [ to { */
        "123": {    /* <- The class id is now the array key */
            "id": "123",
            ...
        },
        "456": {
            "id": "456",
            ...
        }
    } /* <- Changed from ] to } */
}
Sign up to request clarification or add additional context in comments.

3 Comments

Is it redundant to have the class id twice? I was just thinking that it might reduce the overall JSON size if the class set is large.
You can remove it if you want. I just want to show that the id is taken from it's property.
I appreciate the responses. For some reason I had gotten so tangled up in the Key-Value pairings that I couldn't come up with the idea of using the actual ID, which is typically a value, as a key. I reformatted my JSON and everything is working perfectly. Thanks!
1

You could write a function to take away all of the cruft involved with iteration:

function getPropValue(param) {
  var obj = data.ClassGroup.filter(function (el) {
    return el[param.where] === param.is;
  });
  return obj[0][param.find];
}

getPropValue({
  find: 'classname',
  where: 'id',
  is: '456'
});

Fiddle

1 Comment

This is a very handy function that I will surely get some use of down the road. Since we have the perk of getting to format the JSON however we like, I think I'm going to go the route that Iqbal suggested. This web application is going to involve a lot of jumping around between "tables" using IDs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.