6

I created a product content type with content reference to article(one product to many articles). Then I added field "Content: ID" "Content: Title" and "(field_article: Content) Content: Title" to a new REST export View. "(field_article: Content) Content: Title" come from the relationship "Content referenced from field_article" I added in advanced. enter image description here The output is:

[
    {
        "nid":"3",
        "title":"Product1 title",
        "article_title":"Article1 title"
    },
    {
        "nid":"3",
        "title":"Product1 title",
        "article_title":"Article2 title"
    }
]

What I would like to achieve is something like this:

[
    {
        "nid":"3",
        "title":"Product1 title",
        "articles":
            [
                {
                    "title":"Article1 title"
                },
                {
                    "title":"Article2 title"
                }
            ]           
    }
]

How to implement this?

UPDATED: I also tried aggregation, but in case of REST export View as the format is Serializer, no "group by" option in format setting but hal_json, json and xml options, while "Aggregation settings" appears on each filed. In this case, I don't know how to set "aggregation settings" group by node id. Also I cannot find document about aggregation settings of field in "Fields" section.

enter image description here

  • idk if this will work, but try turning on aggregation (use agregation: yes) and group by node id – No Sssweat May 21 '16 at 23:57
  • I tried aggregation, but in case of REST export View, in case of REST export View as the format is Serializer, no "group by" option in format setting but hal_json, json and xml options while "Aggregation settings" appears on each filed. In this case, I don't know how to set "aggregation settings" group by node id. I updated the question with the screen snap for aggregation enabled. – Jack Zhou May 22 '16 at 2:59
  • As replied here, you can create your own serializer plugin like explained here => REST API through views in Drupal 8 - how to modify serializer It's quite easy to extend and you can achieve what you're trying to do. – pbonnefoi Sep 18 '17 at 16:17
2

I've created a very simple module REST Export Nested to support nested JSON using Views Field Views.

After installing "REST Export Nested":

  1. Install and enable Views Field Views

  2. Create view display of referenced content (Articles in your case) of type "REST export" or "REST export nested"

  3. Add a relationship to the parent entity and contextual filter of parent entity ID
  4. Create a view display of the parent entity of type "REST export nested"
  5. Add required fields (e.g. "nid", "title")
  6. Add a field of type "Views field" (e.g. "articles"), configure with the correct View and display and pass "nid" as the contextual filter

The module doesn't depend on Views Field View and may work with other fields which produce a JSON string.

1

If you don't want to write your own REST api plugin you may want to try using jsonapi module that uses the jsonapi.org specification.

Jsonapi provides field collections by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.