Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

Drupal 8 jsonapi: How to change the structure of returned json relationships included array?

I am sending a request to the jsonapi node endpoint, and using the following parameter to include the "relationship" objects of those nodes for user (post author data) and user picture (avatar):

include=uid,uid.user_picture

This returns to me the json data of the nodes as well as all of the relationship objects lumped together (regardless of type) into the "included" array. The included array ends up looking like this by default:

"included": [
        {
            "type": "user--user",
            "id": "4c717273-f903-4ffa-abe2-b5e5709ad727",
            "attributes": {
                "display_name": "cherylm1234",
               ...etc...
            }
        },
        {
            "type": "file--file",
            "id": "4c717273-f903-4ffa-abe2-b5e5709ad727",
            "attributes": {
                "filename": "Cheryl-Fun.jpg",
               ...etc...
            }
        }
    ]

Context: We are trying to integrate jsonapi with an iOS app as the client/consumer.

My Problem: It's difficult to write up data models in the iOS client for these relationships (without looping and sorting every response) since they are all lumped into the same level of the "included" array. Also a problem is that each "type" will have its own set of attributes/fields so data models in iOS need to be built based on "type".

Question: Is it possible to change this structure so that all included objects are sorted by type?

I would like for the "included" array to be indexed by "type" where all objects of that type would be inside that index. Maybe something like:

included['user--user'] = [
    {user-object},
    {user-object},
    ...etc.
],
included['file--file'] = [
    {file-object},
    {file-object},
    ....etc.
],

I'm assuming this would require a custom module with some sort of hook where I could loop and sort the data before returning it to the iOS app. But, haven't been able to find any documentation on this type of json response modification.

Any help would be much appreciated.

question from:https://stackoverflow.com/questions/65836362/drupal-8-jsonapi-how-to-change-the-structure-of-returned-json-relationships-inc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Drupal implements the JSON:API specification. The specification specifies how a resource should be encoded. Both if it is returned as primary data or included as a relation resource.

If you want the data to be returned in another format, you would need to implement your own REST API in Drupal following another specification.

But if this is only about managing the data in a client, I would recommend to use one of the existing libraries for JSON:API specification. A list of implementations is provided at jsonapi.org/implementations/. The JSONAPI Swift package listed on that page seems to be well maintained.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...