Avoiding Duplication/Redundancy in REST API Responses

847 views
Skip to first unread message

skarlovic

unread,
Dec 7, 2012, 2:16:33 PM12/7/12
to api-...@googlegroups.com
I am posting this to see if there are any opinions out there on a best practice to handle redundancy in responses.

The data being returned in our REST API responses can become large depending on the specific call. This is partly because much of the data is repeated/redundant.

For example, the same items are repeated in different groups. See the XML below. When looking at the example ignore the meaning of a group, item etc., I just made up some element names for the purpose of the example.

An item can have a lot of information associated with it. It makes the most sense for us to represent the items at this level, however, we are trying to avoid having to the consumer make multiple calls to retrieve all the item information if we don't contain all the data at this level and we just provide the link/reference to the item. We want the response to contain all the data for each item but only want to represent it once in the response. 

I'd be interested in hearing how others have addressed the redundancy issue while avoiding clients having to make multiple calls to your API. Sorry, if there is already a similar topic or question, I couldn't find one in this group. Thanks in advance!

Steve 



<groups>
<group>
<itemCategory id="1001">
<code></code>
<description></description>
</itemCategory>
<items>
<item id="2001">
<code></code>
<description></description>
</item>
<item id="2002">
<code></code>
<description></description>
</item>
<item id="2003">
<code></code>
<description></description>
</item>
</items>
</group>
<group>
<itemCategory id="1002">
<code></code>
<description></description>
</itemCategory>
<items>
<item id="2001">
<code></code>
<description></description>
</item>
<item id="2002">
<code></code>
<description></description>
</item>
<item id="2003">
<code></code>
<description></description>
</item>
</items>
</group>
</groups>


Romain Bessuges-Meusy

unread,
Dec 14, 2012, 5:24:34 PM12/14/12
to api-...@googlegroups.com
Intuitively, I'll build a hash map of items and make internal references :
<groups>
    <group>
        <item id=23/>
        <item id=25/>
    </group>

    <group>
        <item id=24/>
        <item id=25/>
    </group>
</groups>
<items>
        <item>
            <id>23</id>
            <title>foo</title>
        </item>

        <item>
            <id>24</id>
            <title>foo</title>
        </item>

        <item>
            <id>25</id>
            <title>foo</title>
        </item>
</item>

skarlovic

unread,
Dec 15, 2012, 4:20:29 PM12/15/12
to api-...@googlegroups.com
Thanks for your reply Romain, this is what we were leaning towards but wanted to see get some opinions. This doesn't 'feel' very RESTful and I haven't seen this sort of pattern used anywhere yet, but it definitely solves the problem. Do you happen to know if there are APIs which leverage this pattern to solve this particular problem?

Thanks,
Steve

Jørn Wildt

unread,
Dec 15, 2012, 4:28:15 PM12/15/12
to api-...@googlegroups.com
Seems to me like a good approach. There is nothing in REST that prescribes anything in the payload, beyond hyper linking, so choose what ever fits your use case. A local dictionary as suggested seems to solve the problem in a simple way.

You can then include links for the client to retrieve further information if required:

<items>
        <item src="http://url-to-item-details">
            <id>23</id>
            <title>foo</title>
        </item>
</Items>

/Jørn






--
You received this message because you are subscribed to the Google Groups "API Craft" group.
To unsubscribe from this group, send email to api-craft+...@googlegroups.com.
Visit this group at http://groups.google.com/group/api-craft?hl=en.
 
 

skarlovic

unread,
Dec 17, 2012, 1:20:55 PM12/17/12
to api-...@googlegroups.com
Good point. Thanks for your feedback and suggestions.
Reply all
Reply to author
Forward
0 new messages