2

Let's say I have a JSON structure that contains the following:

{
    "ROWS": [{
        "name": "Greg",
        "age": "24",
    },
    {
        "name": "Tom",
        "age": "53",
    }]
}

The value for the key "ROWS" is a list of dictionaries, right?

Okay, well what if I only have one entry? Is it still appropriate to use list notation, even if that list has a single element?

{
    "ROWS": [{
        "name": "Greg",
        "age": "24",
    }]
}

Would there be any reason I could NOT do this?

1
  • 2
    Pretty much up to you. If there is a chance that it will have multiple entries potentially (as the name ROWS seems to imply), better go withthe array. Commented Jun 12, 2014 at 3:46

2 Answers 2

6

There is no technical reason why you could not use a list. Your array could be empty and that's perfectly acceptable and valid technically.

For your ROWS property I think the most important thing to consider is how many rows you could possibly have. You want to incorporate the computer engineering principle of generality to make sure you don't paint yourself into a corner by making ROWS an object. If you can expect to ever have more then one object as a row, even if currently there is only one, then it's absolutely appropriate to use an array.

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

1 Comment

Thanks! I'm inclined to agree - and in this case, you're right...there's always a possibility for more than one object. So as long as it's permissible, I'd like to always have the brackets there.
1

For example let's assume you expect to get a unique record such as a login system. Then it wouldn't make sense to use an array , in this case you should use an object instead

{
    "LOGIN_ROW": {
        "name": "Greg",
        "age": "24",
    }
}

Again I said should because it's up to you to format your json object graph. But of course if you have a scenario where you have a list of employees then it would make sense to use an array:

{
    "LIST_OF_ROWS": [{
        "name": "Greg",
        "age": "24",
    }]
}

This is perfectly fine because you have one employee at this time but you wish to expand your company so you would expect to get more employees.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.