2

So I'm currently trying to develop a JSON document which will be easiest to iterate through and access objects from. I will be using underscore, so accessing objects isn't necessarily difficult, but I'm wondering what the best way of going about formatting is - best practices, etc.

Here're the two formats I'm considering: The first is an array

{
    "defaultViews" : [
        {
            "name" : "view1",
            "title" : "View 1"
        },
        {
            "name" : "view2",
            "title" : "View 2"
        }
    ]
}

Or, the other way, which is more.... "object oriented"...

{
    "defaultViews" : {
        "view1" : {
            "title" : "View 1"
        },
        "view2" : {
            "title" : "View 2"
        }
    }
}

So in the first example it's easier to iterate through the objects, whereas in the second example it's easier to access the object directly (obj.defaultViews.view1). I suppose this is quite subjective, but again I'm looking for what's hopefully considered a "best practice". Thanks!

5
  • You've answered to your question yourself: "So in the first example it's easier to iterate through the objects, whereas in the second example it's easier to access the object directly (obj.defaultViews.view1)." Commented Feb 1, 2013 at 7:20
  • Yes I suppose haha, so there's no real standard for this? Commented Feb 1, 2013 at 7:23
  • Exact format generally depends on your goal. Also, second notation is applicable only when keys (view1, view2) are unique while in first notation there is not such limitation. Commented Feb 1, 2013 at 7:25
  • Soliciting opinions about code design belong in codereview.stackexchange.com, not here. Commented Feb 1, 2013 at 7:26
  • 'more.... "object oriented"' - I can't tell if you mean that as a joke, or if you think it somehow follows "object oriented" coding principles? The first way is perfectly fine from an OO point of view. (By the way, you don't iterate through JSON, you parse the JSON and iterate through the resulting object/array.) Commented Feb 1, 2013 at 7:37

1 Answer 1

1

there is nothing wrong with using an array in object orientation principles. also the first examples that uses an array is a better approach because what you actually want is an array

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I'm leaning towards this too - just because I do have the option of iteration through the array as opposed to the other way..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.