2

For json, what is considered best practice when you have an array of objects? Do you name the array and then put in the objects or do you add an additional name to each of the objects within the array?

Example #1 (Car is named inside of cars, but requires the additional "{}":

{
    "cars": [
        {
            "car": {}
        },
        {
            "car": {}
        }
    ]
}

Example #2: Car is not named. Simply an object inside of cars array:

{
    "cars": [
        {},
        {}
    ]
}

Trying to work on json spec with another party and wanted to make sure we are properly specifying the json schemas.

4
  • This isn't JSON, this is just Javascript objects and arrays. Commented Apr 21, 2014 at 18:20
  • JSON is used when you convert Javascript data into a text format for sending over the network or saving in files. Commented Apr 21, 2014 at 18:20
  • What I mean is that the question relates to the underlying data structure, not how it's serialized. Commented Apr 21, 2014 at 20:04
  • I went by the first line, best practice when you have an array of objects. I thought he was asking how best to design the array of objects, not just how to communicate them. Commented Apr 21, 2014 at 22:18

1 Answer 1

6

The first format allows for future expansion, as you could change it to:

{
    "cars": [
        {
            "car": {},
            "driver": {}
        },
        {
            "car": {}
            "driver": {}
        }
    ]
}

without requiring changes to the code that processes the original layout.

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

Comments

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.