How to present data from a table in JSON format How to present data from a table in JSON format json json

How to present data from a table in JSON format


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.


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.


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