1

Well, this is my first nodeJS app. I've been researching on RESTful API best practices, but none seem to directly address my concerns.

Here is the format which my data is saved in the data base:

'1212': {
    name: 'Plasma Blaster',
    id: 1212,
    price: 7000,
    quantity: 10,
    minimum: false,
    date: new Date().toISOString().replace('T', ' ').substr(0, 19),
    image: 'http://www.img.com/image.jpeg',
},

Each item is saved with the string of it's unique id as the key.BTW, I'm using simple data structures as db.

A get request made to the endpoint /api/v1/store/products returns the result:

{
"completed": true,
"message": "get products sucessful",
"products": {
    "1370": {
        "name": "DH-17 blaster pistol",
        "id": 1370,
        "price": 600,
        "quantity": 10,
        "minimum": "false",
        "date": "2018-11-05 13:29",
        "image": "http://www.img.com/image.jpeg"
    },
    "1473": {
        "name": "C-22 fragmentation grenade",
        "id": 1473,
        "price": 200,
        "quantity": 16,
        "minimum": "false",
        "date": "2018-11-05 13:32",
        "image": "http://www.img.com/image.jpeg"
    },
    "8385": {
        "name": "Neon-Blue Crystal Lightsaber",
        "id": 8385,
        "price": 200,
        "quantity": 1,
        "minimum": "true",
        "date": "2018-11-05 13:35",
        "image": "http://www.img.com/image.jpeg"
    },
    "0836": {
        "name": "Treppus-2 vibroblade",
        "id": 836,
        "price": 2000,
        "quantity": 1,
        "minimum": "true",
        "date": "2018-11-05 13:36",
        "image": "http://www.img.com/image.jpeg"
    }
}

}

I need know if this an accepted structure. Api response structures I've worked with before are Array of objects. I'll also appreciate help in formatting it better.

2 Answers 2

1

There isn't a lot of guidance on "accepted structure" for JSON resources, most folks just care if you're consistent.

That said, I think your "complete" and "message" keys don't add much as they should be obvious based on the HTTP response code (eg 200 Success). Clients may find it slightly easier to work with an array vice an object, but that's less a concern

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

4 Comments

Thanks Paul, I'll remove the parts you suggested. I considered using an array, even Mongodb queries returns an array of objects, I think. But since I'm working with data structures, I learnt querying objects will be are better in terms of Time Complexities since I could just do Object[id] instead of Array.forEach(object => {if object.id = id ... })
@Mudi maybe, but "early optimization is the root of all evil" or something like that. ;) That said, it really depends on your use case. If you're typically going to be displaying them as a list, then Array makes sense. If you're going to be normally searching for them by ID on the client, the object makes sense. If you want both, i've even done a clientside index where the data is in an array and then I have an object with keys as the IDs and values as the array position of the item.
whoa, no way I would've thought about that! Actually, the results from that endpoint will likely summarised on a table client-side. Then, complete information about a single object can be pulled up from the table (this will be done by object[id] back-end). I thought that structure might be the best way to implement this. Your thoughts please?
@Mudi sure, for your use case I would probably build the table from an array of items. If you don't need it looked up by ID on the client (sounds like you're talking about a separate request to a details endpoint like /api/item/:itemId), just embed the ID as data somewhere in the row and let the client fetch it separately. If you want to do the lookup on the client, then just build the index I described as you build the table so the client doesn't have to iterate multiple times, if you're worried about that.
0

The accepted structure depends on what the client(s) that consumes your API requires, some people call it a "contract". And if you need to define it with a tool I'd recommend swagger.

On the matter you mentioned about your json in particular, an array of objects is indeed what makes more sense for the products key of your json, but that's just because an array is the simplest structure. If what you are developing requires a data structure on which you need to quickly retrieve a product by it's id, your structure is better.

1 Comment

Thanks Graciano, I'll check out your recommendation. You're on point, and like you said, objects is definitely better for this since each product will be looked up by id primarily.

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.