Hi everybody,

I created a simple view (export attached) which just returns the REST export of all content entities on page. All works perfectly except the api_json export. I can see the xml, hal_json, json exports, but in the api_json I am only getting empty objects. The count of these objects is correct, it is equal to number or articles, but they're empty. The export of single articles (api/node/article/<id>?_format=api_json) works alright, I can see the json output.

Comments

petiar created an issue. See original summary.

e0ipso’s picture

I'm not sure I understand the use case for using the views export along with JSON API. Care to provide more clarification?

e0ipso’s picture

Status: Active » Postponed (maintainer needs more info)
petiar’s picture

Status: Postponed (maintainer needs more info) » Active

Hi e0ipso,

I need to provide a way to editors to sort articles on the page in the particular taxonomy term. The only way how to achieve that is, I think, to use the draggable views to get sorted list of nodes in particular taxonomy term. Is there any other way how to achieve this?

wim leers’s picture

I think I understand what's going on.

  1. Install the JSON API module. The JSON API module has its own routes, but it also provides a serialization format.
  2. Install the REST module anyway.
  3. Create a "REST Export" view, and use the api_json format.

(Note that even Preston So got this wrong in his article at https://dev.acquia.com/blog/decoupled-drupal-with-ember-introducing-embe....)

I think this is a real problem: the REST module makes every installed serialization format available for use on REST resources, even when it's a format that does not really make sense in the REST module.

Shall I create a separate issue for this, or do you want to repurpose this one, Mateu?

janlaureys’s picture

@petiar. I'm not exactly sure what you mean but you can probably do something add a sort parameters to your query like this.

?_format=api_json&sort[][path]=.weight&sort[][direction]=ASC|DESC.

with the being just a identifier that's something relevant or a number or something.

petiar’s picture

@Wim Leers: I do not understand what's the point of your comment. Is it the order in which modules have to be installed or is it something else? What's wrong in Preston's article? Of course I have JSON API module installed and the view created as you described and all formats are returning JSON output, except the JSON API.

@JanLaureys: I would need to do a custom sorting, so editors can have some drag and drop interface where they can sort articles. It is achievable via Draggable Views module, it does even exist for Drupal 8, however, I can't use it if I am not getting the correct JSON output.

Thanks for your time, guys.

e0ipso’s picture

I don't think that you will get integration between jsonapi and draggable views. I am not sure if you can do it with any of the formats out there either, but if you succeed please post back.

e0ipso’s picture

Status: Active » Closed (won't fix)
petiar’s picture

Status: Closed (won't fix) » Active

Hi @e0ipso,

sorry, but I am opening this again. It does not have to do anything with the Draggable views, that's only a mechanism for sorting which I mentioned to explain what am I doing. The real point of this ticket is, that when using the api_json format vith views, I am getting empty entities. Compare:

// 20161217111520
// http://localhost/~peterla/drupal/articles?_format=api_json

[
  {
    
  },
  {
    
  },
  {
    
  }
]

with the

[
  {
    "nid": [
      {
        "value": "1"
      }
    ],
    "uuid": [
      {
        "value": "667a8c20-b5ff-4e5d-b688-51e556ed506a"
      }
    ],
    "vid": [
      {
        "value": "7"
      }
    ],
    "langcode": [
      {
        "value": "en"
      }
    ],
    "type": [
      {
        "target_id": "page",
        "target_type": "node_type",
        "target_uuid": "5c54a9e5-94fd-4e09-a2d8-7a459c1f6f0d"
      }
    ],
    "title": [
      {
        "value": "Party Peter!"
      }
    ],
    "uid": [
      {
        "target_id": "1",
        "target_type": "user",
        "target_uuid": "569ca213-6123-4f96-909d-7fb23fbc9e05",
        "url": "\/~peterla\/drupal\/user\/1"
      }
    ],
    "status": [
      {
        "value": "1"
      }
    ],
    "created": [
      {
        "value": "1477405317"
      }
    ],
    "changed": [
      {
        "value": "1481639209"
      }
    ],
    "promote": [
      {
        "value": "0"
      }
    ],
    "sticky": [
      {
        "value": "0"
      }
    ],
    "revision_timestamp": [
      {
        "value": "1481639209"
      }
    ],
    "revision_uid": [
      {
        "target_id": "1",
        "target_type": "user",
        "target_uuid": "569ca213-6123-4f96-909d-7fb23fbc9e05",
        "url": "\/~peterla\/drupal\/user\/1"
      }
    ],
    "revision_log": [],
    "revision_translation_affected": [
      {
        "value": "1"
      }
    ],
    "default_langcode": [
      {
        "value": "1"
      }
    ],
    "path": [],
    "menu_link": [],
    "body": [
      {
        "value": "<p>And this is some content for the page.<\/p>\r\n",
        "format": "basic_html",
        "summary": ""
      }
    ]
  },
  {
    "nid": [
      {
        "value": "2"
      }
    ],
    "uuid": [
      {
        "value": "ef941231-990a-4bac-a055-43d8e17f08af"
      }
    ],
    "vid": [
      {
        "value": "5"
      }
    ],
    "langcode": [
      {
        "value": "en"
      }
    ],
    "type": [
      {
        "target_id": "article",
        "target_type": "node_type",
        "target_uuid": "013a85ee-3671-4ae8-be5b-ad2281d85f69"
      }
    ],
    "title": [
      {
        "value": "My first article"
      }
    ],
    "uid": [
      {
        "target_id": "1",
        "target_type": "user",
        "target_uuid": "569ca213-6123-4f96-909d-7fb23fbc9e05",
        "url": "\/~peterla\/drupal\/user\/1"
      }
    ],
    "status": [
      {
        "value": "1"
      }
    ],
    "created": [
      {
        "value": "1481616327"
      }
    ],
    "changed": [
      {
        "value": "1481635694"
      }
    ],
    "promote": [
      {
        "value": "1"
      }
    ],
    "sticky": [
      {
        "value": "0"
      }
    ],
    "revision_timestamp": [
      {
        "value": "1481635694"
      }
    ],
    "revision_uid": [
      {
        "target_id": "1",
        "target_type": "user",
        "target_uuid": "569ca213-6123-4f96-909d-7fb23fbc9e05",
        "url": "\/~peterla\/drupal\/user\/1"
      }
    ],
    "revision_log": [],
    "revision_translation_affected": [
      {
        "value": "1"
      }
    ],
    "default_langcode": [
      {
        "value": "1"
      }
    ],
    "path": [],
    "menu_link": [],
    "body": [
      {
        "value": "<p>Hello world!<\/p>\r\n",
        "format": "basic_html",
        "summary": ""
      }
    ],
    "field_edition": [
      {
        "target_id": "5",
        "target_type": "taxonomy_term",
        "target_uuid": "4e8382ed-a54c-491f-bf7c-39add4cb9c99",
        "url": "\/~peterla\/drupal\/taxonomy\/term\/5"
      }
    ],
    "field_image": [
      {
        "target_id": "1",
        "alt": "This is Peter.",
        "title": "",
        "width": "584",
        "height": "584",
        "target_type": "file",
        "target_uuid": "38cde5b1-6baa-411d-a3a7-26784f02244d",
        "url": "http:\/\/localhost\/~peterla\/drupal\/sites\/default\/files\/2016-12\/700206-peter-lachky.jpg"
      }
    ],
    "field_section": [
      {
        "target_id": "9",
        "target_type": "taxonomy_term",
        "target_uuid": "f3ce263f-7845-487b-a433-a7c27d88b855",
        "url": "\/~peterla\/drupal\/taxonomy\/term\/9"
      }
    ],
    "field_tags": [
      {
        "target_id": "1",
        "target_type": "taxonomy_term",
        "target_uuid": "b7c48e61-194a-45a0-8693-9741fc9bc4e6",
        "url": "\/~peterla\/drupal\/taxonomy\/term\/1"
      },
      {
        "target_id": "2",
        "target_type": "taxonomy_term",
        "target_uuid": "b09768cb-e71b-4a5f-89ab-253d2dc107b2",
        "url": "\/~peterla\/drupal\/taxonomy\/term\/2"
      },
      {
        "target_id": "3",
        "target_type": "taxonomy_term",
        "target_uuid": "e4e7365c-20dd-4891-a701-c40666a1609b",
        "url": "\/~peterla\/drupal\/taxonomy\/term\/3"
      }
    ]
  },
  {
    "nid": [
      {
        "value": "3"
      }
    ],
    "uuid": [
      {
        "value": "fd678a3a-ff36-4ee1-8746-e3497cabe604"
      }
    ],
    "vid": [
      {
        "value": "4"
      }
    ],
    "langcode": [
      {
        "value": "en"
      }
    ],
    "type": [
      {
        "target_id": "article",
        "target_type": "node_type",
        "target_uuid": "013a85ee-3671-4ae8-be5b-ad2281d85f69"
      }
    ],
    "title": [
      {
        "value": "Another article."
      }
    ],
    "uid": [
      {
        "target_id": "1",
        "target_type": "user",
        "target_uuid": "569ca213-6123-4f96-909d-7fb23fbc9e05",
        "url": "\/~peterla\/drupal\/user\/1"
      }
    ],
    "status": [
      {
        "value": "1"
      }
    ],
    "created": [
      {
        "value": "1481616512"
      }
    ],
    "changed": [
      {
        "value": "1481635680"
      }
    ],
    "promote": [
      {
        "value": "1"
      }
    ],
    "sticky": [
      {
        "value": "0"
      }
    ],
    "revision_timestamp": [
      {
        "value": "1481635680"
      }
    ],
    "revision_uid": [
      {
        "target_id": "1",
        "target_type": "user",
        "target_uuid": "569ca213-6123-4f96-909d-7fb23fbc9e05",
        "url": "\/~peterla\/drupal\/user\/1"
      }
    ],
    "revision_log": [],
    "revision_translation_affected": [
      {
        "value": "1"
      }
    ],
    "default_langcode": [
      {
        "value": "1"
      }
    ],
    "path": [],
    "menu_link": [],
    "body": [
      {
        "value": "<p>This article is just so we have more than 1.<\/p>\r\n",
        "format": "basic_html",
        "summary": ""
      }
    ],
    "field_edition": [
      {
        "target_id": "5",
        "target_type": "taxonomy_term",
        "target_uuid": "4e8382ed-a54c-491f-bf7c-39add4cb9c99",
        "url": "\/~peterla\/drupal\/taxonomy\/term\/5"
      }
    ],
    "field_image": [],
    "field_section": [
      {
        "target_id": "8",
        "target_type": "taxonomy_term",
        "target_uuid": "a5efe427-e729-49de-8892-22c379ef040c",
        "url": "\/~peterla\/drupal\/taxonomy\/term\/8"
      }
    ],
    "field_tags": [
      {
        "target_id": "3",
        "target_type": "taxonomy_term",
        "target_uuid": "e4e7365c-20dd-4891-a701-c40666a1609b",
        "url": "\/~peterla\/drupal\/taxonomy\/term\/3"
      }
    ]
  }
]

The first one is api_json, the second one just json. As you can see, it is exporting three entities/nodes, which is correct, but they are empty. It is very simple view.

petiar’s picture

Category: Support request » Bug report
StatusFileSize
new4.82 KB
new427 bytes
new31 bytes
new82.7 KB

Hi there,

as I would like to get this issue solved as soon as possible, I am providing more info. I made a clean install of Drupal 8.2.4, enabled to modules related to REST (HAL, HTTP Basic Authentication, RESTful Web Services, Serialization), installed and enabled JSON API. Generated couple of articles using the drupal console and created a simple view. Still providing empty object where there should be nodes when using JSON API format. As this works alright for other formats, I am considering this as a bug.

Attached you can find all relevant files. There are JSON views output in json and api_json format, yml export of the REST settings and the yml export of the view (the 4.82 KB is the one I uploaded now, use that one, please).

If you want any more information or if I could be of any help, let me know.

e0ipso’s picture

Category: Bug report » Support request

[…] that when using the api_json format vith views

I am yet to understand why is that you want to use JSON API with views. What are you trying to accomplish that you can't accomplish with views that you cannot do with JSON API collections?

There is no explicit support for having the api_json format work with views, because the jsonapi module implements that feature in a completely different way, that we think it's much more flexible.

What I think that the problem is, is that views should not offer all formats in their UI, it should exclude api_json. Unfortunately this is not something that can be fixed in this module, but I'll be glad to review any patches to core in that regard.

wim leers’s picture

#12: please re-read what I wrote in #5. I think that's the big problem here: we let other modules, like the REST module, use the JSON API format. This is wrong/problematic.

petiar’s picture

Hey guys,

I am deeply sorry, the only problem here was the lack of understanding the JSON API concepts. The word collection is the answer to my problem(s). I found this link: https://www.drupal.org/node/2803141 and it answers almost all my questions, except the sorting and pagination. Could you, please, specify an example of sorting and pagination GET request? Feel free to close this ticket as "works as designed". Thanks for your effort.

petiar’s picture

Status: Active » Closed (works as designed)

Got it, for example:

api/node/article?_format=api_json&sort=-nid will sort nodes by nid, descending (hence the -) sign.

e0ipso’s picture

@petiar no need for apologies, this will be great documentation for the next person that has those same questions. It will be amazing if you could write what you found confusing in the project documentation: https://www.drupal.org/node/2803093

We will see you in your next issue :-)

wim leers’s picture

alexevh’s picture

@petiar

Your comment works except because I cant use that with limit 1

I want to retrieve the last article of a category so I made this call

http://example.org/jsonapi/node/Tienda?page[limit]=1 and as result I have one article only but is the first

I want the last , If I make this call http://example.org/jsonapi/node/Tienda?sort=-nid I get every single article sorted DESC,

I want both things I want the sorting to be DESC but only one article

Any advice?