3

I'm trying to represent this table using JSON, but do not know what the best way to do. Let's say I have 3 different buckets and I want to group them and show the total number of items by fruit types:

           | Orange  | Apple  | Banana  |
----------------------------------------
Bucket 1   |    3    |    4   |   7     |
Bucket 2   |    5    |    6   |   2     |
Bucket 3   |    5    |    11  |   8     |

One way I can think of is this:

{"buckets":

    [ 
       {"BucketId":1, "FruitType": "Orange", "Count": 3},
       {"BucketId":1, "FruitType": "Apple", "Count": 4},
       {"BucketId":1, "FruitType": "Banana", "Count": 7},
       {"BucketId":2, "FruitType": "Orange", "Count": 5},
       {"BucketId":2, "FruitType": "Apple", "Count": 6},
       {"BucketId":2, "FruitType": "Banana", "Count": 11},
       {"BucketId":3, "FruitType": "Orange", "Count": 5},
       {"BucketId":3, "FruitType": "Apple", "Count": 11},
       {"BucketId":3, "FruitType": "Banana", "Count": 8}
    ]
}

Is there is a better way of respresent in JSON?

Thanks.

Andy
  • 61
  • 1
  • 7

3 Answers3

1

Think about how you want to use your JSON. Your suggested answer breaks up the contents of one bucket across lots of objects. To find the number of oranges in bucket 1, you would have to write a loop. Yuck. I would suggest...

"buckets" : {
    "1":{"oranges":3,"apples":4,"bananas":7},
    "2":{"oranges":5,"apples":6,"bananas":2},
    "3":{"oranges":5,"apples":11,"bananas":8}
}

If you have an ID, use it as the name of the bucket objects. This lets you access the objects by ID afterwards. For example

buckets["1"].oranges
bbsimonbb
  • 27,056
  • 15
  • 80
  • 110
  • What if the id is not equal to the array index? It's not in the example, because arrays are typically zero-indexed – Tim Oct 21 '15 at 08:20
  • I've changed the answer to incorporate using large IDs as a key to the "array". Javascript uses [sparse arrays](http://stackoverflow.com/questions/1510778/are-javascript-arrays-sparse), so this is a perfectly [workable format](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors). – bbsimonbb Oct 21 '15 at 13:09
0

Basically a bucket is an object that holds a list of fruit objects that each have some properties.

I think this is a lot cleaner. Give each fruit their own { } because they are objects with their own properties etc.

{
    "buckets": [
        {
            "id": 1,
            "fruits": [
                {
                    "type": "orange",
                    "count": 3
                },
                {
                    "type": "apple",
                    "count": 4
                }
            ]
        },
        {
            "id": 2,
            "fruits": [
                {
                    "type": "orange",
                    "count": 5
                },
                {
                    "type": "apple",
                    "count": 6
                }
            ]
        }
    ]
}

This also allows for multiple fruits/counts per bucket, which your example does not.

Tim
  • 41,901
  • 18
  • 127
  • 145
0

I know 3 JSON specifications : JSON-LD, HAL and JSON API. I prefer the second.

So I will write your json like this :

{
"buckets": [
    {
        "id": 1,
        "orange": 3,
        "apple": 4,
        "banana": 7
    },
    {
        "id": 2,
        "orange": 5,
        "apple": 6,
        "banana": 2
    },
    {
        "id": 3,
        "orange": 5,
        "apple": 11,
        "banana": 8
    }
]

}

@Andy's response is more like JSON API specification.

sebthemonster
  • 982
  • 8
  • 11