Skip to content

Get reviews

Before you can retrieve the reviews, you must get the resource endpoint via the OPTIONS request. Depending on the used routes it might be something like this:

curl -X OPTIONS 'http://localhost:8000/jsonapi'

The response will contain the required endpoints you have to use:

{
    "meta": {
        "prefix": null,
        "resources": {
            "review": "http://localhost:8000/jsonapi/review"
        }
    }
}

Have a look at retrieving an OPTIONS request for more details. An OPTIONS request to the review endpoint also reveals the supported filters:

curl -X OPTIONS 'http://localhost:8000/jsonapi/review'

It also returns the prefix you have to use if the value is not null:

{
    "meta": {
        "prefix": null,
        "filter": {
            "f_domain": {
                "label": "Return reviews for that domain, e.g. 'product'",
                "type": "string",
                "default": "product",
                "required": false
            },
            "f_refid": {
                "label": "Return reviews for the item from the domain with that ID",
                "type": "string",
                "default": null,
                "required": false
            }
        },
        "sort": {
            "ctime": {
                "label": "Sort reviews by creation date/time",
                "type": "string",
                "default": true,
                "required": false
            },
            "rating": {
                "label": "Sort reviews by rating",
                "type": "string",
                "default": false,
                "required": false
            }
        }
    }
}

Fetch reviews#

Now you can retrieve reviews via the "review" resource you've just received from the OPTIONS response. By default, the list of all product reviews is returned and you should filter them by the ID of the product the user requested, e.g.:

curl -X GET 'http://localhost:8000/jsonapi/review?filter[f_refid]=1'
var args = {
    filter: {
        'f_refid': '1'
    }
};
var params = {};

if(options.meta.prefix) { // returned from OPTIONS call
    params[options.meta.prefix] = args;
} else {
    params = args;
}

$.ajax({
    method: "GET",
    dataType: "json",
    url: options.meta.resources['review'], // returned from OPTIONS call
    data: params
}).done( function( result ) {
    console.log( result );
});

This response contains all reviews for the product with the ID "1":

{
    "meta": {
        "total": 1,
        "prefix": null,
        "content-baseurl": "http://localhost:8000/",
        "csrf": {
            "name": "_token",
            "value": "..."
        }
    },
    "links": {
        "next": "http://localhost:8000/default/jsonapi/review?f_refid=1&page%5Boffset%5D=10",
        "last": "http://localhost:8000/default/jsonapi/review?f_refid=1&page%5Boffset%5D=10",
        "self": "http://localhost:8000/default/jsonapi/review?f_refid=1"
    },
    "data": [{
        "id": "1",
        "type": "review",
        "links": {
            "self": {
                "href": "http:\/\/localhost:8000\/default\/jsonapi\/review?id=1",
                "allow": ["GET"]
            }
        },
        "attributes": {
            "review.id": "1",
            "review.refid": "1",
            "review.domain": "product",
            "review.response": "test response",
            "review.comment": "test comment",
            "review.rating": 5,
            "review.status": 1,
            "review.name": "test user"
        }
    }]
}

Tip

It's also possible to retrive reviews for other domains than product by using the f_domain parameter, e.g. &f_domain=customer. You can also filter by additional fields using the filter parameter.

Sort reviews#

In addition to the generic filter possibilities, you can sort reviews by these keys:

  • "ctime" (asc) or "-ctime" (desc)
  • "rating" (asc) or "-rating" (desc)
curl -X GET 'http://localhost:8000/jsonapi/review?sort=-rating'
var args = {'sort': '-rating'};
var params = {};

if(options.meta.prefix) { // returned from OPTIONS call
    params[options.meta.prefix] = params;
} else {
    params = args;
}

$.ajax({
    method: "GET",
    dataType: "json",
    url: options.meta.resources['review'], // returned from OPTIONS call
    data: params
}).done( function( result ) {
    console.log( result );
});

Tip

You can also sort by other fields, e.g. by the last modification date of the reviews using &sort=review.mtime

Count ratings#

You can get the rating counts for the reviews by using the aggregate key and the corresponding review.rating search key:

curl -X GET 'http://localhost:8000/jsonapi/review?aggregate=review.rating'
var args = {'aggregate': 'review.rating'};
var params = {};

if(options.meta.prefix) { // returned from OPTIONS call
    params[options.meta.prefix] = args;
} else {
    params = args;
}

$.ajax({
    method: 'GET',
    dataType: 'json',
    url: options.meta.resources['review'], // returned from OPTIONS call
    data: params
}).done( function( result ) {
    console.log( result );
});

This will return a list of "id" and "attributes" pairs where the value of "id" is the rating value and "attributes" is the number of reviews with that rating:

{
    "meta": {
        "total": 1
    },
    "data": [
        {"id":1,"type":"review.rating","attributes":"0"},
        {"id":2,"type":"review.rating","attributes":"2"},
        {"id":3,"type":"review.rating","attributes":"3"},
        {"id":4,"type":"review.rating","attributes":"5"},
        {"id":5,"type":"review.rating","attributes":"10"}
    ]
}

Comments